| 1 | import { Injectable } from '@nestjs/common'; |
| 2 | import { ConfigService } |
| 3 | |
| 4 | import { orNull } |
| 5 | import type { Loaders |
| 6 | import { encodeNodeId } |
| 7 | import { |
| 8 | toCommitNode, |
| 9 | toPullRequestReviewNode, |
| 10 | toRefNode, |
| 11 | toUserNode, |
| 12 | } from '../../../../lib/github/nodes.js'; |
| 13 | import { githubOrigins } |
| 14 | import { PullRequestsService } |
| 15 | import type { GithubViewer |
| 16 | import { sliceConnection } |
| 17 | import { |
| 18 | MergeableState, |
| 19 | MergeStateStatus, |
| 20 | PatchStatus, |
| 21 | PullRequestReviewDecision, |
| 22 | PullRequestReviewState, |
| 23 | PullRequestState, |
| 24 | } from '../../enums.js'; |
| 25 | import { |
| 26 | GitActorNode, |
| 27 | PullRequestNode, |
| 28 | PullRequestReviewNode, |
| 29 | } from '../../types/pull-request.type.js'; |
| 30 | import { RepositoryNodesService } |
| 31 | |
| 32 | // ponytail: a pull request's commits are read in one page of this many; gh asks for 100 at most. |
| 33 | const MAX_COMMITS = 250 |
| 34 | |
| 35 | const CHANGE_TYPES: Record |
| 36 | A: PatchStatus.ADDED |
| 37 | D: PatchStatus.DELETED |
| 38 | M: PatchStatus.MODIFIED |
| 39 | T: PatchStatus.CHANGED |
| 40 | }; |
| 41 | |
| 42 | @Injectable() |
| 43 | export class PullRequestNodesService |
| 44 | constructor( |
| 45 | private readonly repositories |
| 46 | private readonly pulls |
| 47 | private readonly config |
| 48 | ) {} |
| 49 | |
| 50 | /** getPullRequest once per node: additions, mergeable and the live head all come from the same git reads. */ |
| 51 | private detail(pull |
| 52 | pull.detail ??= |
| 53 | username: pull.ownerLogin |
| 54 | repo: pull.repoSlug |
| 55 | number: pull.number |
| 56 | requesterId: viewer?.userId |
| 57 | }); |
| 58 | return pull.detail |
| 59 | } |
| 60 | |
| 61 | headRef(pull: |
| 62 | return pull.headRepositoryGhostId |
| 63 | ? toRefNode(pull. |
| 64 | : null; |
| 65 | } |
| 66 | |
| 67 | /** The branch as it stands while the request is open; what was merged, or last recorded, once it is not. */ |
| 68 | async headRefOid(pull |
| 69 | if (pull.state |
| 70 | return ( |
| 71 | (await this.pulls |
| 72 | pull.headRepositoryGhostId, |
| 73 | pull.headRefName, |
| 74 | )) ?? pull.storedHeadSha |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | baseRepository(pull: |
| 79 | return this.repositories |
| 80 | } |
| 81 | |
| 82 | /** Null once the head repository is deleted, or when the viewer cannot read it, as on GitHub. */ |
| 83 | headRepository(pull: |
| 84 | return pull.headRepositoryGhostId |
| 85 | ? orNull(this. |
| 86 | : null; |
| 87 | } |
| 88 | |
| 89 | async headRepositoryOwner( |
| 90 | pull: PullRequestNode |
| 91 | viewer: GithubViewer |
| 92 | loaders: Loaders |
| 93 | ) { |
| 94 | const head = |
| 95 | return head ? |
| 96 | } |
| 97 | |
| 98 | async additions(pull |
| 99 | return (await this |
| 100 | } |
| 101 | |
| 102 | async deletions(pull |
| 103 | return (await this |
| 104 | } |
| 105 | |
| 106 | async changedFiles(pull |
| 107 | return (await this |
| 108 | } |
| 109 | |
| 110 | /** GitHub answers UNKNOWN for anything not open, and for a head with nothing to merge. */ |
| 111 | async mergeable(pull |
| 112 | if (pull.state |
| 113 | const detail = |
| 114 | if (detail.conflicts |
| 115 | return detail.mergeBase |
| 116 | ? MergeableState.MERGEABLE |
| 117 | : MergeableState.UNKNOWN; |
| 118 | } |
| 119 | |
| 120 | async mergeStateStatus(pull |
| 121 | if (pull.state |
| 122 | return MergeStateStatus.DRAFT |
| 123 | const mergeable = |
| 124 | return mergeable === |
| 125 | ? MergeStateStatus.CLEAN |
| 126 | : mergeable === MergeableState |
| 127 | ? MergeStateStatus.DIRTY |
| 128 | : MergeStateStatus.UNKNOWN; |
| 129 | } |
| 130 | |
| 131 | /** Ghost never requires a review, so there is no REVIEW_REQUIRED: a request with no standing verdict has no decision. */ |
| 132 | async reviewDecision(pull |
| 133 | const verdicts = |
| 134 | .map((review) |
| 135 | .filter( |
| 136 | (state) => |
| 137 | state === PullRequestReviewState. |
| 138 | state === PullRequestReviewState. |
| 139 | ); |
| 140 | if (verdicts.includes |
| 141 | return PullRequestReviewDecision.CHANGES_REQUESTED |
| 142 | if (verdicts.includes |
| 143 | return PullRequestReviewDecision.APPROVED |
| 144 | return null; |
| 145 | } |
| 146 | |
| 147 | async reviews( |
| 148 | pull: PullRequestNode |
| 149 | loaders: Loaders |
| 150 | first?: number |
| 151 | last?: number |
| 152 | after?: string |
| 153 | ) { |
| 154 | const rows = |
| 155 | pull.pullRequestGhostId, |
| 156 | ); |
| 157 | return sliceConnection( |
| 158 | rows.map((row |
| 159 | { first, last |
| 160 | ); |
| 161 | } |
| 162 | |
| 163 | /** Each reviewer's most recent review, in the order they were submitted. */ |
| 164 | async latestReviews( |
| 165 | pull: PullRequestNode |
| 166 | loaders: Loaders |
| 167 | first?: number |
| 168 | last?: number |
| 169 | after?: string |
| 170 | ) { |
| 171 | const rows = |
| 172 | pull.pullRequestGhostId, |
| 173 | ); |
| 174 | const latest = |
| 175 | return sliceConnection( |
| 176 | rows |
| 177 | .filter((row) |
| 178 | .map((row) |
| 179 | { first, last |
| 180 | ); |
| 181 | } |
| 182 | |
| 183 | async files( |
| 184 | pull: PullRequestNode |
| 185 | viewer: GithubViewer |
| 186 | first?: number |
| 187 | after?: string |
| 188 | ) { |
| 189 | const { files |
| 190 | username: pull.ownerLogin |
| 191 | repo: pull.repoSlug |
| 192 | number: pull.number |
| 193 | requesterId: viewer?.userId |
| 194 | }); |
| 195 | return sliceConnection( |
| 196 | files.map((file |
| 197 | path: file.path |
| 198 | additions: file.additions |
| 199 | deletions: file.deletions |
| 200 | changeType: CHANGE_TYPES[file |
| 201 | })), |
| 202 | { first, after |
| 203 | ); |
| 204 | } |
| 205 | |
| 206 | /** Oldest first, as GitHub lists them; gh reads the last one for the head's checks. */ |
| 207 | async commits( |
| 208 | pull: PullRequestNode |
| 209 | viewer: GithubViewer |
| 210 | first?: number |
| 211 | last?: number |
| 212 | after?: string |
| 213 | ) { |
| 214 | const { commits, |
| 215 | username: pull.ownerLogin |
| 216 | repo: pull.repoSlug |
| 217 | number: pull.number |
| 218 | requesterId: viewer?.userId |
| 219 | limit: MAX_COMMITS, |
| 220 | oldestFirst: true, |
| 221 | }); |
| 222 | const repository = |
| 223 | ghostId: pull.repositoryGhostId |
| 224 | url: pull.repositoryUrl |
| 225 | resourcePath: pull.repositoryResourcePath |
| 226 | }; |
| 227 | const nodes = |
| 228 | id: encodeNodeId('PullRequestCommit |
| 229 | url: `${pull. |
| 230 | resourcePath: `${pull. |
| 231 | commit: toCommitNode(commit |
| 232 | })); |
| 233 | return { |
| 234 | ...sliceConnection(nodes |
| 235 | totalCount: total, |
| 236 | }; |
| 237 | } |
| 238 | |
| 239 | /** The account behind a commit's author or committer email, as GitHub links them. */ |
| 240 | async actorUser(actor |
| 241 | if (!actor.email |
| 242 | const row = |
| 243 | return row ? |
| 244 | } |
| 245 | |
| 246 | /** The head the reviewer read, from git; null once no repository holds it. */ |
| 247 | async reviewCommit( |
| 248 | review: PullRequestReviewNode |
| 249 | viewer: GithubViewer |
| 250 | ) { |
| 251 | const { pullRequest: |
| 252 | const commit = |
| 253 | { |
| 254 | username: pull.ownerLogin |
| 255 | repo: pull.repoSlug |
| 256 | number: pull.number |
| 257 | requesterId: viewer?.userId |
| 258 | }, |
| 259 | review.commitSha, |
| 260 | ); |
| 261 | return ( |
| 262 | commit && |
| 263 | toCommitNode(commit, { |
| 264 | ghostId: pull.repositoryGhostId |
| 265 | url: pull.repositoryUrl |
| 266 | resourcePath: pull.repositoryResourcePath |
| 267 | }) |
| 268 | ); |
| 269 | } |
| 270 | } |