| 1 | import { Injectable } from '@nestjs/common'; |
| 2 | |
| 3 | import { UnprocessableError } |
| 4 | import type { Loaders |
| 5 | import { IssuesService } |
| 6 | import { PullRequestsService } |
| 7 | import type { GithubViewer |
| 8 | import { requireWriteScope } |
| 9 | import { PullRequestState, PullRequestUpdateState |
| 10 | import * as M |
| 11 | import { PullRequestNode } |
| 12 | import type { RepositoryNode |
| 13 | import { IssueNodesService } |
| 14 | import { |
| 15 | IssueMutationsService, |
| 16 | type IssueRef, |
| 17 | } from '../issue-mutations/issue-mutations.service.js'; |
| 18 | |
| 19 | @Injectable() |
| 20 | export class PullRequestMutationsService |
| 21 | constructor( |
| 22 | private readonly pulls |
| 23 | private readonly issues |
| 24 | private readonly mutations |
| 25 | private readonly issueNodes |
| 26 | ) {} |
| 27 | |
| 28 | async createPullRequest( |
| 29 | input: M |
| 30 | viewer: GithubViewer |
| 31 | loaders: Loaders |
| 32 | ) { |
| 33 | const signedIn = |
| 34 | const repository = |
| 35 | input.repositoryId, |
| 36 | signedIn, |
| 37 | ); |
| 38 | requireWriteScope(signedIn, ' |
| 39 | const head = |
| 40 | const created = |
| 41 | 'CreatePullRequest', |
| 42 | signedIn.userId, |
| 43 | loaders, |
| 44 | () => |
| 45 | this.pulls.createPullRequest |
| 46 | username: repository.ownerLogin |
| 47 | repo: repository.slug |
| 48 | requesterId: signedIn.userId |
| 49 | body: { |
| 50 | title: input.title |
| 51 | body: input.body |
| 52 | base: input.baseRefName |
| 53 | head, |
| 54 | draft: input.draft |
| 55 | }, |
| 56 | }), |
| 57 | ); |
| 58 | return { |
| 59 | clientMutationId: input.clientMutationId |
| 60 | pullRequest: await this |
| 61 | }; |
| 62 | } |
| 63 | |
| 64 | async updatePullRequest( |
| 65 | input: M |
| 66 | viewer: GithubViewer |
| 67 | loaders: Loaders |
| 68 | ) { |
| 69 | const signedIn = |
| 70 | this.mutations.refuseMilestonesAndProjects |
| 71 | const { repository, |
| 72 | input.pullRequestId, |
| 73 | signedIn, |
| 74 | ['PullRequest'], |
| 75 | ); |
| 76 | requireWriteScope(signedIn, ' |
| 77 | const current = |
| 78 | if (input.baseRefName |
| 79 | throw new UnprocessableError( |
| 80 | 'Ghost cannot change the base branch of a pull request', |
| 81 | ); |
| 82 | if ( |
| 83 | input.state === |
| 84 | current.state === |
| 85 | ) |
| 86 | throw new UnprocessableError( |
| 87 | const labels = |
| 88 | ? await this. |
| 89 | : null; |
| 90 | const assignees = |
| 91 | ? await this. |
| 92 | : null; |
| 93 | // ponytail: one transaction per changed aspect, as updateIssue does. |
| 94 | await this.mutations |
| 95 | 'UpdatePullRequest', |
| 96 | signedIn.userId, |
| 97 | loaders, |
| 98 | async () => |
| 99 | if (input.title |
| 100 | await this.pulls |
| 101 | ...ref, |
| 102 | body: { title: input |
| 103 | }); |
| 104 | // Setting the state a request already has is a no-op on GitHub; Ghost refuses it. |
| 105 | if ( |
| 106 | input.state === |
| 107 | current.state === |
| 108 | ) |
| 109 | await this.pulls |
| 110 | if ( |
| 111 | input.state === |
| 112 | current.state === |
| 113 | ) |
| 114 | await this.pulls |
| 115 | if (labels) await |
| 116 | if (assignees) |
| 117 | await this.issues |
| 118 | }, |
| 119 | ); |
| 120 | return { |
| 121 | clientMutationId: input.clientMutationId |
| 122 | pullRequest: await this |
| 123 | }; |
| 124 | } |
| 125 | |
| 126 | closePullRequest( |
| 127 | input: M |
| 128 | viewer: GithubViewer |
| 129 | loaders: Loaders |
| 130 | ) { |
| 131 | return this.change |
| 132 | this.pulls.closePullRequest |
| 133 | ); |
| 134 | } |
| 135 | |
| 136 | reopenPullRequest( |
| 137 | input: M |
| 138 | viewer: GithubViewer |
| 139 | loaders: Loaders |
| 140 | ) { |
| 141 | return this.change |
| 142 | this.pulls.reopenPullRequest |
| 143 | ); |
| 144 | } |
| 145 | |
| 146 | markPullRequestReadyForReview( |
| 147 | input: M |
| 148 | viewer: GithubViewer |
| 149 | loaders: Loaders |
| 150 | ) { |
| 151 | return this.change |
| 152 | 'MarkPullRequestReadyForReview', |
| 153 | input, |
| 154 | viewer, |
| 155 | loaders, |
| 156 | (ref) => |
| 157 | ); |
| 158 | } |
| 159 | |
| 160 | convertPullRequestToDraft( |
| 161 | input: M |
| 162 | viewer: GithubViewer |
| 163 | loaders: Loaders |
| 164 | ) { |
| 165 | return this.change |
| 166 | 'ConvertPullRequestToDraft', |
| 167 | input, |
| 168 | viewer, |
| 169 | loaders, |
| 170 | (ref) => |
| 171 | ); |
| 172 | } |
| 173 | |
| 174 | mergePullRequest( |
| 175 | input: M |
| 176 | viewer: GithubViewer |
| 177 | loaders: Loaders |
| 178 | ) { |
| 179 | return this.change |
| 180 | 'MergePullRequest', |
| 181 | input, |
| 182 | viewer, |
| 183 | loaders, |
| 184 | async (ref) |
| 185 | if (input.expectedHeadOid |
| 186 | // ponytail: checked before the merge, not under its lock; a push landing in between merges the newer head. |
| 187 | const { headSha |
| 188 | if (headSha !== |
| 189 | throw new UnprocessableError( |
| 190 | 'Head branch was modified. Review and try the merge again.', |
| 191 | ); |
| 192 | } |
| 193 | await this.pulls |
| 194 | ...ref, |
| 195 | method: input.mergeMethod |
| 196 | ? (input.mergeMethod |
| 197 | typeof input.mergeMethod |
| 198 | >) |
| 199 | : undefined, |
| 200 | title: input.commitHeadline |
| 201 | message: input.commitBody |
| 202 | }); |
| 203 | }, |
| 204 | ); |
| 205 | } |
| 206 | |
| 207 | /** A mutation that names one pull request, runs `run` on it, and answers with it as it then stands. */ |
| 208 | private async change |
| 209 | mutation: string, |
| 210 | input: { |
| 211 | viewer: GithubViewer |
| 212 | loaders: Loaders |
| 213 | run: (ref |
| 214 | ) { |
| 215 | const signedIn = |
| 216 | const { repository, |
| 217 | input.pullRequestId, |
| 218 | signedIn, |
| 219 | ['PullRequest'], |
| 220 | ); |
| 221 | const field = |
| 222 | requireWriteScope(signedIn, field |
| 223 | await this.mutations |
| 224 | run(ref), |
| 225 | ); |
| 226 | return { |
| 227 | clientMutationId: input.clientMutationId |
| 228 | pullRequest: await this |
| 229 | }; |
| 230 | } |
| 231 | |
| 232 | private async node |
| 233 | repository: RepositoryNode |
| 234 | number: number, |
| 235 | viewer: GithubViewer |
| 236 | ) { |
| 237 | const found = |
| 238 | repository, |
| 239 | number, |
| 240 | viewer, |
| 241 | ); |
| 242 | if (!(found |
| 243 | throw this.mutations. |
| 244 | return found; |
| 245 | } |
| 246 | |
| 247 | /** Ghost's `owner:branch`: gh writes it itself for a fork, or names the fork by `headRepositoryId`. */ |
| 248 | private async headSpec |
| 249 | input: M |
| 250 | repository: RepositoryNode |
| 251 | viewer: GithubViewer |
| 252 | ) { |
| 253 | if (input.headRefName |
| 254 | return input.headRefName |
| 255 | const head = |
| 256 | input.headRepositoryId, |
| 257 | viewer, |
| 258 | ); |
| 259 | return head.ghostId |
| 260 | ? input.headRefName |
| 261 | : `${head.ownerLogin |
| 262 | } |
| 263 | } |