| 1 | import type { Database } from '@ghost/db'; |
| 2 | import { Inject, Injectable |
| 3 | |
| 4 | import { DATABASE } |
| 5 | import { orNull } |
| 6 | import { |
| 7 | CouldNotResolveError, |
| 8 | GithubForbiddenError, |
| 9 | } from '../../../../lib/github/github.errors.js'; |
| 10 | import type { Loaders |
| 11 | import { |
| 12 | decodeNodeIdAs, |
| 13 | type NodeType, |
| 14 | } from '../../../../lib/github/node-id.js'; |
| 15 | import { |
| 16 | issueRefOf, |
| 17 | labelNamesOf, |
| 18 | usernamesOf, |
| 19 | } from '../../../../lib/github/node-lookup.js'; |
| 20 | import { toIssueCommentNode } |
| 21 | import { RepositoryForbiddenError } |
| 22 | import { IssuesService } |
| 23 | import type { GithubViewer |
| 24 | import { requireWriteScope } |
| 25 | import * as M |
| 26 | import type { RepositoryNode |
| 27 | import { IssueNodesService } |
| 28 | import { RepositoryNodesService } |
| 29 | |
| 30 | /** Comments, labels and assignees go on pull requests as on issues. */ |
| 31 | const SUBJECTS: NodeType |
| 32 | |
| 33 | export type IssueRef |
| 34 | username: string |
| 35 | repo: string |
| 36 | number: number |
| 37 | requesterId: string |
| 38 | }; |
| 39 | |
| 40 | @Injectable() |
| 41 | export class IssueMutationsService |
| 42 | constructor( |
| 43 | @Inject(DATABASE) |
| 44 | private readonly issues |
| 45 | private readonly repositories |
| 46 | private readonly issueNodes |
| 47 | ) {} |
| 48 | |
| 49 | async createIssue( |
| 50 | input: M |
| 51 | viewer: GithubViewer |
| 52 | loaders: Loaders |
| 53 | ) { |
| 54 | const signedIn = |
| 55 | this.refuseMilestonesAndProjects(input |
| 56 | const repository = |
| 57 | requireWriteScope(signedIn, ' |
| 58 | const labels = |
| 59 | const assignees = |
| 60 | const created = |
| 61 | 'CreateIssue', |
| 62 | signedIn.userId, |
| 63 | loaders, |
| 64 | () => |
| 65 | this.issues.createIssue |
| 66 | username: repository.ownerLogin |
| 67 | repo: repository.slug |
| 68 | requesterId: signedIn.userId |
| 69 | body: { |
| 70 | title: input.title |
| 71 | body: input.body |
| 72 | labels, |
| 73 | assignees, |
| 74 | }, |
| 75 | }), |
| 76 | ); |
| 77 | return { |
| 78 | clientMutationId: input.clientMutationId |
| 79 | issue: await this |
| 80 | repository, |
| 81 | created.number, |
| 82 | signedIn, |
| 83 | ), |
| 84 | }; |
| 85 | } |
| 86 | |
| 87 | async updateIssue( |
| 88 | input: M |
| 89 | viewer: GithubViewer |
| 90 | loaders: Loaders |
| 91 | ) { |
| 92 | const signedIn = |
| 93 | this.refuseMilestonesAndProjects(input |
| 94 | const { repository, |
| 95 | requireWriteScope(signedIn, ' |
| 96 | const labels = |
| 97 | ? await this. |
| 98 | : null; |
| 99 | const assignees = |
| 100 | ? await this. |
| 101 | : null; |
| 102 | const current = |
| 103 | // ponytail: one transaction per changed aspect; a failure midway keeps the earlier changes. Add IssuesService.updateMany if gh users hit it. |
| 104 | await this.write |
| 105 | if (input.title |
| 106 | await this.issues |
| 107 | ...ref, |
| 108 | body: { title: input |
| 109 | }); |
| 110 | // Setting the state an issue already has is a no-op on GitHub; Ghost refuses it. |
| 111 | if (input.state |
| 112 | await this.issues |
| 113 | if (input.state |
| 114 | await this.issues |
| 115 | if (labels) await |
| 116 | if (assignees) |
| 117 | await this.issues |
| 118 | }); |
| 119 | return { |
| 120 | clientMutationId: input.clientMutationId |
| 121 | issue: await this |
| 122 | repository, |
| 123 | ref.number, |
| 124 | signedIn, |
| 125 | ), |
| 126 | }; |
| 127 | } |
| 128 | |
| 129 | async closeIssue( |
| 130 | input: M |
| 131 | viewer: GithubViewer |
| 132 | loaders: Loaders |
| 133 | ) { |
| 134 | const signedIn = |
| 135 | const { repository, |
| 136 | requireWriteScope(signedIn, ' |
| 137 | await this.write |
| 138 | this.issues.closeIssue |
| 139 | ); |
| 140 | return { |
| 141 | clientMutationId: input.clientMutationId |
| 142 | issue: await this |
| 143 | repository, |
| 144 | ref.number, |
| 145 | signedIn, |
| 146 | ), |
| 147 | }; |
| 148 | } |
| 149 | |
| 150 | async reopenIssue( |
| 151 | input: M |
| 152 | viewer: GithubViewer |
| 153 | loaders: Loaders |
| 154 | ) { |
| 155 | const signedIn = |
| 156 | const { repository, |
| 157 | requireWriteScope(signedIn, ' |
| 158 | await this.write |
| 159 | this.issues.reopenIssue |
| 160 | ); |
| 161 | return { |
| 162 | clientMutationId: input.clientMutationId |
| 163 | issue: await this |
| 164 | repository, |
| 165 | ref.number, |
| 166 | signedIn, |
| 167 | ), |
| 168 | }; |
| 169 | } |
| 170 | |
| 171 | async addComment( |
| 172 | input: M |
| 173 | viewer: GithubViewer |
| 174 | loaders: Loaders |
| 175 | ) { |
| 176 | const signedIn = |
| 177 | const { repository, |
| 178 | input.subjectId, |
| 179 | signedIn, |
| 180 | SUBJECTS, |
| 181 | ); |
| 182 | requireWriteScope(signedIn, ' |
| 183 | const comment = |
| 184 | 'AddComment', |
| 185 | signedIn.userId, |
| 186 | loaders, |
| 187 | () => this. |
| 188 | ); |
| 189 | const issue = |
| 190 | return { |
| 191 | clientMutationId: input.clientMutationId |
| 192 | commentEdge: { |
| 193 | cursor: '', |
| 194 | node: toIssueCommentNode(comment |
| 195 | }, |
| 196 | }; |
| 197 | } |
| 198 | |
| 199 | async addLabelsToLabelable( |
| 200 | input: M |
| 201 | viewer: GithubViewer |
| 202 | loaders: Loaders |
| 203 | ) { |
| 204 | const signedIn = |
| 205 | const { repository, |
| 206 | input.labelableId, |
| 207 | signedIn, |
| 208 | SUBJECTS, |
| 209 | ); |
| 210 | requireWriteScope(signedIn, ' |
| 211 | const current = |
| 212 | await this.subject |
| 213 | ).labelDtos.map( |
| 214 | const added = |
| 215 | await this.write |
| 216 | this.issues.setIssueLabels |
| 217 | ...ref, |
| 218 | names: [...new |
| 219 | }), |
| 220 | ); |
| 221 | return { |
| 222 | clientMutationId: input.clientMutationId |
| 223 | labelable: await this |
| 224 | }; |
| 225 | } |
| 226 | |
| 227 | async removeLabelsFromLabelable( |
| 228 | input: M |
| 229 | viewer: GithubViewer |
| 230 | loaders: Loaders |
| 231 | ) { |
| 232 | const signedIn = |
| 233 | const { repository, |
| 234 | input.labelableId, |
| 235 | signedIn, |
| 236 | SUBJECTS, |
| 237 | ); |
| 238 | requireWriteScope(signedIn, ' |
| 239 | const current = |
| 240 | await this.subject |
| 241 | ).labelDtos.map( |
| 242 | const removed = |
| 243 | await this.write |
| 244 | 'RemoveLabelsFromLabelable', |
| 245 | signedIn.userId, |
| 246 | loaders, |
| 247 | () => |
| 248 | this.issues.setIssueLabels |
| 249 | ...ref, |
| 250 | names: current.filter |
| 251 | }), |
| 252 | ); |
| 253 | return { |
| 254 | clientMutationId: input.clientMutationId |
| 255 | labelable: await this |
| 256 | }; |
| 257 | } |
| 258 | |
| 259 | async addAssigneesToAssignable( |
| 260 | input: M |
| 261 | viewer: GithubViewer |
| 262 | loaders: Loaders |
| 263 | ) { |
| 264 | const signedIn = |
| 265 | const { repository, |
| 266 | input.assignableId, |
| 267 | signedIn, |
| 268 | SUBJECTS, |
| 269 | ); |
| 270 | requireWriteScope(signedIn, ' |
| 271 | const current = |
| 272 | .assigneeLogins; |
| 273 | const added = |
| 274 | await this.write |
| 275 | this.issues.setIssueAssignees |
| 276 | ...ref, |
| 277 | usernames: [...new |
| 278 | }), |
| 279 | ); |
| 280 | return { |
| 281 | clientMutationId: input.clientMutationId |
| 282 | assignable: await this |
| 283 | }; |
| 284 | } |
| 285 | |
| 286 | async removeAssigneesFromAssignable( |
| 287 | input: M |
| 288 | viewer: GithubViewer |
| 289 | loaders: Loaders |
| 290 | ) { |
| 291 | const signedIn = |
| 292 | const { repository, |
| 293 | input.assignableId, |
| 294 | signedIn, |
| 295 | SUBJECTS, |
| 296 | ); |
| 297 | requireWriteScope(signedIn, ' |
| 298 | const current = |
| 299 | .assigneeLogins; |
| 300 | const removed = |
| 301 | await this.write |
| 302 | 'RemoveAssigneesFromAssignable', |
| 303 | signedIn.userId, |
| 304 | loaders, |
| 305 | () => |
| 306 | this.issues.setIssueAssignees |
| 307 | ...ref, |
| 308 | usernames: current.filter |
| 309 | }), |
| 310 | ); |
| 311 | return { |
| 312 | clientMutationId: input.clientMutationId |
| 313 | assignable: await this |
| 314 | }; |
| 315 | } |
| 316 | |
| 317 | require(viewer: |
| 318 | if (!viewer) |
| 319 | throw new GithubForbiddenError( |
| 320 | `You must be signed in to run ${mutation}.` |
| 321 | ); |
| 322 | return viewer; |
| 323 | } |
| 324 | |
| 325 | /** Ghost has no milestones or projects, so any id gh sends for one cannot resolve. */ |
| 326 | refuseMilestonesAndProjects({ |
| 327 | milestoneId, |
| 328 | projectIds, |
| 329 | }: { |
| 330 | milestoneId?: string |
| 331 | projectIds?: string |
| 332 | }) { |
| 333 | const unknown = |
| 334 | if (unknown) throw |
| 335 | } |
| 336 | |
| 337 | unresolved(nodeId: |
| 338 | return new CouldNotResolveError |
| 339 | `Could not resolve to a node with the global id of '${nodeId}'` |
| 340 | ); |
| 341 | } |
| 342 | |
| 343 | async repositoryFor(nodeId |
| 344 | const repositoryId = |
| 345 | const repository = |
| 346 | this.repositories.load |
| 347 | ); |
| 348 | if (!repository) |
| 349 | return repository; |
| 350 | } |
| 351 | |
| 352 | /** The issue a node id names, with the IssueRef the service methods take; `types` widens it to pull requests for Labelable, Assignable and comment subjects. */ |
| 353 | async issueRef( |
| 354 | nodeId: string, |
| 355 | viewer: GithubViewer |
| 356 | types: NodeType |
| 357 | ): Promise<{ |
| 358 | const found = |
| 359 | if (!found) |
| 360 | const repository = |
| 361 | this.repositories.load |
| 362 | ); |
| 363 | if (!repository) |
| 364 | return { |
| 365 | repository, |
| 366 | ref: { |
| 367 | username: repository.ownerLogin |
| 368 | repo: repository.slug |
| 369 | number: found.number |
| 370 | requesterId: viewer.userId |
| 371 | }, |
| 372 | }; |
| 373 | } |
| 374 | |
| 375 | /** The node a mutation answers with, which also carries the labels and assignees it read. */ |
| 376 | private subject( |
| 377 | repository: RepositoryNode |
| 378 | ref: IssueRef |
| 379 | viewer: GithubViewer |
| 380 | ) { |
| 381 | return this.issueNodes |
| 382 | } |
| 383 | |
| 384 | async labelNames(repository |
| 385 | const ids = |
| 386 | return this.resolveAll |
| 387 | nodeIds, |
| 388 | ids, |
| 389 | await labelNamesOf(this |
| 390 | ); |
| 391 | } |
| 392 | |
| 393 | async usernames(nodeIds |
| 394 | const ids = |
| 395 | return this.resolveAll |
| 396 | } |
| 397 | |
| 398 | /** The value for each id in order; the first id with none fails as GitHub fails an unresolvable node. */ |
| 399 | private resolveAll( |
| 400 | nodeIds: string[] |
| 401 | ids: string[] |
| 402 | found: Map |
| 403 | ) { |
| 404 | return ids.map |
| 405 | const value = |
| 406 | if (value === |
| 407 | return value; |
| 408 | }); |
| 409 | } |
| 410 | |
| 411 | /** Runs a write; a permission refusal reads as GitHub's FORBIDDEN wording, which names the login. */ |
| 412 | async write<T |
| 413 | mutation: string, |
| 414 | userId: string, |
| 415 | loaders: Loaders |
| 416 | run: () |
| 417 | ) { |
| 418 | try { |
| 419 | return await run |
| 420 | } catch (error) |
| 421 | if (!(error |
| 422 | const login = |
| 423 | throw new GithubForbiddenError( |
| 424 | `${login} does not have the correct permissions to execute \`${ |
| 425 | ); |
| 426 | } |
| 427 | } |
| 428 | } |