| 1 | import type { RepositoryNode } from '../../github/graphql/types/repository.type.js'; |
| 2 | import type { IssueDTO |
| 3 | import type { LabelDTO |
| 4 | import type { |
| 5 | ReleaseAssetDTO, |
| 6 | ReleaseDTO, |
| 7 | } from '../../resources/releases/dto/release.dto.js'; |
| 8 | import { GithubValidationError } |
| 9 | import { encodeNodeId } |
| 10 | import type { ReleaseAssetExtras |
| 11 | import type { GithubOrigins |
| 12 | |
| 13 | export type RestUserSource |
| 14 | ghostId: string |
| 15 | numericId: number |
| 16 | login: string |
| 17 | avatarUrl: string |
| 18 | }; |
| 19 | |
| 20 | // Mapper: GitHub's simple-user is the same few columns plus URL templates typed SDKs expect, whichever query found the user. |
| 21 | export function toUserRest( |
| 22 | user: RestUserSource |
| 23 | { api, web |
| 24 | type: 'User |
| 25 | ) { |
| 26 | const url = |
| 27 | return { |
| 28 | login: user.login |
| 29 | id: user.numericId |
| 30 | node_id: encodeNodeId(type |
| 31 | avatar_url: user.avatarUrl |
| 32 | gravatar_id: '', |
| 33 | url, |
| 34 | html_url: `${web} |
| 35 | followers_url: `${url} |
| 36 | following_url: `${url} |
| 37 | gists_url: `${url} |
| 38 | starred_url: `${url} |
| 39 | subscriptions_url: `${url} |
| 40 | organizations_url: `${url} |
| 41 | repos_url: `${url} |
| 42 | events_url: `${url} |
| 43 | received_events_url: `${url} |
| 44 | type, |
| 45 | site_admin: false, |
| 46 | }; |
| 47 | } |
| 48 | |
| 49 | export type RestUser |
| 50 | |
| 51 | // Mapper: the repository node GraphQL already built, with its permission, in GitHub's REST shape. |
| 52 | export function toRepositoryRest( |
| 53 | node: RepositoryNode |
| 54 | { api, web |
| 55 | ) { |
| 56 | return { |
| 57 | id: node.databaseId |
| 58 | node_id: node.id |
| 59 | name: node.name |
| 60 | full_name: node.nameWithOwner |
| 61 | private: node.isPrivate |
| 62 | visibility: node.isPrivate |
| 63 | owner: { login: node |
| 64 | html_url: node.url |
| 65 | description: node.description |
| 66 | fork: node.isFork |
| 67 | url: `${api} |
| 68 | clone_url: `${api} |
| 69 | ssh_url: node.sshUrl |
| 70 | default_branch: node.defaultBranch |
| 71 | created_at: node.createdAt |
| 72 | updated_at: node.updatedAt |
| 73 | pushed_at: node.pushedAt |
| 74 | has_issues: node.hasIssuesEnabled |
| 75 | permissions: node.viewerPermission |
| 76 | admin: node.viewerPermission |
| 77 | maintain: ['ADMIN', |
| 78 | push: ['ADMIN', |
| 79 | triage: node.viewerPermission |
| 80 | pull: true, |
| 81 | }, |
| 82 | }; |
| 83 | } |
| 84 | |
| 85 | /** What every issue, comment and label response links to: the repository's API URL and its page in the web app. */ |
| 86 | export type RestRepository |
| 87 | ownerLogin: string |
| 88 | apiUrl: string |
| 89 | webUrl: string |
| 90 | }; |
| 91 | |
| 92 | /** The ids and users an issue, comment or label response embeds, loaded once per response. */ |
| 93 | export type RestContext |
| 94 | repository: RestRepository |
| 95 | users: Map |
| 96 | labelIds: Map |
| 97 | }; |
| 98 | |
| 99 | // Mapper: labels come from IssuesService in Ghost's shape; GitHub's adds the numeric id and URLs. |
| 100 | export function toLabelRest(label |
| 101 | return { |
| 102 | id: context.labelIds |
| 103 | node_id: encodeNodeId('Label |
| 104 | url: `${context. |
| 105 | name: label.name |
| 106 | color: label.color |
| 107 | description: label.description |
| 108 | default: false, |
| 109 | archived_at: null, |
| 110 | archived_by: null, |
| 111 | }; |
| 112 | } |
| 113 | |
| 114 | /** The issue columns IssueDTO leaves out: its numeric id, and for a pull request whether it is a draft and when it merged. */ |
| 115 | export type IssueExtras |
| 116 | numericId: number |
| 117 | draft: boolean |
| 118 | mergedAt: Date |
| 119 | }; |
| 120 | |
| 121 | function authorAssociation(login: |
| 122 | // ponytail: OWNER or NONE only, as in GraphQL; MEMBER and COLLABORATOR need a role lookup per author. |
| 123 | return login === |
| 124 | } |
| 125 | |
| 126 | // Mapper: IssuesService expands authors, labels and assignees for Ghost's API; GitHub's shape embeds full users and labels and adds URLs. |
| 127 | export function toIssueRest( |
| 128 | issue: IssueDTO |
| 129 | extras: IssueExtras |
| 130 | context: RestContext |
| 131 | ) { |
| 132 | const { apiUrl, |
| 133 | const url = |
| 134 | const htmlUrl = |
| 135 | const user = |
| 136 | (login && context |
| 137 | const assignees = |
| 138 | const closed = |
| 139 | return { |
| 140 | id: extras.numericId |
| 141 | node_id: encodeNodeId( |
| 142 | issue.isPullRequest ? |
| 143 | issue.id, |
| 144 | ), |
| 145 | url, |
| 146 | repository_url: apiUrl, |
| 147 | labels_url: `${url} |
| 148 | comments_url: `${url} |
| 149 | events_url: `${url} |
| 150 | timeline_url: `${url} |
| 151 | html_url: htmlUrl, |
| 152 | number: issue.number |
| 153 | state: issue.state |
| 154 | // ponytail: Ghost records no close reason; every close reads as completed until it does. |
| 155 | state_reason: closed ? |
| 156 | title: issue.title |
| 157 | body: issue.body |
| 158 | user: user(issue |
| 159 | labels: issue.labels |
| 160 | assignee: assignees[0] |
| 161 | assignees, |
| 162 | milestone: null, |
| 163 | locked: false, |
| 164 | active_lock_reason: null, |
| 165 | comments: issue.commentCount |
| 166 | closed_at: issue.closedAt |
| 167 | closed_by: user(issue |
| 168 | created_at: issue.createdAt |
| 169 | updated_at: issue.updatedAt |
| 170 | author_association: authorAssociation(issue |
| 171 | ...(issue.isPullRequest |
| 172 | draft: extras.draft |
| 173 | pull_request: { |
| 174 | url: `${apiUrl} |
| 175 | html_url: htmlUrl, |
| 176 | diff_url: `${htmlUrl} |
| 177 | patch_url: `${htmlUrl} |
| 178 | merged_at: extras.mergedAt |
| 179 | }, |
| 180 | }), |
| 181 | }; |
| 182 | } |
| 183 | |
| 184 | export type RestCommentSource |
| 185 | id: string |
| 186 | numericId: number |
| 187 | body: string |
| 188 | createdAt: string |
| 189 | updatedAt: string |
| 190 | authorUsername: string |
| 191 | issueNumber: number |
| 192 | isPullRequest: boolean |
| 193 | }; |
| 194 | |
| 195 | // Mapper: comments come from IssuesService in Ghost's shape; GitHub's embeds the author and adds URLs. |
| 196 | export function toCommentRest( |
| 197 | comment: RestCommentSource |
| 198 | context: RestContext |
| 199 | ) { |
| 200 | const { apiUrl, |
| 201 | const path = |
| 202 | return { |
| 203 | id: comment.numericId |
| 204 | node_id: encodeNodeId('IssueComment |
| 205 | url: `${apiUrl} |
| 206 | // The web app anchors comments by Ghost's id, so the link opens on the comment. |
| 207 | html_url: `${webUrl} |
| 208 | issue_url: `${apiUrl} |
| 209 | body: comment.body |
| 210 | user: context.users |
| 211 | created_at: comment.createdAt |
| 212 | updated_at: comment.updatedAt |
| 213 | author_association: authorAssociation(comment |
| 214 | }; |
| 215 | } |
| 216 | |
| 217 | export type Page |
| 218 | |
| 219 | /** One page of a REST list and how many items the whole list holds; GithubRestInterceptor answers `items` and links the other pages. */ |
| 220 | export class RestPage |
| 221 | constructor( |
| 222 | readonly items: |
| 223 | readonly page: |
| 224 | readonly total: |
| 225 | ) {} |
| 226 | } |
| 227 | |
| 228 | function positiveInteger(value: |
| 229 | const number = |
| 230 | return Number.isInteger |
| 231 | } |
| 232 | |
| 233 | /** GitHub's `page` and `per_page`: 1 and 30 when missing or not a positive integer, `per_page` capped at 100. */ |
| 234 | export function pageOf(query |
| 235 | const page = |
| 236 | const perPage = |
| 237 | return { page, |
| 238 | } |
| 239 | |
| 240 | /** GitHub's `Link` header for a numbered page of `total` items, or null when everything fits on one page. */ |
| 241 | export function linkHeader(url |
| 242 | const last = |
| 243 | const link = |
| 244 | const target = |
| 245 | target.searchParams.set |
| 246 | target.searchParams.set |
| 247 | return `<${target |
| 248 | }; |
| 249 | const links = |
| 250 | ...(page > |
| 251 | ...(page < |
| 252 | ...(page > |
| 253 | ]; |
| 254 | return links.length |
| 255 | } |
| 256 | |
| 257 | function labelName(value: |
| 258 | if (typeof value |
| 259 | if ( |
| 260 | typeof value === ' |
| 261 | value !== null && |
| 262 | 'name' in value |
| 263 | typeof value.name |
| 264 | ) |
| 265 | return value.name |
| 266 | return null; |
| 267 | } |
| 268 | |
| 269 | /** The label names in any body shape GitHub accepts for an issue's labels: `{ labels }` or a bare array, of names or `{ name }`. */ |
| 270 | export function labelNamesOf(body |
| 271 | const list = |
| 272 | typeof body === ' |
| 273 | ? body.labels |
| 274 | : body; |
| 275 | const names = |
| 276 | if (!names || |
| 277 | throw new GithubValidationError([ |
| 278 | { resource: 'Label', |
| 279 | ]); |
| 280 | return names as |
| 281 | } |
| 282 | |
| 283 | export type RestRepositoryObject |
| 284 | |
| 285 | /** What a pull request response adds to its issue: the pull_request row, its head as it stands, and both sides' repositories, null once deleted or unreadable. */ |
| 286 | export type PullRestSource |
| 287 | numericId: number |
| 288 | state: 'open |
| 289 | draft: boolean |
| 290 | baseRef: string |
| 291 | headRef: string |
| 292 | headSha: string |
| 293 | baseSha: string |
| 294 | mergeCommitSha: string |
| 295 | mergedAt: Date |
| 296 | base: RestRepositoryObject |
| 297 | head: RestRepositoryObject |
| 298 | }; |
| 299 | |
| 300 | /** The figures only a single pull request's response carries, from PullRequestsService.getPullRequest. */ |
| 301 | export type PullRestDetail |
| 302 | mergeable: boolean |
| 303 | conflicted: boolean |
| 304 | commits: number |
| 305 | additions: number |
| 306 | deletions: number |
| 307 | changedFiles: number |
| 308 | reviewComments: number |
| 309 | }; |
| 310 | |
| 311 | // Mapper: the issue fields come from IssuesService as for toIssueRest; GitHub's pull request adds the two branches, merge state and its own URLs. |
| 312 | export function toPullRest( |
| 313 | issue: IssueDTO |
| 314 | pull: PullRestSource |
| 315 | context: RestContext |
| 316 | detail?: PullRestDetail |
| 317 | ) { |
| 318 | const { apiUrl, |
| 319 | const url = |
| 320 | const issueUrl = |
| 321 | const htmlUrl = |
| 322 | const user = |
| 323 | (login && context |
| 324 | const assignees = |
| 325 | const side = |
| 326 | ref: string, |
| 327 | sha: string |
| 328 | repository: RestRepositoryObject |
| 329 | ) => ({ |
| 330 | label: repository ? |
| 331 | ref, |
| 332 | sha, |
| 333 | // ponytail: an organization owner is not in context.users and reads as null. |
| 334 | user: repository ? |
| 335 | repo: repository, |
| 336 | }); |
| 337 | const merged = |
| 338 | return { |
| 339 | url, |
| 340 | id: pull.numericId |
| 341 | node_id: encodeNodeId('PullRequest |
| 342 | html_url: htmlUrl, |
| 343 | diff_url: `${htmlUrl} |
| 344 | patch_url: `${htmlUrl} |
| 345 | issue_url: issueUrl, |
| 346 | commits_url: `${url} |
| 347 | review_comments_url: `${url} |
| 348 | review_comment_url: `${apiUrl} |
| 349 | comments_url: `${issueUrl} |
| 350 | statuses_url: `${apiUrl} |
| 351 | number: issue.number |
| 352 | state: issue.state |
| 353 | locked: false, |
| 354 | title: issue.title |
| 355 | user: user(issue |
| 356 | body: issue.body |
| 357 | labels: issue.labels |
| 358 | milestone: null, |
| 359 | active_lock_reason: null, |
| 360 | created_at: issue.createdAt |
| 361 | updated_at: issue.updatedAt |
| 362 | closed_at: issue.closedAt |
| 363 | merged_at: pull.mergedAt |
| 364 | merge_commit_sha: pull.mergeCommitSha |
| 365 | assignee: assignees[0] |
| 366 | assignees, |
| 367 | requested_reviewers: [], |
| 368 | requested_teams: [], |
| 369 | head: side(pull |
| 370 | base: side(pull |
| 371 | _links: { |
| 372 | self: { href: url |
| 373 | html: { href: htmlUrl |
| 374 | issue: { href: issueUrl |
| 375 | comments: { href: ` |
| 376 | review_comments: { href: ` |
| 377 | review_comment: { href: ` |
| 378 | commits: { href: ` |
| 379 | statuses: { href: ` |
| 380 | }, |
| 381 | author_association: authorAssociation(issue |
| 382 | auto_merge: null, |
| 383 | draft: pull.draft |
| 384 | ...(detail && |
| 385 | merged, |
| 386 | mergeable: detail.mergeable |
| 387 | rebaseable: detail.mergeable |
| 388 | mergeable_state: pull.draft |
| 389 | ? 'draft' |
| 390 | : detail.conflicted |
| 391 | ? 'dirty' |
| 392 | : detail.mergeable |
| 393 | ? 'clean' |
| 394 | : 'unknown', |
| 395 | merged_by: merged ? |
| 396 | comments: issue.commentCount |
| 397 | review_comments: detail.reviewComments |
| 398 | maintainer_can_modify: false, |
| 399 | commits: detail.commits |
| 400 | additions: detail.additions |
| 401 | deletions: detail.deletions |
| 402 | changed_files: detail.changedFiles |
| 403 | }), |
| 404 | }; |
| 405 | } |
| 406 | |
| 407 | /** A submitted review as GitHub's REST `pull-request-review`, from reviewsOf. */ |
| 408 | export type RestReviewSource |
| 409 | id: string |
| 410 | numericId: number |
| 411 | authorLogin: string |
| 412 | state: 'commented |
| 413 | body: string |
| 414 | commitSha: string |
| 415 | dismissed: boolean |
| 416 | submittedAt: Date |
| 417 | }; |
| 418 | |
| 419 | // Mapper: reviews come from reviewsOf in Ghost's shape; GitHub's embeds the author and links the pull request. |
| 420 | export function toReviewRest( |
| 421 | review: RestReviewSource |
| 422 | number: number, |
| 423 | context: RestContext |
| 424 | ) { |
| 425 | const { apiUrl, |
| 426 | const htmlUrl = |
| 427 | const pullUrl = |
| 428 | return { |
| 429 | id: review.numericId |
| 430 | node_id: encodeNodeId('PullRequestReview |
| 431 | user: context.users |
| 432 | body: review.body |
| 433 | state: review.dismissed |
| 434 | ? 'DISMISSED' |
| 435 | : (review.state |
| 436 | html_url: htmlUrl, |
| 437 | pull_request_url: pullUrl, |
| 438 | author_association: authorAssociation(review |
| 439 | _links: { html: { |
| 440 | submitted_at: review.submittedAt |
| 441 | commit_id: review.commitSha |
| 442 | }; |
| 443 | } |
| 444 | |
| 445 | /** A tag as a path, each segment escaped and its slashes kept, as the web app's release routes take it. */ |
| 446 | export const tagPath = |
| 447 | tagName.split('/ |
| 448 | |
| 449 | // Mapper: assets come from ReleasesService in Ghost's shape; GitHub's adds the integer id, label, uploader and URLs, which releaseAssetExtrasOf loads. |
| 450 | export function toReleaseAssetRest( |
| 451 | asset: ReleaseAssetDTO |
| 452 | extras: ReleaseAssetExtras |
| 453 | tagName: string, |
| 454 | context: RestContext |
| 455 | ) { |
| 456 | return { |
| 457 | url: `${context. |
| 458 | browser_download_url: `${context. |
| 459 | id: extras.numericId |
| 460 | node_id: encodeNodeId('ReleaseAsset |
| 461 | name: asset.name |
| 462 | label: extras.label |
| 463 | // Assets still uploading are never listed. |
| 464 | state: 'uploaded', |
| 465 | content_type: asset.contentType |
| 466 | size: asset.size |
| 467 | digest: null, |
| 468 | download_count: asset.downloadCount |
| 469 | created_at: asset.createdAt |
| 470 | updated_at: extras.updatedAt |
| 471 | uploader: |
| 472 | (extras.uploaderLogin && |
| 473 | }; |
| 474 | } |
| 475 | |
| 476 | export type RestReleaseAsset |
| 477 | |
| 478 | // Mapper: releases come from ReleasesService in Ghost's shape; GitHub's embeds the author and adds the integer id and URLs. |
| 479 | export function toReleaseRest( |
| 480 | release: ReleaseDTO |
| 481 | numericId: number, |
| 482 | assets: RestReleaseAsset |
| 483 | { uploadUrl, defaultBranch |
| 484 | context: RestContext |
| 485 | ) { |
| 486 | const url = |
| 487 | return { |
| 488 | url, |
| 489 | html_url: `${context. |
| 490 | assets_url: `${url} |
| 491 | upload_url: `${uploadUrl} |
| 492 | // ponytail: no source archives until #66. |
| 493 | tarball_url: null, |
| 494 | zipball_url: null, |
| 495 | id: numericId, |
| 496 | node_id: encodeNodeId('Release |
| 497 | tag_name: release.tagName |
| 498 | // Ghost keeps the commit the tag points at, not the branch it was cut from; a deleted tag reads as the default branch, GitHub's default target. |
| 499 | target_commitish: release.commitSha |
| 500 | name: release.name |
| 501 | body: release.body |
| 502 | draft: release.isDraft |
| 503 | prerelease: release.isPrerelease |
| 504 | immutable: false, |
| 505 | created_at: release.createdAt |
| 506 | published_at: release.publishedAt |
| 507 | updated_at: release.updatedAt |
| 508 | author: |
| 509 | (release.authorUsername && |
| 510 | null, |
| 511 | assets, |
| 512 | }; |
| 513 | } |