| 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. Issue events cover pull requests too, since a pull request is an issue. */ |
| 7 | export type EventPayloads |
| 8 | 'issue.opened': |
| 9 | 'issue.closed': |
| 10 | 'issue.reopened': |
| 11 | 'issue.assigned': |
| 12 | 'issue.commented': |
| 13 | 'pull_request.merged': |
| 14 | 'pull_request.reviewed': |
| 15 | 'pull_request.review_commented': |
| 16 | /** One per ref a push moved. Unlike the others it carries what it describes, since commits are not rows to read back. */ |
| 17 | push: { |
| 18 | ref: string |
| 19 | /** 40 zeros when the push created the ref. */ |
| 20 | before: string |
| 21 | /** 40 zeros when the push deleted the ref. */ |
| 22 | after: string |
| 23 | /** Newest first, at most `MAX_PUSH_COMMITS`. */ |
| 24 | commits: Commit |
| 25 | }; |
| 26 | }; |
| 27 | |
| 28 | /** Enough to see what a push did without making a huge push a huge payload; GitHub caps at 20 too. */ |
| 29 | export const MAX_PUSH_COMMITS = |
| 30 | |
| 31 | export type EventType |
| 32 | |
| 33 | export type RepositoryEvent |
| 34 | [Type |
| 35 | type: Type |
| 36 | repositoryId: string |
| 37 | actorId: string |
| 38 | payload: EventPayloads |
| 39 | }; |
| 40 | }[EventType] |
| 41 | |
| 42 | /** An event as a consumer receives it: stored, so it has an id that stays the same across retries. */ |
| 43 | export type StoredEvent |
| 44 | |
| 45 | /** Queues `event` for every consumer. Pass the transaction making the change, so the event exists exactly when the change does. */ |
| 46 | export async function publishEvent |
| 47 | await db.insert |
| 48 | } |