| 1 | import { type Database, schema } from '@ghost/db'; |
| 2 | import { Inject, Injectable |
| 3 | import { ConfigService } |
| 4 | import { eq, sql |
| 5 | |
| 6 | import { DATABASE } |
| 7 | import { |
| 8 | authorizeOrNotFound, |
| 9 | orNull, |
| 10 | } from '../../../../lib/github/authorize.js'; |
| 11 | import { CouldNotResolveError } |
| 12 | import type { Loaders |
| 13 | import { encodeNodeId } |
| 14 | import { |
| 15 | toIssueNode, |
| 16 | toLabelNode, |
| 17 | toOrganizationNode, |
| 18 | toRepositoryNode, |
| 19 | toUserNode, |
| 20 | type UserRow, |
| 21 | } from '../../../../lib/github/nodes.js'; |
| 22 | import { githubOrigins } |
| 23 | import { hasScope } |
| 24 | import { oauthAppBlocked } |
| 25 | import type { AuthorizedRepository |
| 26 | import { ownerNameOf } |
| 27 | import { IssuesService } |
| 28 | import { RepositoryAccessService } |
| 29 | import type { GithubViewer |
| 30 | import { issueCursor, sliceConnection |
| 31 | import { |
| 32 | IssueOrderField, |
| 33 | IssueState, |
| 34 | LabelOrderField, |
| 35 | OrderDirection, |
| 36 | PullRequestState, |
| 37 | } from '../../enums.js'; |
| 38 | import { IssueFilters, IssueOrder |
| 39 | import { PullRequestNode } |
| 40 | import { RepositoryNode } |
| 41 | import { IssueNodesService } |
| 42 | |
| 43 | @Injectable() |
| 44 | export class RepositoryNodesService |
| 45 | constructor( |
| 46 | @Inject(DATABASE) |
| 47 | private readonly access |
| 48 | private readonly config |
| 49 | private readonly issuesService |
| 50 | private readonly issueNodes |
| 51 | ) {} |
| 52 | |
| 53 | async repository(owner |
| 54 | const row = |
| 55 | owner, |
| 56 | name, |
| 57 | requesterId: viewer?.userId |
| 58 | }); |
| 59 | await this.refuseUnreachable |
| 60 | row, |
| 61 | viewer, |
| 62 | `Could not resolve to a Repository with the name '${owner}/${ |
| 63 | ); |
| 64 | return this.toNode |
| 65 | } |
| 66 | |
| 67 | /** RepositoryOwner.repository(name:): a repository the viewer cannot read answers null, as on GitHub. */ |
| 68 | ownedRepository(login: |
| 69 | return this.repository |
| 70 | if (error instanceof |
| 71 | throw error; |
| 72 | }); |
| 73 | } |
| 74 | |
| 75 | /** The node for a repository already authorized, for other resolvers (issue.repository, node(id:)). */ |
| 76 | async load(repositoryId |
| 77 | const row = |
| 78 | repositoryId, |
| 79 | requesterId: viewer?.userId |
| 80 | }); |
| 81 | await this.refuseUnreachable |
| 82 | row, |
| 83 | viewer, |
| 84 | `Could not resolve to a node with the global id of '${encodeNodeId('Repository |
| 85 | ); |
| 86 | return this.toNode |
| 87 | } |
| 88 | |
| 89 | /** What the viewer may read, the token can still miss: a token without `repo` cannot see private repositories at all, and an OAuth app an organization has not approved cannot see the organization's repositories. Both read as missing, not forbidden, as on GitHub. */ |
| 90 | private async refuseUnreachable |
| 91 | row: AuthorizedRepository |
| 92 | viewer: GithubViewer |
| 93 | message: string, |
| 94 | ) { |
| 95 | if (!viewer) |
| 96 | if ( |
| 97 | (row.visibility === |
| 98 | (viewer.oauthClientId && |
| 99 | row.organizationId && |
| 100 | (await oauthAppBlocked(this |
| 101 | clientId: viewer.oauthClientId |
| 102 | organizationId: row.organizationId |
| 103 | }))) |
| 104 | ) |
| 105 | throw new CouldNotResolveError( |
| 106 | } |
| 107 | |
| 108 | async owner(repository |
| 109 | const origins = |
| 110 | if (repository.organizationGhostId |
| 111 | const [organization] |
| 112 | .select() |
| 113 | .from(schema.organization |
| 114 | .where(eq(schema |
| 115 | return toOrganizationNode(organization |
| 116 | } |
| 117 | return toUserNode( |
| 118 | (await loaders.usersById |
| 119 | origins, |
| 120 | ); |
| 121 | } |
| 122 | |
| 123 | async parent(repository |
| 124 | if (!repository.parentGhostId |
| 125 | // A parent the viewer cannot read reads as no parent, as on GitHub. |
| 126 | return orNull(this |
| 127 | } |
| 128 | |
| 129 | private async toNode |
| 130 | repositoryId: string, |
| 131 | row: Awaited |
| 132 | ) { |
| 133 | const [owner] |
| 134 | .select({ login: |
| 135 | .from(schema.repository |
| 136 | .innerJoin(schema.user |
| 137 | .leftJoin( |
| 138 | schema.organization, |
| 139 | eq(schema.organization |
| 140 | ) |
| 141 | .where(eq(schema |
| 142 | return toRepositoryNode(row |
| 143 | } |
| 144 | |
| 145 | async labels( |
| 146 | repository: RepositoryNode |
| 147 | viewer: GithubViewer |
| 148 | first?: number |
| 149 | after?: string |
| 150 | query?: string |
| 151 | orderBy?: LabelOrder |
| 152 | ) { |
| 153 | // listLabels answers by name, ascending. |
| 154 | const { labels |
| 155 | username: repository.ownerLogin |
| 156 | repo: repository.slug |
| 157 | requesterId: viewer?.userId |
| 158 | }); |
| 159 | const matching = |
| 160 | ? labels.filter( |
| 161 | label.name.toLowerCase |
| 162 | ) |
| 163 | : labels; |
| 164 | const ordered = |
| 165 | orderBy?.field === |
| 166 | ? matching.toSorted( |
| 167 | : matching; |
| 168 | const directed = |
| 169 | orderBy?.direction === |
| 170 | ? ordered.toReversed() |
| 171 | : ordered; |
| 172 | return sliceConnection( |
| 173 | directed.map((label |
| 174 | { first, after |
| 175 | ); |
| 176 | } |
| 177 | |
| 178 | async label( |
| 179 | repository: RepositoryNode |
| 180 | name: string, |
| 181 | viewer: GithubViewer |
| 182 | ) { |
| 183 | const { labels |
| 184 | username: repository.ownerLogin |
| 185 | repo: repository.slug |
| 186 | requesterId: viewer?.userId |
| 187 | }); |
| 188 | const label = |
| 189 | (candidate) => |
| 190 | ); |
| 191 | return label ? |
| 192 | } |
| 193 | |
| 194 | async assignableUsers( |
| 195 | repository: RepositoryNode |
| 196 | loaders: Loaders |
| 197 | first?: number |
| 198 | after?: string |
| 199 | query?: string |
| 200 | ) { |
| 201 | const ids = |
| 202 | const rows = |
| 203 | (row): row |
| 204 | ); |
| 205 | const matching = |
| 206 | ? rows.filter( |
| 207 | `${row.username} |
| 208 | .toLowerCase() |
| 209 | .includes(query.toLowerCase |
| 210 | ) |
| 211 | : rows; |
| 212 | // Sorted so the offset cursors sliceConnection hands out name the same users on every page. |
| 213 | const sorted = |
| 214 | (a.username ?? |
| 215 | ); |
| 216 | return sliceConnection( |
| 217 | sorted.map((row |
| 218 | { first, after |
| 219 | ); |
| 220 | } |
| 221 | |
| 222 | async issues( |
| 223 | repository: RepositoryNode |
| 224 | viewer: GithubViewer |
| 225 | first?: number |
| 226 | after?: string |
| 227 | states?: IssueState |
| 228 | labels?: string |
| 229 | orderBy?: IssueOrder |
| 230 | filterBy?: IssueFilters |
| 231 | ) { |
| 232 | const wanted = |
| 233 | const state = |
| 234 | wanted.size === |
| 235 | ? wanted.has( |
| 236 | ? 'open' |
| 237 | : 'closed' |
| 238 | : 'all'; |
| 239 | const sort = |
| 240 | orderBy?.field === |
| 241 | ? 'updated' |
| 242 | : orderBy?.field |
| 243 | ? 'comments' |
| 244 | : 'created'; |
| 245 | const page = |
| 246 | username: repository.ownerLogin |
| 247 | repo: repository.slug |
| 248 | requesterId: viewer?.userId |
| 249 | query: { |
| 250 | state, |
| 251 | sort, |
| 252 | direction: orderBy?.direction |
| 253 | cursor: after, |
| 254 | // The controller's ValidationPipe caps limit at 100; GraphQL arguments skip it. |
| 255 | limit: Math.min |
| 256 | assignee: filterBy?.assignee |
| 257 | author: filterBy?.createdBy |
| 258 | labels: (labels ?? |
| 259 | }, |
| 260 | }); |
| 261 | return { |
| 262 | nodes: page.issues |
| 263 | totalCount: |
| 264 | state === 'open' |
| 265 | ? page.openCount |
| 266 | : state === ' |
| 267 | ? page.closedCount |
| 268 | : page.total, |
| 269 | pageInfo: { |
| 270 | hasNextPage: page.hasMore |
| 271 | hasPreviousPage: !!after, |
| 272 | startCursor: null, |
| 273 | endCursor: page.nextCursor |
| 274 | }, |
| 275 | }; |
| 276 | } |
| 277 | |
| 278 | async issue( |
| 279 | repository: RepositoryNode |
| 280 | number: number, |
| 281 | viewer: GithubViewer |
| 282 | ) { |
| 283 | const found = |
| 284 | .fromRepository(repository, |
| 285 | .catch((error: |
| 286 | if (error instanceof |
| 287 | throw error; |
| 288 | }); |
| 289 | if (!found || |
| 290 | throw new CouldNotResolveError( |
| 291 | `Could not resolve to an Issue with the number of ${number}.` |
| 292 | ); |
| 293 | return found; |
| 294 | } |
| 295 | |
| 296 | async pullRequests( |
| 297 | repository: RepositoryNode |
| 298 | viewer: GithubViewer |
| 299 | first?: number |
| 300 | after?: string |
| 301 | states?: PullRequestState |
| 302 | labels?: string |
| 303 | headRefName?: string |
| 304 | baseRefName?: string |
| 305 | orderBy?: IssueOrder |
| 306 | ) { |
| 307 | const sort = |
| 308 | orderBy?.field === |
| 309 | const page = |
| 310 | { |
| 311 | username: repository.ownerLogin |
| 312 | repo: repository.slug |
| 313 | requesterId: viewer?.userId |
| 314 | query: { |
| 315 | state: 'all', |
| 316 | sort, |
| 317 | direction: orderBy?.direction |
| 318 | cursor: after, |
| 319 | // The controller's ValidationPipe caps limit at 100; GraphQL arguments skip it. |
| 320 | limit: Math.min |
| 321 | labels: labels?.join |
| 322 | }, |
| 323 | }, |
| 324 | { |
| 325 | pullRequests: { |
| 326 | states: states?.map |
| 327 | (state) => |
| 328 | ), |
| 329 | headRef: headRefName, |
| 330 | baseRef: baseRefName, |
| 331 | }, |
| 332 | }, |
| 333 | ); |
| 334 | const nodes = |
| 335 | return { |
| 336 | nodes, |
| 337 | edges: nodes.map |
| 338 | node, |
| 339 | cursor: issueCursor(page |
| 340 | })), |
| 341 | totalCount: page.total |
| 342 | pageInfo: { |
| 343 | hasNextPage: page.hasMore |
| 344 | hasPreviousPage: !!after, |
| 345 | startCursor: null, |
| 346 | endCursor: page.nextCursor |
| 347 | }, |
| 348 | }; |
| 349 | } |
| 350 | |
| 351 | async pullRequest( |
| 352 | repository: RepositoryNode |
| 353 | number: number, |
| 354 | viewer: GithubViewer |
| 355 | ) { |
| 356 | const found = |
| 357 | this.issueNodes.fromRepository |
| 358 | ); |
| 359 | if (!(found |
| 360 | throw new CouldNotResolveError( |
| 361 | `Could not resolve to a PullRequest with the number of ${number}.` |
| 362 | ); |
| 363 | return found; |
| 364 | } |
| 365 | |
| 366 | issueOrPullRequest( |
| 367 | repository: RepositoryNode |
| 368 | number: number, |
| 369 | viewer: GithubViewer |
| 370 | ) { |
| 371 | return this.issueNodes |
| 372 | } |
| 373 | |
| 374 | /** Who gh offers as assignees: the owner, accepted collaborators and organization members, as the assignee picker's involved set. */ |
| 375 | private async assignableUserIds |
| 376 | const rows = |
| 377 | select owner_id as id from repository where id = ${repositoryId} |
| 378 | union select user_id from repository_collaborator where repository_id = ${repositoryId} and accepted_at is not null |
| 379 | union select m.user_id from member m join repository r on r.organization_id = m.organization_id where r.id = ${repositoryId} |
| 380 | `); |
| 381 | return rows.rows |
| 382 | } |
| 383 | } |