| 1 | import { type Database, schema } from '@ghost/db'; |
| 2 | import { Inject, Injectable |
| 3 | import { |
| 4 | and, |
| 5 | asc, |
| 6 | count, |
| 7 | desc, |
| 8 | eq, |
| 9 | ilike, |
| 10 | exists, |
| 11 | gte, |
| 12 | inArray, |
| 13 | isNotNull, |
| 14 | not, |
| 15 | or, |
| 16 | sql, |
| 17 | } from 'drizzle-orm'; |
| 18 | import { alias } |
| 19 | |
| 20 | import { DATABASE } |
| 21 | import { publishEvent } |
| 22 | import { assertNotImporting } |
| 23 | import { closeIssue } |
| 24 | import type { Executor |
| 25 | import { selectReviews } |
| 26 | import type { Role |
| 27 | import { RepositoryAccessService } |
| 28 | import { |
| 29 | ownerNameOf, |
| 30 | repositoryFullNameOf, |
| 31 | type Repository, |
| 32 | type RepositoryOperation, |
| 33 | } from '../../lib/repositories/access/repository-access.js'; |
| 34 | import { IssueReferencesService } |
| 35 | import { UsersService } |
| 36 | import { UserNotFoundError } |
| 37 | import { escapeLike, isoTimestamp |
| 38 | import { keyset } |
| 39 | import type { CreateIssueRequestDTO |
| 40 | import type { LabelDTO |
| 41 | import type { GetIssuesQueryDTO |
| 42 | import { |
| 43 | IssueCommentNotFoundError, |
| 44 | NotCommentAuthorError, |
| 45 | IssueNotFoundError, |
| 46 | IssueNotOpenError, |
| 47 | LabelAlreadyExistsError, |
| 48 | PullRequestReopenError, |
| 49 | LabelNotFoundError, |
| 50 | } from '../../lib/issues/issues.errors.js'; |
| 51 | import { touchIssue } |
| 52 | import { canEditThread } |
| 53 | |
| 54 | /** Filters and paging the GitHub REST layer needs beyond Ghost's own list: offset pages, pull requests alongside issues, `since`, and any or no assignee. */ |
| 55 | export type IssueListOptions |
| 56 | offset?: number |
| 57 | includePullRequests?: boolean |
| 58 | /** Lists only pull requests, narrowed by their own state and branches; overrides `includePullRequests`. */ |
| 59 | pullRequests?: { |
| 60 | states?: ( |
| 61 | baseRef?: string |
| 62 | headRef?: string |
| 63 | /** The login or organization owning the head repository, as in GitHub's `head=owner:branch`. */ |
| 64 | headOwner?: string |
| 65 | }; |
| 66 | since?: Date |
| 67 | assigned?: boolean |
| 68 | }; |
| 69 | |
| 70 | const NO_ISSUES = { |
| 71 | issues: [], |
| 72 | total: 0, |
| 73 | openCount: 0, |
| 74 | closedCount: 0, |
| 75 | nextCursor: null, |
| 76 | hasMore: false, |
| 77 | }; |
| 78 | |
| 79 | const labelColumns = { |
| 80 | id: schema.label |
| 81 | name: schema.label |
| 82 | description: schema.label |
| 83 | color: schema.label |
| 84 | createdAt: isoTimestamp(schema |
| 85 | updatedAt: isoTimestamp(schema |
| 86 | }; |
| 87 | |
| 88 | const commentColumns = { |
| 89 | id: schema.issueComment |
| 90 | body: schema.issueComment |
| 91 | createdAt: isoTimestamp(schema |
| 92 | updatedAt: isoTimestamp(schema |
| 93 | }; |
| 94 | |
| 95 | // The author is joined when comments are listed; a write already knows who made it. |
| 96 | const commentColumnsWithAuthor = { |
| 97 | ...commentColumns, |
| 98 | authorUsername: sql<string |
| 99 | }; |
| 100 | |
| 101 | const eventSource = alias |
| 102 | const eventSourceRepository = alias |
| 103 | schema.repository, |
| 104 | 'event_source_repository', |
| 105 | ); |
| 106 | const eventSourceOwner = alias |
| 107 | const eventSourceOrganization = alias |
| 108 | schema.organization, |
| 109 | 'event_source_organization', |
| 110 | ); |
| 111 | |
| 112 | const DEFAULT_PAGE_SIZE = 20 |
| 113 | |
| 114 | type Issue |
| 115 | type IssueEventType |
| 116 | |
| 117 | @Injectable() |
| 118 | export class IssuesService |
| 119 | constructor( |
| 120 | @Inject(DATABASE) |
| 121 | private readonly users |
| 122 | private readonly access |
| 123 | private readonly references |
| 124 | ) {} |
| 125 | |
| 126 | async createIssue({ |
| 127 | username, |
| 128 | repo, |
| 129 | requesterId, |
| 130 | body, |
| 131 | }: { |
| 132 | username: string |
| 133 | repo: string |
| 134 | requesterId: string |
| 135 | body: CreateIssueRequestDTO |
| 136 | }) { |
| 137 | const repository = |
| 138 | username, |
| 139 | repo, |
| 140 | requesterId, |
| 141 | }); |
| 142 | |
| 143 | const labels = |
| 144 | repository.id, |
| 145 | dedupe(body.labels |
| 146 | ); |
| 147 | const assignees = |
| 148 | |
| 149 | const created = |
| 150 | { |
| 151 | repository, |
| 152 | title: body.title |
| 153 | body: body.body |
| 154 | authorId: requesterId, |
| 155 | isPullRequest: false, |
| 156 | }, |
| 157 | async (tx, |
| 158 | if (labels.length |
| 159 | await tx.insert |
| 160 | labels.map((label |
| 161 | issueId: row.id |
| 162 | labelId: label.id |
| 163 | })), |
| 164 | ); |
| 165 | for (const label |
| 166 | await this.recordEvent |
| 167 | labelName: label.name |
| 168 | }); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | if (assignees.length |
| 173 | await tx.insert |
| 174 | assignees.map((user |
| 175 | issueId: row.id |
| 176 | userId: user.id |
| 177 | })), |
| 178 | ); |
| 179 | for (const user |
| 180 | await this.recordEvent |
| 181 | assigneeUsername: user.username |
| 182 | }); |
| 183 | } |
| 184 | } |
| 185 | }, |
| 186 | ); |
| 187 | |
| 188 | return this.expandIssue |
| 189 | } |
| 190 | |
| 191 | /** Inserts an issue or pull request with the repository's next number, records its opening and references, then runs `extend` in the same transaction. */ |
| 192 | async open( |
| 193 | { |
| 194 | repository, |
| 195 | title, |
| 196 | body, |
| 197 | authorId, |
| 198 | isPullRequest, |
| 199 | }: { |
| 200 | repository: Repository |
| 201 | title: string |
| 202 | body: string |
| 203 | authorId: string |
| 204 | isPullRequest: boolean |
| 205 | }, |
| 206 | extend: (tx |
| 207 | ) { |
| 208 | const values = |
| 209 | repositoryId: repository.id |
| 210 | title, |
| 211 | body, |
| 212 | authorId, |
| 213 | isPullRequest, |
| 214 | number: sql<number |
| 215 | }; |
| 216 | |
| 217 | // Two concurrent opens read the same max; the unique index rejects the loser, whose retry reads the winner's number. |
| 218 | for (let attempt |
| 219 | try { |
| 220 | return await this |
| 221 | await assertNotImporting(tx |
| 222 | const [row] |
| 223 | .insert(schema.issue |
| 224 | .values(values) |
| 225 | .returning(); |
| 226 | if (!row) |
| 227 | |
| 228 | await this.recordEvent |
| 229 | await publishEvent(tx |
| 230 | type: 'issue.opened', |
| 231 | repositoryId: repository.id |
| 232 | actorId: authorId, |
| 233 | payload: { issueId: row |
| 234 | }); |
| 235 | await this.references |
| 236 | tx, |
| 237 | { |
| 238 | type: 'issue', |
| 239 | id: row.id |
| 240 | repository, |
| 241 | issueId: row.id |
| 242 | actorId: authorId, |
| 243 | }, |
| 244 | body, |
| 245 | ); |
| 246 | await extend(tx |
| 247 | return row; |
| 248 | }); |
| 249 | } catch (error) |
| 250 | if (!isNumberCollision(error |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | async getIssues( |
| 256 | { |
| 257 | username, |
| 258 | repo, |
| 259 | requesterId, |
| 260 | query, |
| 261 | }: { |
| 262 | username: string |
| 263 | repo: string |
| 264 | requesterId?: string |
| 265 | query: GetIssuesQueryDTO |
| 266 | }, |
| 267 | options: IssueListOptions |
| 268 | ) { |
| 269 | const repository = |
| 270 | username, |
| 271 | repo, |
| 272 | requesterId, |
| 273 | }); |
| 274 | |
| 275 | const state = |
| 276 | const sort = |
| 277 | const list = |
| 278 | cursor: query.cursor |
| 279 | limit: query.limit |
| 280 | direction: query.direction |
| 281 | keys: |
| 282 | sort === 'comments' |
| 283 | ? { |
| 284 | commentCount: schema.issue |
| 285 | createdAt: schema.issue |
| 286 | id: schema.issue |
| 287 | } |
| 288 | : sort === ' |
| 289 | ? { updatedAt: schema |
| 290 | : { createdAt: schema |
| 291 | }); |
| 292 | |
| 293 | const labelNames = |
| 294 | (query.labels ?? |
| 295 | .split(',') |
| 296 | .map((name) |
| 297 | .filter(Boolean), |
| 298 | ); |
| 299 | const labelFilterIds = |
| 300 | repository.id, |
| 301 | labelNames, |
| 302 | ); |
| 303 | if (labelNames.length |
| 304 | return NO_ISSUES; |
| 305 | } |
| 306 | |
| 307 | const authorId = |
| 308 | ? await this. |
| 309 | : undefined; |
| 310 | if (query.author |
| 311 | return NO_ISSUES; |
| 312 | } |
| 313 | const assigneeId = |
| 314 | ? await this. |
| 315 | : undefined; |
| 316 | if (query.assignee |
| 317 | return NO_ISSUES; |
| 318 | } |
| 319 | |
| 320 | // Label (AND) and assignee filters resolve to id sets first so the main query stays a single indexed scan plus `inArray`. |
| 321 | const labelIssueIds: |
| 322 | labelFilterIds && labelFilterIds. |
| 323 | ? await this. |
| 324 | : undefined; |
| 325 | if (labelIssueIds && |
| 326 | return NO_ISSUES; |
| 327 | } |
| 328 | const assigneeRows = |
| 329 | ? await this. |
| 330 | .select({ issueId: |
| 331 | .from(schema.issueAssignee |
| 332 | .where(eq(schema |
| 333 | : null; |
| 334 | const assigneeFilterIds: |
| 335 | ? assigneeRows.map( |
| 336 | : undefined; |
| 337 | if (assigneeFilterIds && |
| 338 | return NO_ISSUES; |
| 339 | } |
| 340 | |
| 341 | const search = |
| 342 | const searchClause = |
| 343 | ? or( |
| 344 | ilike(schema.issue |
| 345 | ilike(schema.issue |
| 346 | ) |
| 347 | : undefined; |
| 348 | |
| 349 | const assignment = |
| 350 | .select({ issueId: |
| 351 | .from(schema.issueAssignee |
| 352 | .where(eq(schema |
| 353 | const pulls = |
| 354 | const pullRequest = |
| 355 | ? this.db |
| 356 | .select({ issueId: |
| 357 | .from(schema.pullRequest |
| 358 | .where( |
| 359 | and( |
| 360 | eq(schema.pullRequest |
| 361 | pulls.states?.length |
| 362 | ? inArray(schema. |
| 363 | : undefined, |
| 364 | pulls.baseRef |
| 365 | ? eq(schema. |
| 366 | : undefined, |
| 367 | pulls.headRef |
| 368 | ? eq(schema. |
| 369 | : undefined, |
| 370 | pulls.headOwner |
| 371 | ? inArray( |
| 372 | schema.pullRequest.headRepositoryId |
| 373 | this.db |
| 374 | .select({ id: |
| 375 | .from(schema.repository |
| 376 | .innerJoin( |
| 377 | schema.user, |
| 378 | eq(schema.user |
| 379 | ) |
| 380 | .leftJoin( |
| 381 | schema.organization, |
| 382 | eq( |
| 383 | schema.organization.id |
| 384 | schema.repository.organizationId |
| 385 | ), |
| 386 | ) |
| 387 | .where( |
| 388 | eq( |
| 389 | ownerNameOf(schema.user |
| 390 | pulls.headOwner, |
| 391 | ), |
| 392 | ), |
| 393 | ) |
| 394 | : undefined, |
| 395 | ), |
| 396 | ) |
| 397 | : undefined; |
| 398 | const baseClause = |
| 399 | and( |
| 400 | eq(schema.issue |
| 401 | pullRequest |
| 402 | ? exists(pullRequest) |
| 403 | : options.includePullRequests |
| 404 | ? undefined |
| 405 | : eq(schema. |
| 406 | forState === 'all' |
| 407 | options.since ? |
| 408 | options.assigned === |
| 409 | ? undefined |
| 410 | : options.assigned |
| 411 | ? exists(assignment) |
| 412 | : not(exists( |
| 413 | authorId ? eq( |
| 414 | labelIssueIds ? inArray( |
| 415 | assigneeFilterIds |
| 416 | ? inArray(schema. |
| 417 | : undefined, |
| 418 | searchClause, |
| 419 | ); |
| 420 | |
| 421 | const rows = |
| 422 | .select() |
| 423 | .from(schema.issue |
| 424 | .where(and(baseClause |
| 425 | .orderBy(...list |
| 426 | .limit(list.limit |
| 427 | // ponytail: OFFSET scans the rows it skips; REST pages deep into a huge repository get slower, keyset via a cursor in Link if that matters. |
| 428 | .offset(options.offset |
| 429 | |
| 430 | const { page, |
| 431 | |
| 432 | const [[totalRow], |
| 433 | this.db |
| 434 | .select({ total: |
| 435 | .from(schema.issue |
| 436 | .where(baseClause(' |
| 437 | this.db |
| 438 | .select({ total: |
| 439 | .from(schema.issue |
| 440 | .where(baseClause(' |
| 441 | this.db |
| 442 | .select({ total: |
| 443 | .from(schema.issue |
| 444 | .where(baseClause(' |
| 445 | ]); |
| 446 | |
| 447 | return { |
| 448 | issues: await this |
| 449 | total: totalRow?.total |
| 450 | openCount: openRow?.total |
| 451 | closedCount: closedRow?.total |
| 452 | nextCursor, |
| 453 | hasMore, |
| 454 | }; |
| 455 | } |
| 456 | |
| 457 | async getIssue(params |
| 458 | const { issue, |
| 459 | return this.expandIssue |
| 460 | } |
| 461 | |
| 462 | /** Title and body only — state moves through close/reopen. */ |
| 463 | async updateIssue( |
| 464 | params: IssueRef |
| 465 | requesterId: string |
| 466 | body: { |
| 467 | }, |
| 468 | ) { |
| 469 | const { issue, |
| 470 | if (issue.authorId |
| 471 | await this.access |
| 472 | } |
| 473 | |
| 474 | const { title, |
| 475 | if (title === |
| 476 | return this.expandIssue |
| 477 | } |
| 478 | |
| 479 | const oldTitle = |
| 480 | const updated = |
| 481 | const [row] |
| 482 | .update(schema.issue |
| 483 | .set({ |
| 484 | ...(title === |
| 485 | ...(body === |
| 486 | }) |
| 487 | .where(eq(schema |
| 488 | .returning(); |
| 489 | if (!row) |
| 490 | |
| 491 | if (title !== |
| 492 | await this.recordEvent |
| 493 | oldTitle, |
| 494 | newTitle: title, |
| 495 | }); |
| 496 | } |
| 497 | if (body !== |
| 498 | await this.recordEvent |
| 499 | await this.references |
| 500 | tx, |
| 501 | { |
| 502 | type: 'issue', |
| 503 | id: issue.id |
| 504 | repository: base, |
| 505 | issueId: issue.id |
| 506 | actorId: params.requesterId |
| 507 | }, |
| 508 | body, |
| 509 | ); |
| 510 | } |
| 511 | if ( |
| 512 | (title !== undefined |
| 513 | (body !== undefined |
| 514 | ) { |
| 515 | await publishEvent(tx |
| 516 | type: 'issue.edited', |
| 517 | repositoryId: base.id |
| 518 | actorId: params.requesterId |
| 519 | payload: { issueId: issue |
| 520 | }); |
| 521 | } |
| 522 | return row; |
| 523 | }); |
| 524 | |
| 525 | return this.expandIssue |
| 526 | } |
| 527 | |
| 528 | async closeIssue(params |
| 529 | const { issue, |
| 530 | if (issue.authorId |
| 531 | await this.access |
| 532 | } |
| 533 | if (issue.state |
| 534 | |
| 535 | const closed = |
| 536 | const row = |
| 537 | issueId: issue.id |
| 538 | actorId: params.requesterId |
| 539 | }); |
| 540 | if (!row) |
| 541 | if (row.isPullRequest |
| 542 | await tx |
| 543 | .update(schema.pullRequest |
| 544 | .set({ state: |
| 545 | .where( |
| 546 | and( |
| 547 | eq(schema.pullRequest |
| 548 | eq(schema.pullRequest |
| 549 | ), |
| 550 | ); |
| 551 | } |
| 552 | return row; |
| 553 | }); |
| 554 | return this.expandIssue |
| 555 | } |
| 556 | |
| 557 | /** A pull request reopens only through PullRequestsService.reopenPullRequest, which passes `reopenPullRequest` to reopen its row in the same transaction. */ |
| 558 | async reopenIssue( |
| 559 | params: IssueRef |
| 560 | reopenPullRequest?: (tx |
| 561 | ) { |
| 562 | const { issue, |
| 563 | if (issue.authorId |
| 564 | await this.access |
| 565 | } |
| 566 | if (issue.state |
| 567 | if (issue.isPullRequest |
| 568 | throw new PullRequestReopenError() |
| 569 | |
| 570 | const reopened = |
| 571 | await reopenPullRequest?.(tx |
| 572 | const [row] |
| 573 | .update(schema.issue |
| 574 | .set({ state: |
| 575 | .where(eq(schema |
| 576 | .returning(); |
| 577 | if (!row) |
| 578 | |
| 579 | await this.recordEvent |
| 580 | await publishEvent(tx |
| 581 | type: 'issue.reopened', |
| 582 | repositoryId: base.id |
| 583 | actorId: params.requesterId |
| 584 | payload: { issueId: issue |
| 585 | }); |
| 586 | return row; |
| 587 | }); |
| 588 | return this.expandIssue |
| 589 | } |
| 590 | |
| 591 | /** Anyone who can read the issue can read its comments, and anyone who can read it can add one — the same bar GitHub sets for a public repository. */ |
| 592 | async getComments(params |
| 593 | const { issue |
| 594 | |
| 595 | // ponytail: unpaginated, add a cursor if a thread ever outgrows one page |
| 596 | const comments = |
| 597 | .select(commentColumnsWithAuthor) |
| 598 | .from(schema.issueComment |
| 599 | .innerJoin(schema.user |
| 600 | .where(eq(schema |
| 601 | .orderBy(asc(schema |
| 602 | |
| 603 | return { comments |
| 604 | } |
| 605 | |
| 606 | /** One page of comments in GitHub's REST order, for one issue when `number` is given or else for the whole repository; GitHub's numeric id and the issue each belongs to come with every comment. */ |
| 607 | async getCommentPage(params |
| 608 | username: string |
| 609 | repo: string |
| 610 | requesterId?: string |
| 611 | number?: number |
| 612 | sort: 'created |
| 613 | direction: 'asc |
| 614 | since?: Date |
| 615 | offset: number |
| 616 | limit: number |
| 617 | }) { |
| 618 | const scope = |
| 619 | params.number === |
| 620 | ? eq( |
| 621 | schema.issue.repositoryId |
| 622 | (await this.access |
| 623 | ) |
| 624 | : eq( |
| 625 | schema.issue.id |
| 626 | (await this.load |
| 627 | ); |
| 628 | const where = |
| 629 | scope, |
| 630 | params.since |
| 631 | ? gte(schema. |
| 632 | : undefined, |
| 633 | ); |
| 634 | const order = |
| 635 | const [comments, |
| 636 | this.db |
| 637 | .select({ |
| 638 | ...commentColumnsWithAuthor, |
| 639 | numericId: schema.issueComment |
| 640 | issueNumber: schema.issue |
| 641 | isPullRequest: schema.issue |
| 642 | }) |
| 643 | .from(schema.issueComment |
| 644 | .innerJoin( |
| 645 | schema.issue, |
| 646 | eq(schema.issue |
| 647 | ) |
| 648 | .innerJoin( |
| 649 | schema.user, |
| 650 | eq(schema.user |
| 651 | ) |
| 652 | .where(where) |
| 653 | .orderBy( |
| 654 | order( |
| 655 | params.sort === |
| 656 | ? schema.issueComment. |
| 657 | : schema.issueComment. |
| 658 | ), |
| 659 | order(schema.issueComment |
| 660 | ) |
| 661 | .limit(params.limit |
| 662 | .offset(params.offset |
| 663 | this.db |
| 664 | .select({ total: |
| 665 | .from(schema.issueComment |
| 666 | .innerJoin( |
| 667 | schema.issue, |
| 668 | eq(schema.issue |
| 669 | ) |
| 670 | .where(where), |
| 671 | ]); |
| 672 | return { comments, |
| 673 | } |
| 674 | |
| 675 | async createComment( |
| 676 | params: IssueRef |
| 677 | ) { |
| 678 | const { issue, |
| 679 | |
| 680 | const created = |
| 681 | const [row] |
| 682 | .insert(schema.issueComment |
| 683 | .values({ |
| 684 | issueId: issue.id |
| 685 | authorId: params.requesterId |
| 686 | body: params.body |
| 687 | }) |
| 688 | .returning(commentColumns); |
| 689 | if (!row) |
| 690 | await publishEvent(tx |
| 691 | type: 'issue.commented', |
| 692 | repositoryId: base.id |
| 693 | actorId: params.requesterId |
| 694 | payload: { issueId: issue |
| 695 | }); |
| 696 | |
| 697 | await this.references |
| 698 | tx, |
| 699 | { |
| 700 | type: 'comment', |
| 701 | id: row.id |
| 702 | repository: base, |
| 703 | issueId: issue.id |
| 704 | actorId: params.requesterId |
| 705 | }, |
| 706 | params.body, |
| 707 | ); |
| 708 | |
| 709 | await tx |
| 710 | .update(schema.issue |
| 711 | .set({ |
| 712 | commentCount: sql`${schema |
| 713 | updatedAt: new Date |
| 714 | }) |
| 715 | .where(eq(schema |
| 716 | |
| 717 | return row; |
| 718 | }); |
| 719 | |
| 720 | const author = |
| 721 | return { ...created |
| 722 | } |
| 723 | |
| 724 | async updateComment( |
| 725 | params: IssueRef |
| 726 | requesterId: string |
| 727 | commentId: string |
| 728 | body: string |
| 729 | }, |
| 730 | ) { |
| 731 | const { issue, |
| 732 | const [comment] |
| 733 | .select() |
| 734 | .from(schema.issueComment |
| 735 | .where( |
| 736 | and( |
| 737 | eq(schema.issueComment |
| 738 | eq(schema.issueComment |
| 739 | ), |
| 740 | ); |
| 741 | if (!comment) |
| 742 | // Writers may delete someone else's comment, but never put words in their mouth. |
| 743 | if (comment.authorId |
| 744 | throw new NotCommentAuthorError() |
| 745 | } |
| 746 | |
| 747 | const updated = |
| 748 | const [row] |
| 749 | .update(schema.issueComment |
| 750 | .set({ body: |
| 751 | .where(eq(schema |
| 752 | .returning(commentColumns); |
| 753 | if (!row) |
| 754 | if (params.body |
| 755 | await publishEvent(tx |
| 756 | type: 'issue.comment_edited', |
| 757 | repositoryId: base.id |
| 758 | actorId: params.requesterId |
| 759 | payload: { issueId: issue |
| 760 | }); |
| 761 | } |
| 762 | |
| 763 | await this.references |
| 764 | tx, |
| 765 | { |
| 766 | type: 'comment', |
| 767 | id: comment.id |
| 768 | repository: base, |
| 769 | issueId: issue.id |
| 770 | actorId: comment.authorId |
| 771 | }, |
| 772 | params.body, |
| 773 | ); |
| 774 | await touchIssue(tx |
| 775 | return row; |
| 776 | }); |
| 777 | |
| 778 | const author = |
| 779 | return { ...updated |
| 780 | } |
| 781 | |
| 782 | async deleteComment( |
| 783 | params: IssueRef |
| 784 | ) { |
| 785 | const { issue, |
| 786 | const [comment] |
| 787 | .select() |
| 788 | .from(schema.issueComment |
| 789 | .where( |
| 790 | and( |
| 791 | eq(schema.issueComment |
| 792 | eq(schema.issueComment |
| 793 | ), |
| 794 | ); |
| 795 | if (!comment) |
| 796 | if (comment.authorId |
| 797 | await this.access |
| 798 | } |
| 799 | |
| 800 | await this.db |
| 801 | await tx |
| 802 | .delete(schema.issueComment |
| 803 | .where(eq(schema |
| 804 | await publishEvent(tx |
| 805 | type: 'issue.comment_deleted', |
| 806 | repositoryId: base.id |
| 807 | actorId: params.requesterId |
| 808 | payload: { |
| 809 | issueId: issue.id |
| 810 | comment: { id: comment |
| 811 | }, |
| 812 | }); |
| 813 | await this.references |
| 814 | await tx |
| 815 | .update(schema.issue |
| 816 | .set({ |
| 817 | commentCount: sql`greatest(${ |
| 818 | updatedAt: new Date |
| 819 | }) |
| 820 | .where(eq(schema |
| 821 | }); |
| 822 | |
| 823 | return { deleted: |
| 824 | } |
| 825 | |
| 826 | async getTimeline(params |
| 827 | const { issue |
| 828 | |
| 829 | const [comments, |
| 830 | this.db |
| 831 | .select({ |
| 832 | kind: sql<'comment |
| 833 | ...commentColumnsWithAuthor, |
| 834 | authorImage: schema.user |
| 835 | }) |
| 836 | .from(schema.issueComment |
| 837 | .innerJoin( |
| 838 | schema.user, |
| 839 | eq(schema.user |
| 840 | ) |
| 841 | .where(eq(schema |
| 842 | this.db |
| 843 | .select({ |
| 844 | kind: sql<'event |
| 845 | id: schema.issueEvent |
| 846 | createdAt: isoTimestamp(schema |
| 847 | event: { |
| 848 | id: schema.issueEvent |
| 849 | type: schema.issueEvent |
| 850 | actorUsername: sql<string |
| 851 | actorImage: schema.user |
| 852 | labelName: schema.issueEvent |
| 853 | assigneeUsername: schema.issueEvent |
| 854 | oldTitle: schema.issueEvent |
| 855 | newTitle: schema.issueEvent |
| 856 | commitSha: schema.issueEvent |
| 857 | beforeSha: schema.issueEvent |
| 858 | commitMessage: schema.issueEvent |
| 859 | commitAuthorName: schema.issueEvent |
| 860 | // `owner/repo`, so a pull request in another repository that closed this one still links |
| 861 | sourceRepository: sql< |
| 862 | string | null |
| 863 | >`${repositoryFullNameOf(eventSourceOwner |
| 864 | sourceNumber: eventSource.number |
| 865 | createdAt: isoTimestamp(schema |
| 866 | }, |
| 867 | }) |
| 868 | .from(schema.issueEvent |
| 869 | .leftJoin(schema.user |
| 870 | .leftJoin( |
| 871 | eventSource, |
| 872 | eq(eventSource.id |
| 873 | ) |
| 874 | .leftJoin( |
| 875 | eventSourceRepository, |
| 876 | eq(eventSourceRepository.id |
| 877 | ) |
| 878 | .leftJoin( |
| 879 | eventSourceOwner, |
| 880 | eq(eventSourceOwner.id |
| 881 | ) |
| 882 | .leftJoin( |
| 883 | eventSourceOrganization, |
| 884 | eq(eventSourceOrganization.id |
| 885 | ) |
| 886 | .where(eq(schema |
| 887 | selectReviews( |
| 888 | this.db, |
| 889 | and( |
| 890 | eq(schema.pullRequest |
| 891 | isNotNull(schema.pullRequestReview |
| 892 | ), |
| 893 | ), |
| 894 | this.references.mentionsOf |
| 895 | issue.id, |
| 896 | params.requesterId ? |
| 897 | ), |
| 898 | ]); |
| 899 | |
| 900 | const timeline = |
| 901 | ...comments, |
| 902 | ...events, |
| 903 | ...reviews, |
| 904 | ...mentions.map( |
| 905 | kind: 'reference' as |
| 906 | ...mention, |
| 907 | })), |
| 908 | ].sort( |
| 909 | (a, b) |
| 910 | a.createdAt.localeCompare |
| 911 | ); |
| 912 | |
| 913 | return { timeline |
| 914 | } |
| 915 | |
| 916 | async listLabels(params |
| 917 | username: string |
| 918 | repo: string |
| 919 | requesterId?: string |
| 920 | }) { |
| 921 | const repository = |
| 922 | const labels = |
| 923 | .select(labelColumns) |
| 924 | .from(schema.label |
| 925 | .where(eq(schema |
| 926 | .orderBy(asc(schema |
| 927 | return { labels |
| 928 | } |
| 929 | |
| 930 | async createLabel(params |
| 931 | username: string |
| 932 | repo: string |
| 933 | requesterId: string |
| 934 | body: { |
| 935 | }) { |
| 936 | const repository = |
| 937 | ...params, |
| 938 | operation: 'write', |
| 939 | }); |
| 940 | const name = |
| 941 | const [existing] |
| 942 | .select({ id: |
| 943 | .from(schema.label |
| 944 | .where( |
| 945 | and( |
| 946 | eq(schema.label |
| 947 | eq(schema.label |
| 948 | ), |
| 949 | ); |
| 950 | if (existing) throw |
| 951 | |
| 952 | return this.db |
| 953 | const [created] |
| 954 | .insert(schema.label |
| 955 | .values({ |
| 956 | repositoryId: repository.id |
| 957 | name, |
| 958 | description: params.body |
| 959 | color: params.body |
| 960 | }) |
| 961 | .returning(labelColumns); |
| 962 | await publishEvent(tx |
| 963 | type: 'label.created', |
| 964 | repositoryId: repository.id |
| 965 | actorId: params.requesterId |
| 966 | payload: { labelId: created |
| 967 | }); |
| 968 | return created; |
| 969 | }); |
| 970 | } |
| 971 | |
| 972 | async updateLabel(params |
| 973 | username: string |
| 974 | repo: string |
| 975 | requesterId: string |
| 976 | labelId: string |
| 977 | body: { |
| 978 | }) { |
| 979 | const repository = |
| 980 | ...params, |
| 981 | operation: 'write', |
| 982 | }); |
| 983 | const [label] |
| 984 | .select() |
| 985 | .from(schema.label |
| 986 | .where( |
| 987 | and( |
| 988 | eq(schema.label |
| 989 | eq(schema.label |
| 990 | ), |
| 991 | ); |
| 992 | if (!label) |
| 993 | |
| 994 | if (params.body |
| 995 | const [clash] |
| 996 | .select({ id: |
| 997 | .from(schema.label |
| 998 | .where( |
| 999 | and( |
| 1000 | eq(schema.label |
| 1001 | eq(schema.label |
| 1002 | ), |
| 1003 | ); |
| 1004 | if (clash) throw |
| 1005 | } |
| 1006 | |
| 1007 | return this.db |
| 1008 | const [updated] |
| 1009 | .update(schema.label |
| 1010 | .set({ |
| 1011 | ...(params.body |
| 1012 | ? {} |
| 1013 | : { name: params |
| 1014 | ...(params.body |
| 1015 | ? {} |
| 1016 | : { description: params |
| 1017 | ...(params.body |
| 1018 | ? {} |
| 1019 | : { color: params |
| 1020 | }) |
| 1021 | .where(eq(schema |
| 1022 | .returning(labelColumns); |
| 1023 | if ( |
| 1024 | updated.name !== |
| 1025 | updated.description !== |
| 1026 | updated.color !== |
| 1027 | ) { |
| 1028 | await publishEvent(tx |
| 1029 | type: 'label.edited', |
| 1030 | repositoryId: repository.id |
| 1031 | actorId: params.requesterId |
| 1032 | payload: { labelId: label |
| 1033 | }); |
| 1034 | } |
| 1035 | return updated; |
| 1036 | }); |
| 1037 | } |
| 1038 | |
| 1039 | async deleteLabel(params |
| 1040 | username: string |
| 1041 | repo: string |
| 1042 | requesterId: string |
| 1043 | labelId: string |
| 1044 | }) { |
| 1045 | const repository = |
| 1046 | ...params, |
| 1047 | operation: 'write', |
| 1048 | }); |
| 1049 | const [label] |
| 1050 | .select() |
| 1051 | .from(schema.label |
| 1052 | .where( |
| 1053 | and( |
| 1054 | eq(schema.label |
| 1055 | eq(schema.label |
| 1056 | ), |
| 1057 | ); |
| 1058 | if (!label) |
| 1059 | await this.db |
| 1060 | await tx.delete |
| 1061 | await publishEvent(tx |
| 1062 | type: 'label.deleted', |
| 1063 | repositoryId: repository.id |
| 1064 | actorId: params.requesterId |
| 1065 | payload: { |
| 1066 | label: { |
| 1067 | id: label.id |
| 1068 | name: label.name |
| 1069 | description: label.description |
| 1070 | color: label.color |
| 1071 | }, |
| 1072 | }, |
| 1073 | }); |
| 1074 | }); |
| 1075 | return { deleted: |
| 1076 | } |
| 1077 | |
| 1078 | /** Full replacement of an issue's label set, like GitHub's sidebar. */ |
| 1079 | async setIssueLabels( |
| 1080 | params: IssueRef |
| 1081 | ) { |
| 1082 | const { issue, |
| 1083 | if (issue.authorId |
| 1084 | await this.access |
| 1085 | } |
| 1086 | |
| 1087 | const names = |
| 1088 | params.names.map |
| 1089 | ); |
| 1090 | const labels = |
| 1091 | |
| 1092 | const current = |
| 1093 | .select({ labelId: |
| 1094 | .from(schema.issueLabel |
| 1095 | .where(eq(schema |
| 1096 | const currentIds = |
| 1097 | const nextIds = |
| 1098 | |
| 1099 | const added = |
| 1100 | const removedIds = |
| 1101 | const removed = |
| 1102 | removedIds.length > 0 |
| 1103 | ? await this. |
| 1104 | .select({ id: |
| 1105 | .from(schema.label |
| 1106 | .where(inArray(schema |
| 1107 | : []; |
| 1108 | |
| 1109 | await this.db |
| 1110 | await tx |
| 1111 | .delete(schema.issueLabel |
| 1112 | .where(eq(schema |
| 1113 | if (labels.length |
| 1114 | await tx |
| 1115 | .insert(schema.issueLabel |
| 1116 | .values( |
| 1117 | labels.map((label |
| 1118 | ); |
| 1119 | } |
| 1120 | |
| 1121 | for (const label |
| 1122 | await this.recordEvent |
| 1123 | labelName: label.name |
| 1124 | }); |
| 1125 | await publishEvent(tx |
| 1126 | type: 'issue.unlabeled', |
| 1127 | repositoryId: base.id |
| 1128 | actorId: params.requesterId |
| 1129 | payload: { issueId: issue |
| 1130 | }); |
| 1131 | } |
| 1132 | for (const label |
| 1133 | await this.recordEvent |
| 1134 | labelName: label.name |
| 1135 | }); |
| 1136 | await publishEvent(tx |
| 1137 | type: 'issue.labeled', |
| 1138 | repositoryId: base.id |
| 1139 | actorId: params.requesterId |
| 1140 | payload: { issueId: issue |
| 1141 | }); |
| 1142 | } |
| 1143 | |
| 1144 | if (added.length |
| 1145 | await touchIssue(tx |
| 1146 | } |
| 1147 | }); |
| 1148 | |
| 1149 | return { labels |
| 1150 | } |
| 1151 | |
| 1152 | /** Full replacement of an issue's assignee set, like GitHub's sidebar. */ |
| 1153 | async setIssueAssignees( |
| 1154 | params: IssueRef |
| 1155 | ) { |
| 1156 | const { issue |
| 1157 | if (issue.authorId |
| 1158 | await this.access |
| 1159 | } |
| 1160 | |
| 1161 | const usernames = |
| 1162 | params.usernames.map |
| 1163 | ); |
| 1164 | const users = |
| 1165 | |
| 1166 | const current = |
| 1167 | .select({ userId: |
| 1168 | .from(schema.issueAssignee |
| 1169 | .where(eq(schema |
| 1170 | const currentIds = |
| 1171 | const nextIds = |
| 1172 | |
| 1173 | const added = |
| 1174 | const removedIds = |
| 1175 | const removed = |
| 1176 | removedIds.length > 0 |
| 1177 | ? await this. |
| 1178 | .select() |
| 1179 | .from(schema.user |
| 1180 | .where(inArray(schema |
| 1181 | : []; |
| 1182 | |
| 1183 | await this.db |
| 1184 | await tx |
| 1185 | .delete(schema.issueAssignee |
| 1186 | .where(eq(schema |
| 1187 | if (users.length |
| 1188 | await tx |
| 1189 | .insert(schema.issueAssignee |
| 1190 | .values( |
| 1191 | users.map((user |
| 1192 | ); |
| 1193 | } |
| 1194 | |
| 1195 | for (const user |
| 1196 | await this.recordEvent |
| 1197 | assigneeUsername: user.username |
| 1198 | }); |
| 1199 | await publishEvent(tx |
| 1200 | type: 'issue.unassigned', |
| 1201 | repositoryId: issue.repositoryId |
| 1202 | actorId: params.requesterId |
| 1203 | payload: { issueId: issue |
| 1204 | }); |
| 1205 | } |
| 1206 | for (const user |
| 1207 | await this.recordEvent |
| 1208 | assigneeUsername: user.username |
| 1209 | }); |
| 1210 | await publishEvent(tx |
| 1211 | type: 'issue.assigned', |
| 1212 | repositoryId: issue.repositoryId |
| 1213 | actorId: params.requesterId |
| 1214 | payload: { issueId: issue |
| 1215 | }); |
| 1216 | } |
| 1217 | |
| 1218 | if (added.length |
| 1219 | await touchIssue(tx |
| 1220 | } |
| 1221 | }); |
| 1222 | |
| 1223 | return { assignees: |
| 1224 | } |
| 1225 | |
| 1226 | async load({ |
| 1227 | const base = |
| 1228 | username, |
| 1229 | repo, |
| 1230 | requesterId, |
| 1231 | operation, |
| 1232 | }); |
| 1233 | const [issue] |
| 1234 | .select() |
| 1235 | .from(schema.issue |
| 1236 | .where( |
| 1237 | and( |
| 1238 | eq(schema.issue |
| 1239 | eq(schema.issue |
| 1240 | ), |
| 1241 | ); |
| 1242 | if (!issue) |
| 1243 | |
| 1244 | return { issue, |
| 1245 | } |
| 1246 | |
| 1247 | private async expandIssue |
| 1248 | issueRow: Issue |
| 1249 | requesterId: string |
| 1250 | viewerRole: Role |
| 1251 | ) { |
| 1252 | const [issue] |
| 1253 | [issueRow], |
| 1254 | requesterId, |
| 1255 | viewerRole, |
| 1256 | ); |
| 1257 | return issue; |
| 1258 | } |
| 1259 | |
| 1260 | private async expandIssues |
| 1261 | rows: Issue |
| 1262 | requesterId: string |
| 1263 | viewerRole: Role |
| 1264 | ) { |
| 1265 | if (rows.length |
| 1266 | const ids = |
| 1267 | const authorIds = |
| 1268 | const closerIds = |
| 1269 | ...new Set |
| 1270 | rows.map((row |
| 1271 | ), |
| 1272 | ]; |
| 1273 | |
| 1274 | const [authors, |
| 1275 | this.db |
| 1276 | .select({ id: |
| 1277 | .from(schema.user |
| 1278 | .where(inArray(schema |
| 1279 | closerIds.length > 0 |
| 1280 | ? this.db |
| 1281 | .select({ id: |
| 1282 | .from(schema.user |
| 1283 | .where(inArray(schema |
| 1284 | : Promise.resolve([] |
| 1285 | this.db |
| 1286 | .select({ issueId: |
| 1287 | .from(schema.issueLabel |
| 1288 | .innerJoin(schema.label |
| 1289 | .where(inArray(schema |
| 1290 | this.db |
| 1291 | .select({ |
| 1292 | issueId: schema.issueAssignee |
| 1293 | username: schema.user |
| 1294 | }) |
| 1295 | .from(schema.issueAssignee |
| 1296 | .innerJoin(schema.user |
| 1297 | .where(inArray(schema |
| 1298 | ]); |
| 1299 | |
| 1300 | const authorById = |
| 1301 | const closerById = |
| 1302 | const labelsByIssue = |
| 1303 | for (const { |
| 1304 | const list = |
| 1305 | list.push(label |
| 1306 | labelsByIssue.set(issueId |
| 1307 | } |
| 1308 | const assigneesByIssue = |
| 1309 | for (const row |
| 1310 | const list = |
| 1311 | list.push(row |
| 1312 | assigneesByIssue.set(row |
| 1313 | } |
| 1314 | |
| 1315 | return rows.map |
| 1316 | id: issueRow.id |
| 1317 | number: issueRow.number |
| 1318 | title: issueRow.title |
| 1319 | body: issueRow.body |
| 1320 | state: issueRow.state |
| 1321 | isPullRequest: issueRow.isPullRequest |
| 1322 | authorUsername: authorById.get |
| 1323 | closedByUsername: issueRow.closedById |
| 1324 | ? (closerById.get |
| 1325 | : null, |
| 1326 | labels: labelsByIssue.get |
| 1327 | assignees: assigneesByIssue.get |
| 1328 | commentCount: issueRow.commentCount |
| 1329 | closedAt: issueRow.closedAt |
| 1330 | createdAt: issueRow.createdAt |
| 1331 | updatedAt: issueRow.updatedAt |
| 1332 | viewerCanEdit: canEditThread(issueRow |
| 1333 | })); |
| 1334 | } |
| 1335 | |
| 1336 | private async recordEvent |
| 1337 | db: Executor |
| 1338 | issueId: string, |
| 1339 | actorId: string, |
| 1340 | type: IssueEventType |
| 1341 | extra: { |
| 1342 | labelName?: string |
| 1343 | assigneeUsername?: string |
| 1344 | oldTitle?: string |
| 1345 | newTitle?: string |
| 1346 | }, |
| 1347 | ) { |
| 1348 | await db.insert |
| 1349 | issueId, |
| 1350 | actorId, |
| 1351 | type, |
| 1352 | ...extra, |
| 1353 | }); |
| 1354 | } |
| 1355 | |
| 1356 | private labelsNamed(repositoryId |
| 1357 | return this.db |
| 1358 | .select(labelColumns) |
| 1359 | .from(schema.label |
| 1360 | .where( |
| 1361 | and( |
| 1362 | eq(schema.label |
| 1363 | inArray(schema.label |
| 1364 | ), |
| 1365 | ); |
| 1366 | } |
| 1367 | |
| 1368 | private async resolveLabels |
| 1369 | if (names.length |
| 1370 | const rows = |
| 1371 | const found = |
| 1372 | const missing = |
| 1373 | if (missing) throw |
| 1374 | return rows; |
| 1375 | } |
| 1376 | |
| 1377 | /** Ids of the labels named, or null when one does not exist, so the filter matches nothing. */ |
| 1378 | private async labelIdsForFilter |
| 1379 | if (names.length |
| 1380 | const rows = |
| 1381 | if (rows.length |
| 1382 | return rows.map |
| 1383 | } |
| 1384 | |
| 1385 | private async issueIdsWithAllLabels |
| 1386 | const rows = |
| 1387 | .select({ |
| 1388 | issueId: schema.issueLabel |
| 1389 | matched: sql<number |
| 1390 | }) |
| 1391 | .from(schema.issueLabel |
| 1392 | .where(inArray(schema |
| 1393 | .groupBy(schema.issueLabel |
| 1394 | .having( |
| 1395 | sql`count(distinct ${schema. |
| 1396 | ); |
| 1397 | return rows.map |
| 1398 | } |
| 1399 | |
| 1400 | private async resolveUsers |
| 1401 | if (usernames.length |
| 1402 | return Promise.all |
| 1403 | usernames.map((username |
| 1404 | ); |
| 1405 | } |
| 1406 | |
| 1407 | private async optionalUserId |
| 1408 | try { |
| 1409 | return (await this |
| 1410 | } catch (error) |
| 1411 | if (error instanceof |
| 1412 | throw error; |
| 1413 | } |
| 1414 | } |
| 1415 | } |
| 1416 | |
| 1417 | interface IssueRef { |
| 1418 | username: string; |
| 1419 | repo: string; |
| 1420 | number: number; |
| 1421 | requesterId?: string |
| 1422 | operation?: RepositoryOperation |
| 1423 | } |
| 1424 | |
| 1425 | // Drizzle wraps the driver error, so the constraint name is on `cause`. |
| 1426 | function isNumberCollision(error: |
| 1427 | if (typeof error |
| 1428 | if ((error as |
| 1429 | return true; |
| 1430 | return isNumberCollision((error |
| 1431 | } |
| 1432 | |
| 1433 | function dedupe(names: |
| 1434 | return [...new |
| 1435 | } |