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