| 1 | import type { Readable } from 'node:stream'; |
| 2 | |
| 3 | import { type Database, |
| 4 | import { Inject, Injectable |
| 5 | import { ConfigService } |
| 6 | import { and, eq |
| 7 | |
| 8 | import { DATABASE } |
| 9 | import { |
| 10 | GithubNotFoundError, |
| 11 | GithubValidationError, |
| 12 | UnprocessableError, |
| 13 | } from '../../../lib/github/github.errors.js'; |
| 14 | import { encodeNodeId } |
| 15 | import { |
| 16 | releaseAssetExtrasOf, |
| 17 | releaseNumericIdsOf, |
| 18 | } from '../../../lib/github/node-lookup.js'; |
| 19 | import { githubOrigins } |
| 20 | import { |
| 21 | pageOf, |
| 22 | RestPage, |
| 23 | toReleaseAssetRest, |
| 24 | toReleaseRest, |
| 25 | } from '../../../lib/github/rest.js'; |
| 26 | import { MEDIA_TYPE } |
| 27 | import type { ReleaseDTO |
| 28 | import { ReleaseAssetsService } |
| 29 | import { ReleasesService } |
| 30 | import type { |
| 31 | GithubRepositoryTarget, |
| 32 | GithubViewer, |
| 33 | } from '../../auth/github-request.js'; |
| 34 | import type { RepositoryNode |
| 35 | import type { GithubPageQueryDTO |
| 36 | import { GithubIssuesService } |
| 37 | import type { |
| 38 | GithubCreateReleaseDTO, |
| 39 | GithubUpdateReleaseAssetDTO, |
| 40 | GithubUpdateReleaseDTO, |
| 41 | GithubUploadReleaseAssetQueryDTO, |
| 42 | } from './dto/release.dto.js'; |
| 43 | |
| 44 | /** GitHub's REST releases and assets over ReleasesService: its releases plus the integer ids, labels and users GitHub's shapes embed. */ |
| 45 | @Injectable() |
| 46 | export class GithubReleasesService |
| 47 | constructor( |
| 48 | @Inject(DATABASE) |
| 49 | private readonly github |
| 50 | private readonly releases |
| 51 | private readonly assets |
| 52 | private readonly config |
| 53 | ) {} |
| 54 | |
| 55 | async list( |
| 56 | { owner, repo |
| 57 | query: GithubPageQueryDTO |
| 58 | ) { |
| 59 | const repository = |
| 60 | const page = |
| 61 | const { releases, |
| 62 | ...this.github. |
| 63 | offset: page.offset |
| 64 | limit: page.perPage |
| 65 | order: { field: ' |
| 66 | }); |
| 67 | return new RestPage |
| 68 | } |
| 69 | |
| 70 | /** Ghost has no discussions, so any category is one that does not exist, which GitHub answers with 404; nor can it generate notes. */ |
| 71 | async create( |
| 72 | { owner, repo |
| 73 | body: GithubCreateReleaseDTO |
| 74 | ) { |
| 75 | const { repository, |
| 76 | owner, |
| 77 | repo, |
| 78 | viewer, |
| 79 | 'POST /repos/:owner/:repo/releases', |
| 80 | ); |
| 81 | if (body.discussion_category_name |
| 82 | throw new GithubNotFoundError() |
| 83 | if (body.generate_release_notes |
| 84 | throw new GithubValidationError([ |
| 85 | { |
| 86 | resource: 'Release', |
| 87 | field: 'generate_release_notes', |
| 88 | code: 'invalid', |
| 89 | }, |
| 90 | ]); |
| 91 | const created = |
| 92 | ...this.github. |
| 93 | requesterId: signedIn.userId |
| 94 | body: { |
| 95 | tagName: body.tag_name |
| 96 | target: body.target_commitish |
| 97 | name: body.name |
| 98 | body: body.body |
| 99 | isDraft: body.draft |
| 100 | isPrerelease: body.prerelease |
| 101 | }, |
| 102 | }); |
| 103 | return this.one |
| 104 | } |
| 105 | |
| 106 | async latest({ |
| 107 | const repository = |
| 108 | return this.one |
| 109 | repository, |
| 110 | await this.releases |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | /** Published releases only, as on GitHub: gh finds a draft by its tag through GraphQL. */ |
| 115 | async byTag( |
| 116 | { owner, repo |
| 117 | tagName: string, |
| 118 | ) { |
| 119 | const repository = |
| 120 | const release = |
| 121 | ...this.github. |
| 122 | tagName, |
| 123 | }); |
| 124 | if (release.isDraft |
| 125 | return this.one |
| 126 | } |
| 127 | |
| 128 | /** Drafts included for those who can write. */ |
| 129 | async get( |
| 130 | { owner, repo |
| 131 | releaseId: number, |
| 132 | ) { |
| 133 | const repository = |
| 134 | return this.one |
| 135 | repository, |
| 136 | await this.byNumericId |
| 137 | ); |
| 138 | } |
| 139 | |
| 140 | async update( |
| 141 | { owner, repo |
| 142 | releaseId: number, |
| 143 | body: GithubUpdateReleaseDTO |
| 144 | ) { |
| 145 | const { repository, |
| 146 | owner, |
| 147 | repo, |
| 148 | viewer, |
| 149 | 'PATCH /repos/:owner/:repo/releases/:release_id', |
| 150 | ); |
| 151 | if (body.discussion_category_name |
| 152 | throw new GithubNotFoundError() |
| 153 | const current = |
| 154 | if (body.tag_name |
| 155 | throw new UnprocessableError( |
| 156 | 'Ghost cannot move a release to another tag', |
| 157 | ); |
| 158 | const updated = |
| 159 | ...this.github. |
| 160 | requesterId: signedIn.userId |
| 161 | id: current.id |
| 162 | body: { |
| 163 | name: body.name |
| 164 | body: body.body |
| 165 | isDraft: body.draft |
| 166 | isPrerelease: body.prerelease |
| 167 | }, |
| 168 | }); |
| 169 | return this.one |
| 170 | } |
| 171 | |
| 172 | /** The tag stays, as on GitHub; gh deletes it separately through git/refs. */ |
| 173 | async delete( |
| 174 | { owner, repo |
| 175 | releaseId: number, |
| 176 | ) { |
| 177 | const { repository, |
| 178 | owner, |
| 179 | repo, |
| 180 | viewer, |
| 181 | 'DELETE /repos/:owner/:repo/releases/:release_id', |
| 182 | ); |
| 183 | const release = |
| 184 | await this.releases |
| 185 | ...this.github. |
| 186 | requesterId: signedIn.userId |
| 187 | id: release.id |
| 188 | }); |
| 189 | } |
| 190 | |
| 191 | async listAssets( |
| 192 | { owner, repo |
| 193 | releaseId: number, |
| 194 | query: GithubPageQueryDTO |
| 195 | ) { |
| 196 | const repository = |
| 197 | const page = |
| 198 | const { assets |
| 199 | repository, |
| 200 | await this.byNumericId |
| 201 | ); |
| 202 | return new RestPage |
| 203 | assets.slice(page |
| 204 | page, |
| 205 | assets.length, |
| 206 | ); |
| 207 | } |
| 208 | |
| 209 | async asset( |
| 210 | { owner, repo |
| 211 | assetId: number, |
| 212 | ) { |
| 213 | const repository = |
| 214 | const { release, |
| 215 | repository, |
| 216 | viewer, |
| 217 | assetId, |
| 218 | ); |
| 219 | return this.assetOf |
| 220 | } |
| 221 | |
| 222 | /** The asset's bytes, counted as a download. */ |
| 223 | async download( |
| 224 | { owner, repo |
| 225 | assetId: number, |
| 226 | ): Promise<{ |
| 227 | const repository = |
| 228 | const { asset |
| 229 | return this.assets |
| 230 | ...this.github. |
| 231 | asset: { id: asset |
| 232 | }); |
| 233 | } |
| 234 | |
| 235 | /** `type` is what the file is served as: the upload's own Content-Type. */ |
| 236 | async upload( |
| 237 | { owner, repo |
| 238 | releaseId: number, |
| 239 | query: GithubUploadReleaseAssetQueryDTO |
| 240 | file: { |
| 241 | type: string |
| 242 | length: string |
| 243 | body: Readable |
| 244 | }, |
| 245 | ) { |
| 246 | const { repository, |
| 247 | owner, |
| 248 | repo, |
| 249 | viewer, |
| 250 | 'POST /repos/:owner/:repo/releases/:release_id/assets', |
| 251 | ); |
| 252 | if (!file.type |
| 253 | throw new GithubValidationError([ |
| 254 | { resource: 'ReleaseAsset', |
| 255 | ]); |
| 256 | const release = |
| 257 | const uploaded = |
| 258 | ...this.github. |
| 259 | requesterId: signedIn.userId |
| 260 | releaseId: release.id |
| 261 | name: query.name |
| 262 | label: query.label |
| 263 | type: file.type |
| 264 | contentLength: file.length, |
| 265 | body: file.body |
| 266 | }); |
| 267 | return this.assetOf |
| 268 | repository, |
| 269 | await this.releases |
| 270 | ...this.github. |
| 271 | id: release.id |
| 272 | }), |
| 273 | uploaded.id, |
| 274 | ); |
| 275 | } |
| 276 | |
| 277 | async updateAsset( |
| 278 | { owner, repo |
| 279 | assetId: number, |
| 280 | body: GithubUpdateReleaseAssetDTO |
| 281 | ) { |
| 282 | const { repository, |
| 283 | owner, |
| 284 | repo, |
| 285 | viewer, |
| 286 | 'PATCH /repos/:owner/:repo/releases/assets/:asset_id', |
| 287 | ); |
| 288 | const { release, |
| 289 | repository, |
| 290 | signedIn, |
| 291 | assetId, |
| 292 | ); |
| 293 | await this.assets |
| 294 | ...this.github. |
| 295 | requesterId: signedIn.userId |
| 296 | assetId: asset.id |
| 297 | name: body.name |
| 298 | label: body.label |
| 299 | }); |
| 300 | return this.assetOf |
| 301 | repository, |
| 302 | await this.releases |
| 303 | ...this.github. |
| 304 | id: release.id |
| 305 | }), |
| 306 | asset.id, |
| 307 | ); |
| 308 | } |
| 309 | |
| 310 | async deleteAsset( |
| 311 | { owner, repo |
| 312 | assetId: number, |
| 313 | ) { |
| 314 | const { repository, |
| 315 | owner, |
| 316 | repo, |
| 317 | viewer, |
| 318 | 'DELETE /repos/:owner/:repo/releases/assets/:asset_id', |
| 319 | ); |
| 320 | const { asset |
| 321 | repository, |
| 322 | signedIn, |
| 323 | assetId, |
| 324 | ); |
| 325 | await this.assets |
| 326 | ...this.github. |
| 327 | requesterId: signedIn.userId |
| 328 | assetId: asset.id |
| 329 | }); |
| 330 | } |
| 331 | |
| 332 | private async rest |
| 333 | const assets = |
| 334 | const [numericIds, |
| 335 | releaseNumericIdsOf( |
| 336 | this.db, |
| 337 | releases.map((release |
| 338 | ), |
| 339 | releaseAssetExtrasOf( |
| 340 | this.db, |
| 341 | assets.map((asset |
| 342 | ), |
| 343 | ]); |
| 344 | const context = |
| 345 | logins: [ |
| 346 | ...releases.flatMap |
| 347 | ...[...extras |
| 348 | ], |
| 349 | labels: [], |
| 350 | }); |
| 351 | const uploadUrl = |
| 352 | return releases.map |
| 353 | toReleaseRest( |
| 354 | release, |
| 355 | numericIds.get(release |
| 356 | release.assets.map |
| 357 | toReleaseAssetRest( |
| 358 | asset, |
| 359 | extras.get(asset |
| 360 | release.tagName, |
| 361 | context, |
| 362 | ), |
| 363 | ), |
| 364 | { uploadUrl, defaultBranch: |
| 365 | context, |
| 366 | ), |
| 367 | ); |
| 368 | } |
| 369 | |
| 370 | private async one |
| 371 | const [rest] |
| 372 | return rest!; |
| 373 | } |
| 374 | |
| 375 | /** A release by its GitHub id, as the viewer may see it; 404 when it is in another repository or a draft they cannot see. */ |
| 376 | private async byNumericId |
| 377 | repository: RepositoryNode |
| 378 | viewer: GithubViewer |
| 379 | numericId: number, |
| 380 | ) { |
| 381 | const [row] |
| 382 | .select({ id: |
| 383 | .from(schema.release |
| 384 | .where( |
| 385 | and( |
| 386 | eq(schema.release |
| 387 | eq(schema.release |
| 388 | ), |
| 389 | ); |
| 390 | if (!row) |
| 391 | return this.releases |
| 392 | ...this.github. |
| 393 | id: row.id |
| 394 | }); |
| 395 | } |
| 396 | |
| 397 | /** An asset by its GitHub id, with its release; reading the release is what checks the viewer may see it. */ |
| 398 | private async assetByNumericId |
| 399 | repository: RepositoryNode |
| 400 | viewer: GithubViewer |
| 401 | numericId: number, |
| 402 | ) { |
| 403 | const [row] |
| 404 | .select({ |
| 405 | id: schema.releaseAsset |
| 406 | releaseId: schema.releaseAsset |
| 407 | }) |
| 408 | .from(schema.releaseAsset |
| 409 | .where( |
| 410 | and( |
| 411 | eq(schema.releaseAsset |
| 412 | eq(schema.releaseAsset |
| 413 | ), |
| 414 | ); |
| 415 | if (!row) |
| 416 | const release = |
| 417 | ...this.github. |
| 418 | id: row.releaseId |
| 419 | }); |
| 420 | const asset = |
| 421 | // Still uploading: ReleasesService lists finished assets only. |
| 422 | if (!asset) |
| 423 | return { release, |
| 424 | } |
| 425 | |
| 426 | /** One asset of `release` in GitHub's shape, by its Ghost id. */ |
| 427 | private async assetOf |
| 428 | repository: RepositoryNode |
| 429 | release: ReleaseDTO |
| 430 | assetId: string, |
| 431 | ) { |
| 432 | const nodeId = |
| 433 | return (await this |
| 434 | (asset) => |
| 435 | )!; |
| 436 | } |
| 437 | } |