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