| 1 | import { type Database, schema } from '@ghost/db'; |
| 2 | import { mkdtemp, rm |
| 3 | import { tmpdir } |
| 4 | import path from 'node:path |
| 5 | import { Inject, Injectable |
| 6 | import { and, eq |
| 7 | |
| 8 | import { DATABASE } |
| 9 | import { isTextBlob, readBlob |
| 10 | import { fileHunks, type |
| 11 | import { packRange } |
| 12 | import { fileBody } |
| 13 | import { replaceFile } |
| 14 | import type { Executor |
| 15 | import { diffHunkFor } |
| 16 | import { selectReviews } |
| 17 | import { |
| 18 | applySuggestion, |
| 19 | extractSuggestion, |
| 20 | } from '../../lib/pull-requests/suggestion.js'; |
| 21 | import { RepositoryAccessService } |
| 22 | import { PushTransactionService } |
| 23 | import { IssueReferencesService } |
| 24 | import { UsersService } |
| 25 | import { isoTimestamp } |
| 26 | import type { |
| 27 | CreateReviewRequestDTO, |
| 28 | ReviewCommentRequestDTO, |
| 29 | } from './dto/pull-request-review.dto.js'; |
| 30 | import { |
| 31 | EmptyReviewError, |
| 32 | InvalidLineRangeError, |
| 33 | OwnPullRequestReviewError, |
| 34 | PendingReviewReplyError, |
| 35 | PullRequestNotOpenError, |
| 36 | ReviewCommentNotFoundError, |
| 37 | ReviewLineNotInDiffError, |
| 38 | ReviewNotDismissableError, |
| 39 | ReviewNotFoundError, |
| 40 | SuggestionNotApplicableError, |
| 41 | SuggestionOutdatedError, |
| 42 | } from './pull-requests.errors.js'; |
| 43 | import { |
| 44 | type PullRequestRef, |
| 45 | PullRequestsService, |
| 46 | } from './pull-requests.service.js'; |
| 47 | |
| 48 | type Opened |
| 49 | type Loaded |
| 50 | |
| 51 | const replyColumns = { |
| 52 | id: schema.pullRequestReviewComment |
| 53 | body: schema.pullRequestReviewComment |
| 54 | createdAt: isoTimestamp(schema |
| 55 | updatedAt: isoTimestamp(schema |
| 56 | }; |
| 57 | |
| 58 | /** Reviews, their line comments and the replies under them. Anyone who can read a request may review it and reply, the same bar as commenting; editing someone else's words takes `write`. */ |
| 59 | @Injectable() |
| 60 | export class ReviewsService |
| 61 | constructor( |
| 62 | @Inject(DATABASE) |
| 63 | private readonly access |
| 64 | private readonly references |
| 65 | private readonly pullRequests |
| 66 | private readonly users |
| 67 | private readonly pushTransaction |
| 68 | ) {} |
| 69 | |
| 70 | /** Submits the requester's pending review, or a new one, together with any further line comments. */ |
| 71 | async submitReview( |
| 72 | params: PullRequestRef |
| 73 | requesterId: string |
| 74 | body: CreateReviewRequestDTO |
| 75 | }, |
| 76 | ) { |
| 77 | const git = |
| 78 | const { pullRequest, |
| 79 | const { state, |
| 80 | const body = |
| 81 | if (state !== |
| 82 | throw new OwnPullRequestReviewError() |
| 83 | } |
| 84 | const located = |
| 85 | |
| 86 | const [pending] |
| 87 | .select({ |
| 88 | id: schema.pullRequestReview |
| 89 | comments: sql<number |
| 90 | }) |
| 91 | .from(schema.pullRequestReview |
| 92 | .where(pendingOf(pullRequest |
| 93 | if ( |
| 94 | state === 'commented' |
| 95 | !body && |
| 96 | comments.length === 0 |
| 97 | !pending?.comments |
| 98 | ) { |
| 99 | throw new EmptyReviewError() |
| 100 | } |
| 101 | |
| 102 | const reviewId = |
| 103 | const [review] |
| 104 | ? await tx |
| 105 | .update(schema.pullRequestReview |
| 106 | .set({ |
| 107 | state, |
| 108 | body, |
| 109 | commitSha: git.headSha |
| 110 | submittedAt: new Date |
| 111 | }) |
| 112 | .where(eq(schema |
| 113 | .returning({ id: |
| 114 | : await tx |
| 115 | .insert(schema.pullRequestReview |
| 116 | .values({ |
| 117 | pullRequestId: pullRequest.id |
| 118 | authorId: params.requesterId |
| 119 | state, |
| 120 | body, |
| 121 | commitSha: git.headSha |
| 122 | submittedAt: new Date |
| 123 | }) |
| 124 | .returning({ id: |
| 125 | if (!review) |
| 126 | |
| 127 | if (located.length |
| 128 | await tx.insert |
| 129 | located.map((comment |
| 130 | ...comment, |
| 131 | pullRequestId: pullRequest.id |
| 132 | reviewId: review.id |
| 133 | authorId: params.requesterId |
| 134 | commitSha: git.headSha |
| 135 | })), |
| 136 | ); |
| 137 | } |
| 138 | |
| 139 | // Pending comments were invisible, so what they mention is recorded only now. |
| 140 | const source = |
| 141 | repository: base, |
| 142 | issueId: pullRequest.issueId |
| 143 | actorId: params.requesterId |
| 144 | }; |
| 145 | await this.references |
| 146 | tx, |
| 147 | { type: 'comment', |
| 148 | body, |
| 149 | ); |
| 150 | const written = |
| 151 | .select({ |
| 152 | id: schema.pullRequestReviewComment |
| 153 | body: schema.pullRequestReviewComment |
| 154 | }) |
| 155 | .from(schema.pullRequestReviewComment |
| 156 | .where(eq(schema |
| 157 | for (const comment |
| 158 | await this.references |
| 159 | tx, |
| 160 | { type: 'comment', |
| 161 | comment.body, |
| 162 | ); |
| 163 | } |
| 164 | |
| 165 | await touch(tx |
| 166 | return review.id |
| 167 | }); |
| 168 | |
| 169 | return this.findReview |
| 170 | } |
| 171 | |
| 172 | /** Adds a line comment to the requester's pending review, starting one on the current head if there is none. */ |
| 173 | async addPendingComment( |
| 174 | params: PullRequestRef |
| 175 | requesterId: string |
| 176 | body: ReviewCommentRequestDTO |
| 177 | }, |
| 178 | ) { |
| 179 | const git = |
| 180 | const [located] |
| 181 | |
| 182 | await this.db |
| 183 | await tx |
| 184 | .insert(schema.pullRequestReview |
| 185 | .values({ |
| 186 | pullRequestId: git.pullRequest |
| 187 | authorId: params.requesterId |
| 188 | commitSha: git.headSha |
| 189 | }) |
| 190 | .onConflictDoNothing(); |
| 191 | const [pending] |
| 192 | .select({ id: |
| 193 | .from(schema.pullRequestReview |
| 194 | .where(pendingOf(git |
| 195 | if (!pending) |
| 196 | |
| 197 | await tx.insert |
| 198 | ...located, |
| 199 | pullRequestId: git.pullRequest |
| 200 | reviewId: pending.id |
| 201 | authorId: params.requesterId |
| 202 | commitSha: git.headSha |
| 203 | }); |
| 204 | }); |
| 205 | |
| 206 | return this.getPendingReview |
| 207 | } |
| 208 | |
| 209 | async getPendingReview(params |
| 210 | const { pullRequest |
| 211 | const [review] |
| 212 | this.db, |
| 213 | pendingOf(pullRequest.id |
| 214 | ); |
| 215 | return { review: |
| 216 | } |
| 217 | |
| 218 | async discardPendingReview(params |
| 219 | const { pullRequest |
| 220 | await this.db |
| 221 | .delete(schema.pullRequestReview |
| 222 | .where(pendingOf(pullRequest |
| 223 | return { deleted: |
| 224 | } |
| 225 | |
| 226 | async updateReview( |
| 227 | params: PullRequestRef |
| 228 | requesterId: string |
| 229 | reviewId: string |
| 230 | body: string |
| 231 | }, |
| 232 | ) { |
| 233 | const loaded = |
| 234 | const review = |
| 235 | if (review.authorId |
| 236 | await this.authorizeWrite |
| 237 | } |
| 238 | |
| 239 | const body = |
| 240 | await this.db |
| 241 | await tx |
| 242 | .update(schema.pullRequestReview |
| 243 | .set({ body |
| 244 | .where(eq(schema |
| 245 | await this.references |
| 246 | tx, |
| 247 | { |
| 248 | type: 'comment', |
| 249 | id: review.id |
| 250 | repository: loaded.base |
| 251 | issueId: loaded.pullRequest |
| 252 | actorId: review.authorId |
| 253 | }, |
| 254 | body, |
| 255 | ); |
| 256 | await touch(tx |
| 257 | }); |
| 258 | return this.findReview |
| 259 | } |
| 260 | |
| 261 | async dismissReview( |
| 262 | params: PullRequestRef |
| 263 | requesterId: string |
| 264 | reviewId: string |
| 265 | message: string |
| 266 | }, |
| 267 | ) { |
| 268 | const loaded = |
| 269 | ...params, |
| 270 | operation: 'write', |
| 271 | }); |
| 272 | const review = |
| 273 | if (review.state |
| 274 | throw new ReviewNotDismissableError() |
| 275 | } |
| 276 | |
| 277 | await this.db |
| 278 | .update(schema.pullRequestReview |
| 279 | .set({ |
| 280 | dismissedById: params.requesterId |
| 281 | dismissalMessage: params.message |
| 282 | }) |
| 283 | .where(eq(schema |
| 284 | return this.findReview |
| 285 | } |
| 286 | |
| 287 | /** Replies join the thread of the comment replied to, even when that comment is itself a reply. */ |
| 288 | async replyToComment( |
| 289 | params: PullRequestRef |
| 290 | requesterId: string |
| 291 | commentId: string |
| 292 | body: string |
| 293 | }, |
| 294 | ) { |
| 295 | const loaded = |
| 296 | const comment = |
| 297 | if (comment.pending |
| 298 | |
| 299 | const reply = |
| 300 | const [row] |
| 301 | .insert(schema.pullRequestReviewComment |
| 302 | .values({ |
| 303 | pullRequestId: loaded.pullRequest |
| 304 | inReplyToId: comment.inReplyToId |
| 305 | authorId: params.requesterId |
| 306 | path: comment.path |
| 307 | side: comment.side |
| 308 | line: comment.line |
| 309 | startSide: comment.startSide |
| 310 | startLine: comment.startLine |
| 311 | commitSha: comment.commitSha |
| 312 | body: params.body |
| 313 | }) |
| 314 | .returning(replyColumns); |
| 315 | if (!row) |
| 316 | |
| 317 | await this.references |
| 318 | tx, |
| 319 | { |
| 320 | type: 'comment', |
| 321 | id: row.id |
| 322 | repository: loaded.base |
| 323 | issueId: loaded.pullRequest |
| 324 | actorId: params.requesterId |
| 325 | }, |
| 326 | params.body, |
| 327 | ); |
| 328 | await touch(tx |
| 329 | return row; |
| 330 | }); |
| 331 | |
| 332 | const [author] |
| 333 | .select({ |
| 334 | username: sql<string |
| 335 | image: schema.user |
| 336 | }) |
| 337 | .from(schema.user |
| 338 | .where(eq(schema |
| 339 | return { |
| 340 | ...reply, |
| 341 | authorUsername: author?.username |
| 342 | authorImage: author?.image |
| 343 | }; |
| 344 | } |
| 345 | |
| 346 | async updateComment( |
| 347 | params: PullRequestRef |
| 348 | requesterId: string |
| 349 | commentId: string |
| 350 | body: string |
| 351 | }, |
| 352 | ) { |
| 353 | const loaded = |
| 354 | const comment = |
| 355 | if (comment.authorId |
| 356 | await this.authorizeWrite |
| 357 | } |
| 358 | |
| 359 | const updated = |
| 360 | const [row] |
| 361 | .update(schema.pullRequestReviewComment |
| 362 | .set({ body: |
| 363 | .where(eq(schema |
| 364 | .returning(replyColumns); |
| 365 | if (!row) |
| 366 | |
| 367 | // A pending comment's mentions are recorded when its review is submitted. |
| 368 | if (!comment.pending |
| 369 | await this.references |
| 370 | tx, |
| 371 | { |
| 372 | type: 'comment', |
| 373 | id: comment.id |
| 374 | repository: loaded.base |
| 375 | issueId: loaded.pullRequest |
| 376 | actorId: comment.authorId |
| 377 | }, |
| 378 | params.body, |
| 379 | ); |
| 380 | } |
| 381 | return row; |
| 382 | }); |
| 383 | return { |
| 384 | ...updated, |
| 385 | authorUsername: comment.authorUsername |
| 386 | authorImage: comment.authorImage |
| 387 | }; |
| 388 | } |
| 389 | |
| 390 | /** Deleting a thread's first comment deletes its replies. A comment-only review left with nothing to say goes with it. */ |
| 391 | async deleteComment( |
| 392 | params: PullRequestRef |
| 393 | ) { |
| 394 | const loaded = |
| 395 | const comment = |
| 396 | if (comment.authorId |
| 397 | await this.authorizeWrite |
| 398 | } |
| 399 | |
| 400 | await this.db |
| 401 | const replies = |
| 402 | .select({ id: |
| 403 | .from(schema.pullRequestReviewComment |
| 404 | .where(eq(schema |
| 405 | for (const { |
| 406 | await this.references |
| 407 | } |
| 408 | |
| 409 | await tx |
| 410 | .delete(schema.pullRequestReviewComment |
| 411 | .where(eq(schema |
| 412 | if (comment.reviewId |
| 413 | await tx |
| 414 | .delete(schema.pullRequestReview |
| 415 | .where( |
| 416 | and( |
| 417 | eq(schema.pullRequestReview |
| 418 | eq(schema.pullRequestReview |
| 419 | sql`${schema.pullRequestReview |
| 420 | notExists( |
| 421 | tx |
| 422 | .select({ id: |
| 423 | .from(schema.pullRequestReviewComment |
| 424 | .where( |
| 425 | eq( |
| 426 | schema.pullRequestReviewComment.reviewId |
| 427 | comment.reviewId, |
| 428 | ), |
| 429 | ), |
| 430 | ), |
| 431 | ), |
| 432 | ); |
| 433 | } |
| 434 | await touch(tx |
| 435 | }); |
| 436 | return { deleted: |
| 437 | } |
| 438 | |
| 439 | /** Commits a comment's suggestion to the head branch in place of the lines it was made on, through the head's own commit point. Takes `write` on the head repository, which for a fork is the fork. */ |
| 440 | async applySuggestion( |
| 441 | params: PullRequestRef |
| 442 | ) { |
| 443 | const loaded = |
| 444 | const { pullRequest, |
| 445 | if (pullRequest.state |
| 446 | throw new PullRequestNotOpenError( |
| 447 | } |
| 448 | const comment = |
| 449 | const suggestion = |
| 450 | if (comment.pending |
| 451 | throw new SuggestionNotApplicableError( |
| 452 | 'This comment has no submitted suggestion', |
| 453 | ); |
| 454 | } |
| 455 | if ( |
| 456 | comment.side !== |
| 457 | (comment.startSide ?? |
| 458 | ) { |
| 459 | throw new SuggestionNotApplicableError( |
| 460 | 'Only lines on the head side of the diff can be changed', |
| 461 | ); |
| 462 | } |
| 463 | |
| 464 | // An open request always has its head: a repository heading one cannot be deleted. |
| 465 | const headRepositoryId = |
| 466 | await this.access |
| 467 | repositoryId: headRepositoryId, |
| 468 | actor: { userId: params |
| 469 | operation: 'write', |
| 470 | }); |
| 471 | const git = |
| 472 | pullRequest, |
| 473 | base, |
| 474 | headRepositoryId, |
| 475 | ); |
| 476 | if (comment.commitSha |
| 477 | |
| 478 | const blob = |
| 479 | gitDir: git.headDirectory |
| 480 | ref: git.headSha |
| 481 | path: comment.path |
| 482 | }); |
| 483 | const content = |
| 484 | if (!isTextBlob(content |
| 485 | throw new SuggestionNotApplicableError( |
| 486 | 'Only a text file under 1 MB can take a suggestion', |
| 487 | ); |
| 488 | } |
| 489 | |
| 490 | const author = |
| 491 | const commitSha = |
| 492 | gitDir: git.headDirectory |
| 493 | parent: git.headSha |
| 494 | file: comment.path |
| 495 | content: Buffer.from |
| 496 | applySuggestion( |
| 497 | content.toString('utf8 |
| 498 | comment.startLine ?? |
| 499 | comment.line, |
| 500 | suggestion, |
| 501 | ), |
| 502 | ), |
| 503 | message: `Apply suggestion from ${comment |
| 504 | author: { name: author |
| 505 | }); |
| 506 | |
| 507 | const directory = |
| 508 | try { |
| 509 | const pack = |
| 510 | gitDir: git.headDirectory |
| 511 | include: [commitSha], |
| 512 | exclude: [git.headSha |
| 513 | prefix: path.join |
| 514 | }); |
| 515 | await this.pushTransaction |
| 516 | repoId: headRepositoryId, |
| 517 | transitions: [ |
| 518 | { |
| 519 | ref: `refs/heads/${pullRequest |
| 520 | oldOid: Buffer.from |
| 521 | newOid: Buffer.from |
| 522 | }, |
| 523 | ], |
| 524 | body: fileBody(pack |
| 525 | packOffset: 0, |
| 526 | pushedBy: params.requesterId |
| 527 | }); |
| 528 | } finally { |
| 529 | await rm(directory |
| 530 | } |
| 531 | |
| 532 | await this.db |
| 533 | .update(schema.repository |
| 534 | .set({ lastPushedAt: |
| 535 | .where(eq(schema |
| 536 | return { commitSha |
| 537 | } |
| 538 | |
| 539 | private async openForReview |
| 540 | const git = |
| 541 | if (git.pullRequest |
| 542 | throw new PullRequestNotOpenError( |
| 543 | } |
| 544 | return git; |
| 545 | } |
| 546 | |
| 547 | /** Every comment must sit on a line the diff shows, on the side it names; each comes back with the rows of the diff it points at. */ |
| 548 | private async locateInDiff |
| 549 | const byPath = |
| 550 | const located = |
| 551 | for (const comment |
| 552 | if (!byPath.has |
| 553 | byPath.set( |
| 554 | comment.path, |
| 555 | await fileHunks({ |
| 556 | gitDir: git.baseDirectory |
| 557 | alternates: git.alternates |
| 558 | from: git.mergeBase |
| 559 | to: git.headSha |
| 560 | path: comment.path |
| 561 | }), |
| 562 | ); |
| 563 | } |
| 564 | const hunks = |
| 565 | const startSide = |
| 566 | const startLine = |
| 567 | const covers = |
| 568 | line >= hunk[ |
| 569 | const hunk = |
| 570 | if (!hunk) |
| 571 | // A range stays inside one hunk: one spanning two would cover, and a suggestion would replace, lines the diff never showed. |
| 572 | if (!covers(hunk |
| 573 | throw new ReviewLineNotInDiffError( |
| 574 | } |
| 575 | if (startSide === |
| 576 | throw new InvalidLineRangeError() |
| 577 | } |
| 578 | located.push({ |
| 579 | } |
| 580 | return located; |
| 581 | } |
| 582 | |
| 583 | private async findReview |
| 584 | const [review] |
| 585 | this.db, |
| 586 | eq(schema.pullRequestReview |
| 587 | ); |
| 588 | if (!review) |
| 589 | return review; |
| 590 | } |
| 591 | |
| 592 | private async submittedReview |
| 593 | const [review] |
| 594 | .select() |
| 595 | .from(schema.pullRequestReview |
| 596 | .where( |
| 597 | and( |
| 598 | eq(schema.pullRequestReview |
| 599 | eq(schema.pullRequestReview |
| 600 | isNotNull(schema.pullRequestReview |
| 601 | ), |
| 602 | ); |
| 603 | if (!review) |
| 604 | return review; |
| 605 | } |
| 606 | |
| 607 | /** Another reviewer's pending comments do not exist as far as the requester can tell. */ |
| 608 | private async visibleComment |
| 609 | { pullRequest }: |
| 610 | { commentId, requesterId |
| 611 | ) { |
| 612 | const [comment] |
| 613 | .select({ |
| 614 | id: schema.pullRequestReviewComment |
| 615 | reviewId: schema.pullRequestReviewComment |
| 616 | inReplyToId: schema.pullRequestReviewComment |
| 617 | authorId: schema.pullRequestReviewComment |
| 618 | authorUsername: sql<string |
| 619 | authorImage: schema.user |
| 620 | path: schema.pullRequestReviewComment |
| 621 | side: schema.pullRequestReviewComment |
| 622 | line: schema.pullRequestReviewComment |
| 623 | startSide: schema.pullRequestReviewComment |
| 624 | startLine: schema.pullRequestReviewComment |
| 625 | body: schema.pullRequestReviewComment |
| 626 | commitSha: schema.pullRequestReviewComment |
| 627 | pending: sql<boolean |
| 628 | }) |
| 629 | .from(schema.pullRequestReviewComment |
| 630 | .innerJoin( |
| 631 | schema.user, |
| 632 | eq(schema.user |
| 633 | ) |
| 634 | .leftJoin( |
| 635 | schema.pullRequestReview, |
| 636 | eq( |
| 637 | schema.pullRequestReview.id |
| 638 | schema.pullRequestReviewComment.reviewId |
| 639 | ), |
| 640 | ) |
| 641 | .where( |
| 642 | and( |
| 643 | eq(schema.pullRequestReviewComment |
| 644 | eq(schema.pullRequestReviewComment |
| 645 | ), |
| 646 | ); |
| 647 | if (!comment || |
| 648 | throw new ReviewCommentNotFoundError() |
| 649 | } |
| 650 | return comment; |
| 651 | } |
| 652 | |
| 653 | private authorizeWrite({ |
| 654 | username, |
| 655 | repo, |
| 656 | requesterId, |
| 657 | }: { |
| 658 | username: string |
| 659 | repo: string |
| 660 | requesterId: string |
| 661 | }) { |
| 662 | return this.access |
| 663 | username, |
| 664 | repo, |
| 665 | actor: { userId: requesterId |
| 666 | operation: 'write', |
| 667 | }); |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | function pendingOf(pullRequestId: |
| 672 | return and( |
| 673 | eq(schema.pullRequestReview |
| 674 | eq(schema.pullRequestReview |
| 675 | isNull(schema.pullRequestReview |
| 676 | ); |
| 677 | } |
| 678 | |
| 679 | function touch(tx: |
| 680 | return tx |
| 681 | .update(schema.issue |
| 682 | .set({ updatedAt: |
| 683 | .where(eq(schema |
| 684 | } |