| 1 | import type { Database } from '@ghost/db'; |
| 2 | import { Inject, Injectable |
| 3 | import { ConfigService } |
| 4 | |
| 5 | import { DATABASE } |
| 6 | import { CouldNotResolveError } |
| 7 | import type { Loaders |
| 8 | import { pullRequestRowsOf } |
| 9 | import { |
| 10 | toIssueCommentNode, |
| 11 | toIssueNode, |
| 12 | toLabelNode, |
| 13 | toPullRequestNode, |
| 14 | toUserNode, |
| 15 | type UserRow, |
| 16 | } from '../../../../lib/github/nodes.js'; |
| 17 | import { githubOrigins } |
| 18 | import { IssueNotFoundError } |
| 19 | import type { IssueDTO |
| 20 | import { IssuesService } |
| 21 | import type { GithubViewer |
| 22 | import { sliceConnection } |
| 23 | import type { IssueOrPullRequestFields |
| 24 | import { RepositoryNode } |
| 25 | |
| 26 | @Injectable() |
| 27 | export class IssueNodesService |
| 28 | constructor( |
| 29 | @Inject(DATABASE) |
| 30 | private readonly issues |
| 31 | private readonly config |
| 32 | ) {} |
| 33 | |
| 34 | /** The issue or pull request behind a number, for repository.issue, issueOrPullRequest, node(id:) and the mutations. */ |
| 35 | async fromRepository( |
| 36 | repository: RepositoryNode |
| 37 | number: number, |
| 38 | viewer: GithubViewer |
| 39 | ) { |
| 40 | try { |
| 41 | const issue = |
| 42 | username: repository.ownerLogin |
| 43 | repo: repository.slug |
| 44 | number, |
| 45 | requesterId: viewer?.userId |
| 46 | }); |
| 47 | const [node] |
| 48 | return node!; |
| 49 | } catch (error) |
| 50 | if (error instanceof |
| 51 | throw new CouldNotResolveError( |
| 52 | `Could not resolve to an issue or pull request with the number of ${number}.` |
| 53 | ); |
| 54 | throw error; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /** Issue or pull request nodes in the order given; the pull_request rows behind them are read in one query. */ |
| 59 | async toNodes(issues |
| 60 | const pulls = |
| 61 | this.db, |
| 62 | issues.filter((issue |
| 63 | ); |
| 64 | return issues.map |
| 65 | const pull = |
| 66 | return pull |
| 67 | ? toPullRequestNode(issue, |
| 68 | : toIssueNode(issue, |
| 69 | }); |
| 70 | } |
| 71 | |
| 72 | /** The author of an issue, pull request, comment or review. */ |
| 73 | async author({ |
| 74 | const row = |
| 75 | return row ? |
| 76 | } |
| 77 | |
| 78 | async assignees( |
| 79 | issue: IssueOrPullRequestFields |
| 80 | loaders: Loaders |
| 81 | first?: number |
| 82 | ) { |
| 83 | const rows = |
| 84 | await loaders.usersByLogin |
| 85 | ).filter((row |
| 86 | return sliceConnection( |
| 87 | rows.map((row |
| 88 | { first }, |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | labels(issue: |
| 93 | const repository = |
| 94 | url: issue.repositoryUrl |
| 95 | resourcePath: issue.repositoryResourcePath |
| 96 | }; |
| 97 | return sliceConnection( |
| 98 | issue.labelDtos.map |
| 99 | { first }, |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | async comments( |
| 104 | issue: IssueOrPullRequestFields |
| 105 | viewer: GithubViewer |
| 106 | loaders: Loaders |
| 107 | first?: number |
| 108 | last?: number |
| 109 | after?: string |
| 110 | ) { |
| 111 | const { comments |
| 112 | username: issue.ownerLogin |
| 113 | repo: issue.repoSlug |
| 114 | number: issue.number |
| 115 | requesterId: viewer?.userId |
| 116 | }); |
| 117 | const viewerLogin = |
| 118 | ? ((await loaders |
| 119 | : null; |
| 120 | return sliceConnection( |
| 121 | comments.map((comment |
| 122 | toIssueCommentNode(comment, issue |
| 123 | ), |
| 124 | { first, last |
| 125 | ); |
| 126 | } |
| 127 | |
| 128 | /** IssueDTO leaves the numeric id out, as Ghost's own API does not return it. */ |
| 129 | databaseId(issue: |
| 130 | return loaders.issueNumericIds |
| 131 | } |
| 132 | } |