| 1 | import { randomUUID } from 'node:crypto'; |
| 2 | import { type Database, |
| 3 | import { Inject, Injectable |
| 4 | import { ConfigService } |
| 5 | import { and, arrayContains |
| 6 | |
| 7 | import { DATABASE } |
| 8 | import type { StoredEvent |
| 9 | import { ownerNameOf } |
| 10 | |
| 11 | type WebhookJob |
| 12 | endpointId: string |
| 13 | event: string |
| 14 | /** The exact bytes the receiver gets and the signature covers, frozen here so a retry sends the same thing. */ |
| 15 | body: string |
| 16 | /** The same key enqueues once, so a retried outbox event queues no duplicate. */ |
| 17 | idempotencyKey: string |
| 18 | }; |
| 19 | |
| 20 | /** Turns repository events into webhook jobs for apps/delivery, which signs and sends them. */ |
| 21 | @Injectable() |
| 22 | export class WebhookFanoutService |
| 23 | private readonly appUrl |
| 24 | |
| 25 | constructor( |
| 26 | @Inject(DATABASE) |
| 27 | config: ConfigService |
| 28 | ) { |
| 29 | this.appUrl = |
| 30 | } |
| 31 | |
| 32 | async handle(event |
| 33 | const endpoints = |
| 34 | .select({ id: |
| 35 | .from(schema.webhookEndpoint |
| 36 | .where( |
| 37 | and( |
| 38 | eq(schema.webhookEndpoint |
| 39 | arrayContains(schema.webhookEndpoint |
| 40 | or( |
| 41 | eq(schema.webhookEndpoint |
| 42 | inArray( |
| 43 | schema.webhookEndpoint.organizationId |
| 44 | this.db |
| 45 | .select({ id: |
| 46 | .from(schema.repository |
| 47 | .where(eq(schema |
| 48 | ), |
| 49 | ), |
| 50 | ), |
| 51 | ); |
| 52 | if (endpoints.length |
| 53 | |
| 54 | const body = |
| 55 | if (!body) |
| 56 | await this.enqueue |
| 57 | endpoints.map((endpoint |
| 58 | endpointId: endpoint.id |
| 59 | event: event.type |
| 60 | body, |
| 61 | idempotencyKey: `${event. |
| 62 | })), |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | /** Sent when an endpoint is created and from its "Send test" button, whatever events it chose. */ |
| 67 | async ping(endpoint |
| 68 | id: string |
| 69 | url: string |
| 70 | events: string |
| 71 | repository: string |
| 72 | }) { |
| 73 | await this.enqueue |
| 74 | { |
| 75 | endpointId: endpoint.id |
| 76 | event: 'ping', |
| 77 | body: JSON.stringify |
| 78 | event: 'ping', |
| 79 | webhook: { |
| 80 | id: endpoint.id |
| 81 | url: endpoint.url |
| 82 | events: endpoint.events |
| 83 | }, |
| 84 | repository: { full_name: endpoint |
| 85 | created_at: new Date |
| 86 | }), |
| 87 | idempotencyKey: `ping:${endpoint |
| 88 | }, |
| 89 | ]); |
| 90 | } |
| 91 | |
| 92 | async enqueue(jobs |
| 93 | if (jobs.length |
| 94 | await this.db |
| 95 | .insert(schema.deliveryJob |
| 96 | .values( |
| 97 | jobs.map((job |
| 98 | kind: 'webhook' as |
| 99 | idempotencyKey: `webhook:${job |
| 100 | endpointId: job.endpointId |
| 101 | payload: { event: job |
| 102 | })), |
| 103 | ) |
| 104 | .onConflictDoNothing(); |
| 105 | await this.db |
| 106 | } |
| 107 | |
| 108 | /** The JSON a receiver gets. Null when what the event points at is gone. */ |
| 109 | private async bodyOf |
| 110 | const [repository] |
| 111 | .select({ |
| 112 | id: schema.repository |
| 113 | fullName: sql<string |
| 114 | visibility: schema.repository |
| 115 | }) |
| 116 | .from(schema.repository |
| 117 | .innerJoin(schema.user |
| 118 | .leftJoin( |
| 119 | schema.organization, |
| 120 | eq(schema.organization |
| 121 | ) |
| 122 | .where(eq(schema |
| 123 | if (!repository) |
| 124 | const repositoryUrl = |
| 125 | |
| 126 | const body: |
| 127 | event: event.type |
| 128 | repository: { |
| 129 | id: repository.id |
| 130 | full_name: repository.fullName |
| 131 | visibility: repository.visibility |
| 132 | html_url: repositoryUrl, |
| 133 | }, |
| 134 | sender: event.actorId |
| 135 | created_at: new Date |
| 136 | }; |
| 137 | |
| 138 | if (event.type |
| 139 | const { ref, |
| 140 | Object.assign(body |
| 141 | ref, |
| 142 | before, |
| 143 | after, |
| 144 | created: /^0+$/ |
| 145 | deleted: /^0+$/ |
| 146 | commits: commits.map |
| 147 | ...commit, |
| 148 | html_url: `${repositoryUrl} |
| 149 | })), |
| 150 | }); |
| 151 | return JSON.stringify |
| 152 | } |
| 153 | |
| 154 | const [thread] |
| 155 | .select({ |
| 156 | id: schema.issue |
| 157 | number: schema.issue |
| 158 | title: schema.issue |
| 159 | body: schema.issue |
| 160 | state: schema.issue |
| 161 | isPullRequest: schema.issue |
| 162 | authorId: schema.issue |
| 163 | }) |
| 164 | .from(schema.issue |
| 165 | .where(eq(schema |
| 166 | if (!thread) |
| 167 | |
| 168 | const kind = |
| 169 | body[thread.isPullRequest |
| 170 | id: thread.id |
| 171 | number: thread.number |
| 172 | title: thread.title |
| 173 | body: thread.body |
| 174 | state: thread.state |
| 175 | author: await this |
| 176 | html_url: `${repositoryUrl} |
| 177 | }; |
| 178 | |
| 179 | switch (event.type |
| 180 | case 'issue.commented': { |
| 181 | const [comment] |
| 182 | .select({ |
| 183 | id: schema.issueComment |
| 184 | body: schema.issueComment |
| 185 | }) |
| 186 | .from(schema.issueComment |
| 187 | .where(eq(schema |
| 188 | if (!comment) |
| 189 | body.comment = |
| 190 | break; |
| 191 | } |
| 192 | case 'pull_request.review_commented': { |
| 193 | const [comment] |
| 194 | .select({ |
| 195 | id: schema.pullRequestReviewComment |
| 196 | body: schema.pullRequestReviewComment |
| 197 | path: schema.pullRequestReviewComment |
| 198 | }) |
| 199 | .from(schema.pullRequestReviewComment |
| 200 | .where( |
| 201 | eq(schema.pullRequestReviewComment |
| 202 | ); |
| 203 | if (!comment) |
| 204 | body.comment = |
| 205 | break; |
| 206 | } |
| 207 | case 'pull_request.reviewed': { |
| 208 | const [review] |
| 209 | .select({ |
| 210 | id: schema.pullRequestReview |
| 211 | state: schema.pullRequestReview |
| 212 | body: schema.pullRequestReview |
| 213 | }) |
| 214 | .from(schema.pullRequestReview |
| 215 | .where(eq(schema |
| 216 | if (!review) |
| 217 | body.review = |
| 218 | break; |
| 219 | } |
| 220 | case 'issue.assigned': { |
| 221 | const assignee = |
| 222 | if (!assignee) |
| 223 | body.assignee = |
| 224 | break; |
| 225 | } |
| 226 | } |
| 227 | return JSON.stringify |
| 228 | } |
| 229 | |
| 230 | private async userRef |
| 231 | const [user] |
| 232 | .select({ id: |
| 233 | .from(schema.user |
| 234 | .where(eq(schema |
| 235 | return user ?? |
| 236 | } |
| 237 | } |