| 1 | import { schema } from '@ghost/db'; |
| 2 | import { sql } |
| 3 | |
| 4 | import { listCommits } |
| 5 | import { alternatesEnv } |
| 6 | import type { Executor |
| 7 | |
| 8 | // What one push adds to a timeline, as GitHub caps it. A longer push shows its newest commits. |
| 9 | export const MAX_TIMELINE_COMMITS = |
| 10 | |
| 11 | /** Commits `tip` brings that none of `exclude` reaches, oldest first: what a push added to a pull request, leaving out the base commits a merge or rebase pulled in. */ |
| 12 | export async function commitsAdded |
| 13 | gitDir, |
| 14 | alternates, |
| 15 | tip, |
| 16 | exclude, |
| 17 | }: { |
| 18 | gitDir: string |
| 19 | alternates: string |
| 20 | tip: string |
| 21 | exclude: string |
| 22 | }) { |
| 23 | const { commits |
| 24 | gitDir, |
| 25 | env: alternatesEnv(alternates |
| 26 | ref: [tip, |
| 27 | limit: MAX_TIMELINE_COMMITS, |
| 28 | }); |
| 29 | return commits.reverse |
| 30 | } |
| 31 | |
| 32 | /** Writes a pull request's `committed` events, after a `head_force_pushed` one when the push rewrote the branch. Each row is a millisecond after the last, so the timeline keeps the commits in order and after anything else written in the same transaction. */ |
| 33 | export async function recordCommitEvents |
| 34 | db: Executor |
| 35 | { |
| 36 | issueId, |
| 37 | actorId, |
| 38 | commits, |
| 39 | forced, |
| 40 | }: { |
| 41 | issueId: string |
| 42 | actorId: string |
| 43 | commits: { |
| 44 | forced?: { |
| 45 | }, |
| 46 | ) { |
| 47 | const rows = |
| 48 | ...(forced |
| 49 | ? [ |
| 50 | { |
| 51 | type: 'head_force_pushed' as |
| 52 | beforeSha: forced.before |
| 53 | commitSha: forced.after |
| 54 | }, |
| 55 | ] |
| 56 | : []), |
| 57 | ...commits.map( |
| 58 | type: 'committed' as |
| 59 | commitSha: commit.sha |
| 60 | commitMessage: commit.subject |
| 61 | commitAuthorName: commit.authorName |
| 62 | })), |
| 63 | ]; |
| 64 | if (rows.length |
| 65 | |
| 66 | await db.insert |
| 67 | rows.map((row |
| 68 | ...row, |
| 69 | issueId, |
| 70 | actorId, |
| 71 | createdAt: sql`now() + ${ |
| 72 | })), |
| 73 | ); |
| 74 | } |