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