| 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 type { WebhookOwner |
| 10 | import { repositoryFullNameOf } |
| 11 | |
| 12 | type WebhookJob |
| 13 | endpointId: string |
| 14 | event: string |
| 15 | /** The exact bytes the receiver gets and the signature covers, frozen here so a retry sends the same thing. */ |
| 16 | body: string |
| 17 | /** The same key enqueues once, so a retried outbox event queues no duplicate. */ |
| 18 | idempotencyKey: string |
| 19 | }; |
| 20 | |
| 21 | /** Turns repository events into webhook jobs for apps/delivery, which signs and sends them. */ |
| 22 | @Injectable() |
| 23 | export class WebhookFanoutService |
| 24 | private readonly appUrl |
| 25 | |
| 26 | constructor( |
| 27 | @Inject(DATABASE) |
| 28 | config: ConfigService |
| 29 | ) { |
| 30 | this.appUrl = |
| 31 | } |
| 32 | |
| 33 | async handle(event |
| 34 | const endpoints = |
| 35 | .select({ id: |
| 36 | .from(schema.webhookEndpoint |
| 37 | .where( |
| 38 | and( |
| 39 | eq(schema.webhookEndpoint |
| 40 | arrayContains(schema.webhookEndpoint |
| 41 | or( |
| 42 | eq(schema.webhookEndpoint |
| 43 | inArray( |
| 44 | schema.webhookEndpoint.organizationId |
| 45 | this.db |
| 46 | .select({ id: |
| 47 | .from(schema.repository |
| 48 | .where(eq(schema |
| 49 | ), |
| 50 | ), |
| 51 | ), |
| 52 | ); |
| 53 | if (endpoints.length |
| 54 | |
| 55 | const body = |
| 56 | if (!body) |
| 57 | await this.enqueue |
| 58 | endpoints.map((endpoint |
| 59 | endpointId: endpoint.id |
| 60 | event: event.type |
| 61 | body, |
| 62 | idempotencyKey: `${event. |
| 63 | })), |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | /** Sent when an endpoint is created and from its "Send test" button, whatever events it chose. */ |
| 68 | async ping( |
| 69 | endpoint: { |
| 70 | owner: WebhookOwner |
| 71 | ) { |
| 72 | await this.enqueue |
| 73 | { |
| 74 | endpointId: endpoint.id |
| 75 | event: 'ping', |
| 76 | body: JSON.stringify |
| 77 | event: 'ping', |
| 78 | webhook: endpoint, |
| 79 | ...('repositoryId' in |
| 80 | ? { repository: { |
| 81 | : { organization: { |
| 82 | createdAt: new Date |
| 83 | }), |
| 84 | idempotencyKey: `ping:${endpoint |
| 85 | }, |
| 86 | ]); |
| 87 | } |
| 88 | |
| 89 | async enqueue(jobs |
| 90 | if (jobs.length |
| 91 | await this.db |
| 92 | .insert(schema.deliveryJob |
| 93 | .values( |
| 94 | jobs.map((job |
| 95 | kind: 'webhook' as |
| 96 | idempotencyKey: `webhook:${job |
| 97 | endpointId: job.endpointId |
| 98 | payload: { event: job |
| 99 | })), |
| 100 | ) |
| 101 | .onConflictDoNothing(); |
| 102 | await this.db |
| 103 | } |
| 104 | |
| 105 | /** The JSON a receiver gets. Null when what the event points at is gone. */ |
| 106 | private async bodyOf |
| 107 | const repository = |
| 108 | if (!repository) |
| 109 | |
| 110 | const body: |
| 111 | event: event.type |
| 112 | repository, |
| 113 | sender: event.actorId |
| 114 | createdAt: new Date |
| 115 | }; |
| 116 | |
| 117 | if ('issueId' in |
| 118 | const [thread] |
| 119 | .select({ |
| 120 | id: schema.issue |
| 121 | number: schema.issue |
| 122 | title: schema.issue |
| 123 | body: schema.issue |
| 124 | state: schema.issue |
| 125 | isPullRequest: schema.issue |
| 126 | authorId: schema.issue |
| 127 | }) |
| 128 | .from(schema.issue |
| 129 | .where(eq(schema |
| 130 | if (!thread) |
| 131 | |
| 132 | const kind = |
| 133 | body[thread.isPullRequest |
| 134 | id: thread.id |
| 135 | number: thread.number |
| 136 | title: thread.title |
| 137 | body: thread.body |
| 138 | state: thread.state |
| 139 | author: await this |
| 140 | htmlUrl: `${repository. |
| 141 | }; |
| 142 | } |
| 143 | |
| 144 | switch (event.type |
| 145 | case 'push': { |
| 146 | const { ref, |
| 147 | Object.assign(body |
| 148 | ref, |
| 149 | before, |
| 150 | after, |
| 151 | created: /^0+$/ |
| 152 | deleted: /^0+$/ |
| 153 | commits, |
| 154 | }); |
| 155 | break; |
| 156 | } |
| 157 | case 'issue.commented': |
| 158 | case 'issue.comment_edited': { |
| 159 | const [comment] |
| 160 | .select({ |
| 161 | id: schema.issueComment |
| 162 | body: schema.issueComment |
| 163 | }) |
| 164 | .from(schema.issueComment |
| 165 | .where(eq(schema |
| 166 | if (!comment) |
| 167 | body.comment = |
| 168 | break; |
| 169 | } |
| 170 | case 'issue.comment_deleted': |
| 171 | body.comment = |
| 172 | break; |
| 173 | case 'pull_request.review_commented': { |
| 174 | const [comment] |
| 175 | .select({ |
| 176 | id: schema.pullRequestReviewComment |
| 177 | body: schema.pullRequestReviewComment |
| 178 | path: schema.pullRequestReviewComment |
| 179 | }) |
| 180 | .from(schema.pullRequestReviewComment |
| 181 | .where( |
| 182 | eq(schema.pullRequestReviewComment |
| 183 | ); |
| 184 | if (!comment) |
| 185 | body.comment = |
| 186 | break; |
| 187 | } |
| 188 | case 'pull_request.reviewed': |
| 189 | case 'pull_request.review_dismissed': { |
| 190 | const [review] |
| 191 | .select({ |
| 192 | id: schema.pullRequestReview |
| 193 | state: schema.pullRequestReview |
| 194 | body: schema.pullRequestReview |
| 195 | dismissalMessage: schema.pullRequestReview |
| 196 | }) |
| 197 | .from(schema.pullRequestReview |
| 198 | .where(eq(schema |
| 199 | if (!review) |
| 200 | body.review = |
| 201 | break; |
| 202 | } |
| 203 | case 'issue.assigned': |
| 204 | case 'issue.unassigned': { |
| 205 | const assignee = |
| 206 | if (!assignee) |
| 207 | body.assignee = |
| 208 | break; |
| 209 | } |
| 210 | case 'member.added': |
| 211 | case 'member.removed': { |
| 212 | const member = |
| 213 | if (!member) |
| 214 | body.member = |
| 215 | break; |
| 216 | } |
| 217 | case 'issue.labeled': |
| 218 | case 'issue.unlabeled': |
| 219 | case 'label.created': |
| 220 | case 'label.edited': { |
| 221 | const [label] |
| 222 | .select({ |
| 223 | id: schema.label |
| 224 | name: schema.label |
| 225 | description: schema.label |
| 226 | color: schema.label |
| 227 | }) |
| 228 | .from(schema.label |
| 229 | .where(eq(schema |
| 230 | if (!label) |
| 231 | body.label = |
| 232 | break; |
| 233 | } |
| 234 | case 'label.deleted': |
| 235 | body.label = |
| 236 | break; |
| 237 | case 'release.created': |
| 238 | case 'release.published': |
| 239 | case 'release.edited': { |
| 240 | const [release] |
| 241 | .select({ |
| 242 | id: schema.release |
| 243 | tagName: schema.release |
| 244 | name: schema.release |
| 245 | body: schema.release |
| 246 | isDraft: schema.release |
| 247 | isPrerelease: schema.release |
| 248 | publishedAt: schema.release |
| 249 | }) |
| 250 | .from(schema.release |
| 251 | .where(eq(schema |
| 252 | if (!release) |
| 253 | body.release = |
| 254 | ...release, |
| 255 | htmlUrl: `${repository. |
| 256 | }; |
| 257 | break; |
| 258 | } |
| 259 | case 'release.deleted': |
| 260 | body.release = |
| 261 | break; |
| 262 | case 'fork.created': { |
| 263 | const fork = |
| 264 | if (!fork) |
| 265 | body.fork = |
| 266 | break; |
| 267 | } |
| 268 | case 'repository.transferred': |
| 269 | body.from = |
| 270 | break; |
| 271 | } |
| 272 | return JSON.stringify |
| 273 | } |
| 274 | |
| 275 | private async repositoryRef |
| 276 | const [repository] |
| 277 | .select({ |
| 278 | id: schema.repository |
| 279 | fullName: repositoryFullNameOf( |
| 280 | schema.user, |
| 281 | schema.organization, |
| 282 | schema.repository, |
| 283 | ), |
| 284 | description: schema.repository |
| 285 | visibility: schema.repository |
| 286 | defaultBranch: schema.repository |
| 287 | }) |
| 288 | .from(schema.repository |
| 289 | .innerJoin(schema.user |
| 290 | .leftJoin( |
| 291 | schema.organization, |
| 292 | eq(schema.organization |
| 293 | ) |
| 294 | .where(eq(schema |
| 295 | return repository |
| 296 | ? { ...repository |
| 297 | : null; |
| 298 | } |
| 299 | |
| 300 | private async userRef |
| 301 | const [user] |
| 302 | .select({ id: |
| 303 | .from(schema.user |
| 304 | .where(eq(schema |
| 305 | return user ?? |
| 306 | } |
| 307 | } |