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