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