| 1 | import { type Database, schema } from '@ghost/db'; |
| 2 | import { Inject, Injectable |
| 3 | import { and, eq |
| 4 | |
| 5 | import { DATABASE } |
| 6 | import { MAX_PUSH_COMMITS, publishEvent |
| 7 | import { isAncestor } |
| 8 | import { |
| 9 | BRANCH_PREFIX, |
| 10 | resolveCommit, |
| 11 | } from '../../lib/git/tree/resolve-ref.js'; |
| 12 | import { type RefTransition, |
| 13 | import { |
| 14 | commitsAdded, |
| 15 | recordCommitEvents, |
| 16 | } from '../../lib/pull-requests/commit-events.js'; |
| 17 | import { RepositoryMaterializerService } |
| 18 | import { errorMessage } |
| 19 | |
| 20 | /** Puts what a push did to an open pull request's head into its timeline. Every writer of a branch calls it once the push has landed: git pushes, applied suggestions, and merges into a branch another request is from. */ |
| 21 | @Injectable() |
| 22 | export class PullRequestPushesService |
| 23 | private readonly logger |
| 24 | |
| 25 | constructor( |
| 26 | @Inject(DATABASE) |
| 27 | private readonly materializer |
| 28 | ) {} |
| 29 | |
| 30 | /** Never rejects: the push it follows has already landed, and a missing timeline entry must not fail it. */ |
| 31 | async recordPush(push |
| 32 | repositoryId: string |
| 33 | transitions: RefTransition |
| 34 | pushedBy: string |
| 35 | }) { |
| 36 | await this.record |
| 37 | this.logger.warn |
| 38 | `Pull request commits were not recorded for ${push.repositoryId |
| 39 | ), |
| 40 | ); |
| 41 | } |
| 42 | |
| 43 | private async record |
| 44 | repositoryId, |
| 45 | transitions, |
| 46 | pushedBy, |
| 47 | }: { |
| 48 | repositoryId: string |
| 49 | transitions: RefTransition |
| 50 | pushedBy: string |
| 51 | }) { |
| 52 | // a deleted branch adds nothing; its request shows the head as gone |
| 53 | const moved = |
| 54 | transitions |
| 55 | .filter( |
| 56 | ({ ref, newOid |
| 57 | ref.startsWith(BRANCH_PREFIX |
| 58 | ) |
| 59 | .map(({ ref |
| 60 | ref.slice(BRANCH_PREFIX |
| 61 | { |
| 62 | before: oldOid.equals |
| 63 | after: newOid.toString |
| 64 | }, |
| 65 | ]), |
| 66 | ); |
| 67 | if (moved.size |
| 68 | |
| 69 | const affected = |
| 70 | .select({ |
| 71 | issueId: schema.pullRequest |
| 72 | baseRepositoryId: schema.pullRequest |
| 73 | baseRef: schema.pullRequest |
| 74 | headRef: schema.pullRequest |
| 75 | }) |
| 76 | .from(schema.pullRequest |
| 77 | .where( |
| 78 | and( |
| 79 | eq(schema.pullRequest |
| 80 | eq(schema.pullRequest |
| 81 | inArray(schema.pullRequest |
| 82 | ), |
| 83 | ); |
| 84 | if (affected.length |
| 85 | |
| 86 | const repositories = |
| 87 | .select({ |
| 88 | id: schema.repository |
| 89 | defaultBranch: schema.repository |
| 90 | }) |
| 91 | .from(schema.repository |
| 92 | .where( |
| 93 | inArray(schema.repository |
| 94 | repositoryId, |
| 95 | ...affected.map( |
| 96 | ]), |
| 97 | ); |
| 98 | const directoryOf = |
| 99 | const repository = |
| 100 | if (!repository) |
| 101 | return this.materializer |
| 102 | }; |
| 103 | const headDirectory = |
| 104 | |
| 105 | for (const pull |
| 106 | const { before, |
| 107 | const baseDirectory = |
| 108 | const alternates = |
| 109 | pull.baseRepositoryId === |
| 110 | const baseSha = |
| 111 | baseDirectory, |
| 112 | `${BRANCH_PREFIX}${pull. |
| 113 | ); |
| 114 | const forced = |
| 115 | before !== null && |
| 116 | // a rewritten head's old tip may be gone, and git refuses to exclude an object it cannot find |
| 117 | const previous = |
| 118 | |
| 119 | const commits = |
| 120 | gitDir: baseDirectory, |
| 121 | alternates, |
| 122 | tip: after, |
| 123 | exclude: [baseSha, |
| 124 | }); |
| 125 | await this.db |
| 126 | await recordCommitEvents(tx |
| 127 | issueId: pull.issueId |
| 128 | actorId: pushedBy, |
| 129 | commits, |
| 130 | forced: forced ? |
| 131 | }); |
| 132 | // webhooks only: nobody's inbox fills up with every push to a request they follow |
| 133 | await publishEvent(tx |
| 134 | type: 'pull_request.synchronized', |
| 135 | repositoryId: pull.baseRepositoryId |
| 136 | actorId: pushedBy, |
| 137 | payload: { |
| 138 | issueId: pull.issueId |
| 139 | before: before ?? |
| 140 | after, |
| 141 | forced, |
| 142 | commits: commits.toReversed |
| 143 | }, |
| 144 | }); |
| 145 | }); |
| 146 | } |
| 147 | } |
| 148 | } |