| 1 | import { schema } from '@ghost/db'; |
| 2 | import { sql } |
| 3 | |
| 4 | import type { Commit |
| 5 | import type { Executor |
| 6 | |
| 7 | /** What each event carries. Ids only: a consumer reads the rows as they are when it runs, and skips an event whose rows are gone. A deletion carries what it deleted, since there is no row left to read. Issue events cover pull requests too, since a pull request is an issue. */ |
| 8 | export type EventPayloads |
| 9 | 'issue.opened': |
| 10 | 'issue.edited': |
| 11 | 'issue.closed': |
| 12 | 'issue.reopened': |
| 13 | 'issue.assigned': |
| 14 | 'issue.unassigned': |
| 15 | 'issue.labeled': |
| 16 | 'issue.unlabeled': |
| 17 | 'issue.commented': |
| 18 | 'issue.comment_edited': |
| 19 | 'issue.comment_deleted': |
| 20 | issueId: string |
| 21 | comment: { |
| 22 | }; |
| 23 | 'pull_request.ready_for_review': |
| 24 | 'pull_request.converted_to_draft': |
| 25 | 'pull_request.merged': |
| 26 | 'pull_request.reviewed': |
| 27 | 'pull_request.review_dismissed': |
| 28 | 'pull_request.review_commented': |
| 29 | 'label.created': |
| 30 | 'label.edited': |
| 31 | 'label.deleted': |
| 32 | label: { |
| 33 | id: string |
| 34 | name: string |
| 35 | description: string |
| 36 | color: string |
| 37 | }; |
| 38 | }; |
| 39 | 'release.created': |
| 40 | /** On create when not a draft, and each time a draft is published. */ |
| 41 | 'release.published': |
| 42 | 'release.edited': |
| 43 | 'release.deleted': |
| 44 | release: { |
| 45 | }; |
| 46 | 'star.created': |
| 47 | 'star.deleted': |
| 48 | /** Watching at `all`; the actor is the watcher. */ |
| 49 | 'watch.started': |
| 50 | /** Published on the repository that was forked. */ |
| 51 | 'fork.created': |
| 52 | 'repository.edited': |
| 53 | /** `from` is the previous owner's name. */ |
| 54 | 'repository.transferred': |
| 55 | /** An invited collaborator accepted. */ |
| 56 | 'member.added': |
| 57 | /** A collaborator who had accepted was removed, or left. */ |
| 58 | 'member.removed': |
| 59 | /** One per ref a push moved. Unlike the others it carries what it describes, since commits are not rows to read back. */ |
| 60 | push: { |
| 61 | ref: string |
| 62 | /** 40 zeros when the push created the ref. */ |
| 63 | before: string |
| 64 | /** 40 zeros when the push deleted the ref. */ |
| 65 | after: string |
| 66 | /** Newest first, at most `MAX_PUSH_COMMITS`. */ |
| 67 | commits: Commit |
| 68 | }; |
| 69 | }; |
| 70 | |
| 71 | /** Enough to see what a push did without making a huge push a huge payload; GitHub caps at 20 too. */ |
| 72 | export const MAX_PUSH_COMMITS = |
| 73 | |
| 74 | export type EventType |
| 75 | |
| 76 | export type RepositoryEvent |
| 77 | [Type |
| 78 | type: Type |
| 79 | repositoryId: string |
| 80 | actorId: string |
| 81 | payload: EventPayloads |
| 82 | }; |
| 83 | }[EventType] |
| 84 | |
| 85 | /** An event as a consumer receives it: stored, so it has an id that stays the same across retries. */ |
| 86 | export type StoredEvent |
| 87 | |
| 88 | /** Queues `event` for every consumer. Pass the transaction making the change, so the event exists exactly when the change does. */ |
| 89 | export async function publishEvent |
| 90 | await db.insert |
| 91 | // Postgres holds the notification until the transaction commits and folds repeats into one. |
| 92 | await db.execute |
| 93 | } |
| 94 | |
| 95 | export const OUTBOX_CHANNEL = |