| 1 | import { mkdtemp, rm } from 'node:fs/promises'; |
| 2 | import { tmpdir } |
| 3 | import path from 'node:path |
| 4 | import { type Database, |
| 5 | import { Inject, Injectable |
| 6 | import { |
| 7 | and, |
| 8 | count, |
| 9 | desc, |
| 10 | eq, |
| 11 | type GetColumnData, |
| 12 | getTableColumns, |
| 13 | inArray, |
| 14 | lt, |
| 15 | or, |
| 16 | } from 'drizzle-orm'; |
| 17 | |
| 18 | import { DATABASE } |
| 19 | import { listCommits } |
| 20 | import { CommitVerificationService } |
| 21 | import { |
| 22 | listDiffFiles, |
| 23 | mergeBase, |
| 24 | streamDiffPatch, |
| 25 | } from '../../lib/git/diff/diff.js'; |
| 26 | import { runGit } |
| 27 | import { RepositoryMaterializerService } |
| 28 | import { commitTree, mergeTree |
| 29 | import { |
| 30 | resolveCommit, |
| 31 | resolveDefaultRef, |
| 32 | } from '../../lib/git/tree/resolve-ref.js'; |
| 33 | import { |
| 34 | closeIssue, |
| 35 | MAX_CLOSING_COMMITS, |
| 36 | } from '../../lib/issues/close-issue.js'; |
| 37 | import { IssueReferencesService } |
| 38 | import { IssuesService } |
| 39 | import { fileBody } |
| 40 | import { RepositoryAccessService } |
| 41 | import { |
| 42 | ownerNameOf, |
| 43 | type Repository, |
| 44 | type RepositoryOperation, |
| 45 | } from '../../lib/git/repository-access/repository-access.js'; |
| 46 | import { RepositoryStorageService } |
| 47 | import { PushTransactionService } |
| 48 | import { UsersService } |
| 49 | import { decodeCursor, encodeCursor |
| 50 | import { |
| 51 | BranchNotFoundError, |
| 52 | CommitNotFoundError, |
| 53 | InvalidCursorError, |
| 54 | } from '../repositories/repositories.errors.js'; |
| 55 | import { |
| 56 | CreatePullRequestRequestDTO, |
| 57 | UpdatePullRequestRequestDTO, |
| 58 | } from './dto/create-pull-request.dto.js'; |
| 59 | import { GetPullRequestsQueryDTO } |
| 60 | import { |
| 61 | NothingToMergeError, |
| 62 | PullRequestAlreadyOpenError, |
| 63 | PullRequestConflictError, |
| 64 | PullRequestHeadDeletedError, |
| 65 | PullRequestNotFoundError, |
| 66 | PullRequestNotOpenError, |
| 67 | SameBranchPullRequestError, |
| 68 | UnrelatedHistoriesError, |
| 69 | UnrelatedRepositoriesError, |
| 70 | } from './pull-requests.errors.js'; |
| 71 | |
| 72 | // Number, title, body and author live on the issue a request is attached to. |
| 73 | const pullRequestColumns = { |
| 74 | id: schema.pullRequest |
| 75 | issueId: schema.pullRequest |
| 76 | number: schema.issue |
| 77 | title: schema.issue |
| 78 | body: schema.issue |
| 79 | authorId: schema.issue |
| 80 | state: schema.pullRequest |
| 81 | baseRepositoryId: schema.pullRequest |
| 82 | baseRef: schema.pullRequest |
| 83 | headRepositoryId: schema.pullRequest |
| 84 | headRef: schema.pullRequest |
| 85 | headSha: schema.pullRequest |
| 86 | mergeCommitSha: schema.pullRequest |
| 87 | createdAt: schema.issue |
| 88 | updatedAt: schema.issue |
| 89 | }; |
| 90 | |
| 91 | const DEFAULT_PAGE_SIZE = 20 |
| 92 | const MAX_PAGE_SIZE = 100 |
| 93 | |
| 94 | type PullRequest |
| 95 | [Key |
| 96 | (typeof pullRequestColumns |
| 97 | >; |
| 98 | }; |
| 99 | |
| 100 | @Injectable() |
| 101 | export class PullRequestsService |
| 102 | constructor( |
| 103 | @Inject(DATABASE) |
| 104 | private readonly users |
| 105 | private readonly access |
| 106 | private readonly storage |
| 107 | private readonly materializer |
| 108 | private readonly pushTransaction |
| 109 | private readonly verification |
| 110 | private readonly issues |
| 111 | private readonly references |
| 112 | ) {} |
| 113 | |
| 114 | async createPullRequest({ |
| 115 | username, |
| 116 | repo, |
| 117 | requesterId, |
| 118 | body, |
| 119 | }: { |
| 120 | username: string |
| 121 | repo: string |
| 122 | requesterId: string |
| 123 | body: CreatePullRequestRequestDTO |
| 124 | }) { |
| 125 | const base = |
| 126 | const head = |
| 127 | const headRef = |
| 128 | ? body.head. |
| 129 | : body.head; |
| 130 | |
| 131 | if (head.id |
| 132 | throw new SameBranchPullRequestError() |
| 133 | } |
| 134 | |
| 135 | const [baseDirectory, |
| 136 | this.openCache(base |
| 137 | this.openCache(head |
| 138 | ]); |
| 139 | const baseSha = |
| 140 | const headSha = |
| 141 | |
| 142 | const alternates = |
| 143 | if ( |
| 144 | !(await mergeBase( |
| 145 | gitDir: baseDirectory, |
| 146 | alternates, |
| 147 | a: baseSha, |
| 148 | b: headSha, |
| 149 | })) |
| 150 | ) { |
| 151 | throw new UnrelatedHistoriesError() |
| 152 | } |
| 153 | |
| 154 | const [existing] |
| 155 | .select({ number: |
| 156 | .from(schema.pullRequest |
| 157 | .innerJoin(schema.issue |
| 158 | .where( |
| 159 | and( |
| 160 | eq(schema.pullRequest |
| 161 | eq(schema.pullRequest |
| 162 | eq(schema.pullRequest |
| 163 | eq(schema.pullRequest |
| 164 | eq(schema.pullRequest |
| 165 | ), |
| 166 | ); |
| 167 | if (existing) throw |
| 168 | |
| 169 | const issue = |
| 170 | { |
| 171 | repository: base, |
| 172 | title: body.title |
| 173 | body: body.body |
| 174 | authorId: requesterId, |
| 175 | isPullRequest: true, |
| 176 | }, |
| 177 | async (tx, |
| 178 | await tx.insert |
| 179 | issueId: row.id |
| 180 | baseRepositoryId: base.id |
| 181 | baseRef: body.base |
| 182 | headRepositoryId: head.id |
| 183 | headRef, |
| 184 | headSha, |
| 185 | }); |
| 186 | }, |
| 187 | ); |
| 188 | |
| 189 | const { pullRequest |
| 190 | username, |
| 191 | repo, |
| 192 | number: issue.number |
| 193 | requesterId, |
| 194 | }); |
| 195 | return this.expandPullRequest |
| 196 | } |
| 197 | |
| 198 | /** The files a request would carry, before one is opened. */ |
| 199 | async compare(params |
| 200 | const { from, |
| 201 | return { from, |
| 202 | } |
| 203 | |
| 204 | /** One file of that same diff, or all of it when no path is given. */ |
| 205 | async compareStreamPatch(params |
| 206 | const { range |
| 207 | return streamDiffPatch({ |
| 208 | } |
| 209 | |
| 210 | private async openComparison |
| 211 | username, |
| 212 | repo, |
| 213 | requesterId, |
| 214 | base: baseRef, |
| 215 | head: headSpec, |
| 216 | }: CompareParams |
| 217 | const base = |
| 218 | const head = |
| 219 | const headRef = |
| 220 | ? headSpec.slice( |
| 221 | : headSpec; |
| 222 | |
| 223 | const [baseDirectory, |
| 224 | this.openCache(base |
| 225 | this.openCache(head |
| 226 | ]); |
| 227 | const alternates = |
| 228 | |
| 229 | const baseSha = |
| 230 | const headSha = |
| 231 | const from = |
| 232 | gitDir: baseDirectory, |
| 233 | alternates, |
| 234 | a: baseSha, |
| 235 | b: headSha, |
| 236 | }); |
| 237 | if (!from) |
| 238 | |
| 239 | return { |
| 240 | from, |
| 241 | to: headSha, |
| 242 | range: { gitDir: baseDirectory |
| 243 | }; |
| 244 | } |
| 245 | |
| 246 | async getPullRequests({ |
| 247 | username, |
| 248 | repo, |
| 249 | requesterId, |
| 250 | query, |
| 251 | }: { |
| 252 | username: string |
| 253 | repo: string |
| 254 | requesterId?: string |
| 255 | query: GetPullRequestsQueryDTO |
| 256 | }) { |
| 257 | const base = |
| 258 | |
| 259 | const requested = |
| 260 | const pageSize = |
| 261 | ? Math.min( |
| 262 | : DEFAULT_PAGE_SIZE; |
| 263 | |
| 264 | const decoded = |
| 265 | if (query.cursor |
| 266 | |
| 267 | const state = |
| 268 | const rows = |
| 269 | .select(pullRequestColumns) |
| 270 | .from(schema.pullRequest |
| 271 | .innerJoin(schema.issue |
| 272 | .where( |
| 273 | and( |
| 274 | eq(schema.pullRequest |
| 275 | state === 'all' |
| 276 | decoded |
| 277 | ? or( |
| 278 | lt(schema.issue |
| 279 | and( |
| 280 | eq(schema.issue |
| 281 | lt(schema.pullRequest |
| 282 | ), |
| 283 | ) |
| 284 | : undefined, |
| 285 | ), |
| 286 | ) |
| 287 | .orderBy(desc(schema |
| 288 | .limit(pageSize + |
| 289 | |
| 290 | const hasMore = |
| 291 | const page = |
| 292 | const last = |
| 293 | |
| 294 | const [totals] |
| 295 | .select({ total: |
| 296 | .from(schema.pullRequest |
| 297 | .where( |
| 298 | and( |
| 299 | eq(schema.pullRequest |
| 300 | state === 'all' |
| 301 | ), |
| 302 | ); |
| 303 | |
| 304 | return { |
| 305 | pullRequests: await Promise |
| 306 | page.map((row |
| 307 | ), |
| 308 | total: totals?.total |
| 309 | nextCursor: |
| 310 | hasMore && last |
| 311 | ? encodeCursor({ date |
| 312 | : null, |
| 313 | hasMore, |
| 314 | }; |
| 315 | } |
| 316 | |
| 317 | async getPullRequest(params |
| 318 | const { pullRequest, |
| 319 | const files = |
| 320 | ? await listDiffFiles( |
| 321 | gitDir: git.baseDirectory |
| 322 | alternates: git.alternates |
| 323 | from: git.mergeBase |
| 324 | to: git.headSha |
| 325 | }) |
| 326 | : []; |
| 327 | |
| 328 | return { |
| 329 | ...(await this |
| 330 | mergeBase: git.mergeBase |
| 331 | commitCount: git.mergeBase |
| 332 | ? await this. |
| 333 | : 0, |
| 334 | changedFiles: files.length, |
| 335 | additions: files.reduce |
| 336 | deletions: files.reduce |
| 337 | mergeable: |
| 338 | pullRequest.state === |
| 339 | git.mergeBase !== |
| 340 | git.mergeBase !== |
| 341 | (await mergeTree({ |
| 342 | gitDir: git.baseDirectory |
| 343 | alternates: git.alternates |
| 344 | base: git.baseSha |
| 345 | head: git.headSha |
| 346 | })) !== null, |
| 347 | }; |
| 348 | } |
| 349 | |
| 350 | async getCommits( |
| 351 | params: PullRequestRef |
| 352 | ) { |
| 353 | const git = |
| 354 | if (!git.mergeBase |
| 355 | |
| 356 | const limit = |
| 357 | Math.max(params |
| 358 | MAX_PAGE_SIZE, |
| 359 | ); |
| 360 | const { commits, |
| 361 | gitDir: git.baseDirectory |
| 362 | env: git.env |
| 363 | ref: `${git. |
| 364 | limit, |
| 365 | }); |
| 366 | |
| 367 | // A fork's commits live in the other repository's objects, which is why the lending env has to come along for the signatures to be readable. |
| 368 | const verdicts = |
| 369 | gitDir: git.baseDirectory |
| 370 | env: git.env |
| 371 | commits, |
| 372 | }); |
| 373 | |
| 374 | return { |
| 375 | commits: commits.map |
| 376 | ...commit, |
| 377 | verification: verdicts.get |
| 378 | })), |
| 379 | total: await this |
| 380 | nextCursor, |
| 381 | }; |
| 382 | } |
| 383 | |
| 384 | async getFiles(params |
| 385 | const git = |
| 386 | if (!git.mergeBase |
| 387 | return { from: |
| 388 | } |
| 389 | |
| 390 | return { |
| 391 | from: git.mergeBase |
| 392 | to: git.headSha |
| 393 | files: await listDiffFiles |
| 394 | gitDir: git.baseDirectory |
| 395 | alternates: git.alternates |
| 396 | from: git.mergeBase |
| 397 | to: git.headSha |
| 398 | }), |
| 399 | }; |
| 400 | } |
| 401 | |
| 402 | async streamPatch(params |
| 403 | const git = |
| 404 | if (git.headDeleted |
| 405 | return streamDiffPatch({ |
| 406 | gitDir: git.baseDirectory |
| 407 | alternates: git.alternates |
| 408 | from: git.mergeBase |
| 409 | to: git.headSha |
| 410 | path: params.path |
| 411 | }); |
| 412 | } |
| 413 | |
| 414 | /** The merge commit is built in the base cache and then pushed through `commitPush`, the same commit point a `git push` uses. Nothing about a merge is allowed to reach the base repository by another route. */ |
| 415 | async mergePullRequest( |
| 416 | params: PullRequestRef |
| 417 | ) { |
| 418 | const { pullRequest, |
| 419 | ...params, |
| 420 | operation: 'write', |
| 421 | }); |
| 422 | if (pullRequest.state |
| 423 | throw new PullRequestNotOpenError( |
| 424 | } |
| 425 | // An open request always has its head: a repository heading one cannot be deleted. |
| 426 | const git = |
| 427 | pullRequest, |
| 428 | base, |
| 429 | pullRequest.headRepositoryId!, |
| 430 | ); |
| 431 | if (!git.mergeBase |
| 432 | if (git.mergeBase |
| 433 | |
| 434 | const tree = |
| 435 | gitDir: git.baseDirectory |
| 436 | alternates: git.alternates |
| 437 | base: git.baseSha |
| 438 | head: git.headSha |
| 439 | }); |
| 440 | if (!tree) |
| 441 | |
| 442 | const author = |
| 443 | // a fork's branch name is ambiguous on its own, so the merge subject carries the owner exactly as the request was opened with |
| 444 | const { head |
| 445 | const headLabel = |
| 446 | git.head.id |
| 447 | ? pullRequest.headRef |
| 448 | : `${head.username |
| 449 | |
| 450 | const mergeCommitSha = |
| 451 | gitDir: git.baseDirectory |
| 452 | alternates: git.alternates |
| 453 | tree, |
| 454 | parents: [git.baseSha |
| 455 | message: `${params. |
| 456 | author: { name: author |
| 457 | }); |
| 458 | |
| 459 | const directory = |
| 460 | try { |
| 461 | // ponytail: excludes only the base tip, so objects on the base repository's other branches can be packed again. Exclude every base ref if entry size matters. |
| 462 | const pack = |
| 463 | gitDir: git.baseDirectory |
| 464 | alternates: git.alternates |
| 465 | include: mergeCommitSha, |
| 466 | exclude: [git.baseSha |
| 467 | prefix: path.join |
| 468 | }); |
| 469 | |
| 470 | const { seq |
| 471 | repoId: base.id |
| 472 | transitions: [ |
| 473 | { |
| 474 | ref: `refs/heads/${pullRequest |
| 475 | oldOid: Buffer.from |
| 476 | newOid: Buffer.from |
| 477 | }, |
| 478 | ], |
| 479 | body: fileBody(pack |
| 480 | packOffset: 0, |
| 481 | pushedBy: params.requesterId |
| 482 | }); |
| 483 | |
| 484 | const isDefaultBranch = |
| 485 | (await resolveDefaultRef({ |
| 486 | `refs/heads/${pullRequest.baseRef |
| 487 | const { commits |
| 488 | ? await listCommits( |
| 489 | gitDir: git.baseDirectory |
| 490 | env: git.env |
| 491 | ref: `${git. |
| 492 | limit: MAX_CLOSING_COMMITS, |
| 493 | }) |
| 494 | : { commits: [] |
| 495 | |
| 496 | await this.db |
| 497 | await tx |
| 498 | .update(schema.pullRequest |
| 499 | .set({ |
| 500 | state: 'merged', |
| 501 | mergeCommitSha, |
| 502 | headSha: git.headSha |
| 503 | mergedAt: new Date |
| 504 | }) |
| 505 | .where(eq(schema |
| 506 | await closeIssue(tx |
| 507 | issueId: pullRequest.issueId |
| 508 | actorId: params.requesterId |
| 509 | type: 'merged', |
| 510 | commitSha: mergeCommitSha, |
| 511 | }); |
| 512 | |
| 513 | // Only a merge into the default branch closes anything, the same rule GitHub follows. |
| 514 | if (!isDefaultBranch) |
| 515 | await this.references |
| 516 | repository: base, |
| 517 | actorId: params.requesterId |
| 518 | commits, |
| 519 | }); |
| 520 | await this.references |
| 521 | sources: [ |
| 522 | { type: 'issue', |
| 523 | ...commits.map( |
| 524 | type: 'commit' as |
| 525 | id: commit.sha |
| 526 | })), |
| 527 | ], |
| 528 | actorId: params.requesterId |
| 529 | sourceIssueId: pullRequest.issueId |
| 530 | }); |
| 531 | }); |
| 532 | |
| 533 | await this.db |
| 534 | .update(schema.repository |
| 535 | .set({ lastPushedAt: |
| 536 | .where(eq(schema |
| 537 | |
| 538 | return { mergeCommitSha, |
| 539 | } finally { |
| 540 | await rm(directory |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | /** Title and description only - the branches a request spans never move. */ |
| 545 | async updatePullRequest( |
| 546 | params: PullRequestRef |
| 547 | requesterId: string |
| 548 | body: UpdatePullRequestRequestDTO |
| 549 | }, |
| 550 | ) { |
| 551 | await this.load |
| 552 | await this.issues |
| 553 | return this.expandPullRequest |
| 554 | } |
| 555 | |
| 556 | async closePullRequest(params |
| 557 | const { pullRequest |
| 558 | if (pullRequest.state |
| 559 | throw new PullRequestNotOpenError( |
| 560 | } |
| 561 | |
| 562 | await this.issues |
| 563 | return this.expandPullRequest |
| 564 | } |
| 565 | |
| 566 | /** Resolves the range the request covers. A merged request reads only the base, which has held its commits since the merge, so it outlives its head branch and repository. */ |
| 567 | private async open |
| 568 | const { pullRequest, |
| 569 | if (pullRequest.mergeCommitSha |
| 570 | return this.openMerged |
| 571 | } |
| 572 | if (!pullRequest.headRepositoryId |
| 573 | return { |
| 574 | pullRequest, |
| 575 | base, |
| 576 | baseDirectory: await this |
| 577 | alternates: [], |
| 578 | env: undefined, |
| 579 | baseSha: pullRequest.headSha |
| 580 | headSha: pullRequest.headSha |
| 581 | mergeBase: null, |
| 582 | headDeleted: true, |
| 583 | }; |
| 584 | } |
| 585 | return this.openLive |
| 586 | } |
| 587 | |
| 588 | private async openMerged |
| 589 | pullRequest: PullRequest |
| 590 | base: Repository |
| 591 | mergeCommitSha: string, |
| 592 | ) { |
| 593 | const baseDirectory = |
| 594 | const baseSha = |
| 595 | if (!baseSha) |
| 596 | |
| 597 | return { |
| 598 | pullRequest, |
| 599 | base, |
| 600 | baseDirectory, |
| 601 | alternates: [], |
| 602 | env: undefined, |
| 603 | baseSha, |
| 604 | headSha: pullRequest.headSha |
| 605 | mergeBase: await mergeBase |
| 606 | gitDir: baseDirectory, |
| 607 | alternates: [], |
| 608 | a: baseSha, |
| 609 | b: pullRequest.headSha |
| 610 | }), |
| 611 | headDeleted: false, |
| 612 | }; |
| 613 | } |
| 614 | |
| 615 | /** Materializes both sides and resolves the branches as they stand now. */ |
| 616 | private async openLive |
| 617 | pullRequest: PullRequest |
| 618 | base: Repository |
| 619 | headRepositoryId: string, |
| 620 | ) { |
| 621 | const [head] |
| 622 | .select() |
| 623 | .from(schema.repository |
| 624 | .where(eq(schema |
| 625 | |
| 626 | const [baseDirectory, |
| 627 | this.openCache(base |
| 628 | this.openCache(head |
| 629 | ]); |
| 630 | const alternates = |
| 631 | |
| 632 | const baseSha = |
| 633 | baseDirectory, |
| 634 | pullRequest.baseRef, |
| 635 | ); |
| 636 | const headSha = |
| 637 | headDirectory, |
| 638 | pullRequest.headRef, |
| 639 | ); |
| 640 | |
| 641 | return { |
| 642 | pullRequest, |
| 643 | base, |
| 644 | head, |
| 645 | baseDirectory, |
| 646 | alternates, |
| 647 | env: alternates.length |
| 648 | ? { |
| 649 | GIT_ALTERNATE_OBJECT_DIRECTORIES: path.join |
| 650 | headDirectory, |
| 651 | 'objects', |
| 652 | ), |
| 653 | } |
| 654 | : undefined, |
| 655 | baseSha, |
| 656 | headSha, |
| 657 | mergeBase: await mergeBase |
| 658 | gitDir: baseDirectory, |
| 659 | alternates, |
| 660 | a: baseSha, |
| 661 | b: headSha, |
| 662 | }), |
| 663 | headDeleted: false, |
| 664 | }; |
| 665 | } |
| 666 | |
| 667 | private async load |
| 668 | username, |
| 669 | repo, |
| 670 | number, |
| 671 | requesterId, |
| 672 | operation, |
| 673 | }: PullRequestRef |
| 674 | const base = |
| 675 | username, |
| 676 | repo, |
| 677 | requesterId, |
| 678 | operation, |
| 679 | }); |
| 680 | const [pullRequest] |
| 681 | .select(pullRequestColumns) |
| 682 | .from(schema.pullRequest |
| 683 | .innerJoin(schema.issue |
| 684 | .where( |
| 685 | and( |
| 686 | eq(schema.issue |
| 687 | eq(schema.issue |
| 688 | ), |
| 689 | ); |
| 690 | if (!pullRequest) |
| 691 | |
| 692 | return { pullRequest, |
| 693 | } |
| 694 | |
| 695 | private authorize({ |
| 696 | username, |
| 697 | repo, |
| 698 | requesterId, |
| 699 | operation = 'read' |
| 700 | }: { |
| 701 | username: string |
| 702 | repo: string |
| 703 | requesterId?: string |
| 704 | operation?: RepositoryOperation |
| 705 | }) { |
| 706 | return this.access |
| 707 | username, |
| 708 | repo, |
| 709 | actor: requesterId ? |
| 710 | operation, |
| 711 | }); |
| 712 | } |
| 713 | |
| 714 | private async openCache |
| 715 | const directory = |
| 716 | await this.materializer |
| 717 | repository.id, |
| 718 | directory, |
| 719 | repository.defaultBranch, |
| 720 | ); |
| 721 | return directory; |
| 722 | } |
| 723 | |
| 724 | private async resolveBranch |
| 725 | const sha = |
| 726 | args: [ |
| 727 | 'rev-parse', |
| 728 | '--verify', |
| 729 | '--end-of-options', |
| 730 | `refs/heads/${branch}`, |
| 731 | ], |
| 732 | gitDir, |
| 733 | }).catch(() |
| 734 | |
| 735 | if (!sha.trim |
| 736 | return sha.trim |
| 737 | } |
| 738 | |
| 739 | private async countRange |
| 740 | { |
| 741 | baseDirectory, |
| 742 | env, |
| 743 | }: { baseDirectory |
| 744 | range: string, |
| 745 | ) { |
| 746 | const raw = |
| 747 | args: ['rev-list', |
| 748 | gitDir: baseDirectory, |
| 749 | env, |
| 750 | }); |
| 751 | return Number(raw |
| 752 | } |
| 753 | |
| 754 | /** `owner:branch` names a branch on another repository in the same fork network - the base itself, a fork of it, or the repository the base was forked from. Anything else is not a pull request, it is two unrelated repos. */ |
| 755 | private async resolveHead |
| 756 | base: Repository |
| 757 | head: string, |
| 758 | requesterId: string, |
| 759 | ) { |
| 760 | if (!head.includes |
| 761 | |
| 762 | const owner = |
| 763 | const candidates = |
| 764 | .select(getTableColumns(schema |
| 765 | .from(schema.repository |
| 766 | .innerJoin(schema.user |
| 767 | .leftJoin( |
| 768 | schema.organization, |
| 769 | eq(schema.organization |
| 770 | ) |
| 771 | .where(eq(ownerNameOf |
| 772 | |
| 773 | const related = |
| 774 | (candidate) => |
| 775 | candidate.id === |
| 776 | candidate.parentRepositoryId === |
| 777 | candidate.id === |
| 778 | ); |
| 779 | if (!related) |
| 780 | |
| 781 | return this.access |
| 782 | username: owner, |
| 783 | repo: related.slug |
| 784 | actor: { userId: requesterId |
| 785 | operation: 'read', |
| 786 | }); |
| 787 | } |
| 788 | |
| 789 | private async expandPullRequest |
| 790 | const sides = |
| 791 | .select({ |
| 792 | repositoryId: schema.repository |
| 793 | slug: schema.repository |
| 794 | username: ownerNameOf(schema |
| 795 | }) |
| 796 | .from(schema.repository |
| 797 | .innerJoin(schema.user |
| 798 | .leftJoin( |
| 799 | schema.organization, |
| 800 | eq(schema.organization |
| 801 | ) |
| 802 | .where( |
| 803 | inArray(schema.repository |
| 804 | pullRequest.baseRepositoryId, |
| 805 | pullRequest.headRepositoryId ?? |
| 806 | ]), |
| 807 | ); |
| 808 | |
| 809 | const author = |
| 810 | // A deleted head repository has no row, and reads as null rather than as some other repository. |
| 811 | const sideOf = |
| 812 | const row = |
| 813 | return { username: |
| 814 | }; |
| 815 | |
| 816 | return { |
| 817 | id: pullRequest.id |
| 818 | number: pullRequest.number |
| 819 | title: pullRequest.title |
| 820 | body: pullRequest.body |
| 821 | state: pullRequest.state |
| 822 | base: sideOf(pullRequest |
| 823 | head: sideOf(pullRequest |
| 824 | headSha: pullRequest.headSha |
| 825 | mergeCommitSha: pullRequest.mergeCommitSha |
| 826 | authorUsername: author.username |
| 827 | createdAt: pullRequest.createdAt |
| 828 | updatedAt: pullRequest.updatedAt |
| 829 | }; |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | interface CompareParams { |
| 834 | username: string; |
| 835 | repo: string; |
| 836 | requesterId: string; |
| 837 | base: string; |
| 838 | head: string; |
| 839 | } |
| 840 | |
| 841 | interface PullRequestRef { |
| 842 | username: string; |
| 843 | repo: string; |
| 844 | number: number; |
| 845 | requesterId?: string |
| 846 | operation?: RepositoryOperation |
| 847 | } |