| 1 | import { mkdtemp, rm } from 'node:fs/promises'; |
| 2 | import { tmpdir } |
| 3 | import path from 'node:path |
| 4 | import { type Database, |
| 5 | import { atLeast } |
| 6 | import { Inject, Injectable |
| 7 | import { |
| 8 | and, |
| 9 | asc, |
| 10 | desc, |
| 11 | eq, |
| 12 | inArray, |
| 13 | lt, |
| 14 | or, |
| 15 | type SQL, |
| 16 | sql, |
| 17 | } from 'drizzle-orm'; |
| 18 | |
| 19 | import { DATABASE } |
| 20 | import { publishEvent } |
| 21 | import { packRange } |
| 22 | import { fileBody } |
| 23 | import type { |
| 24 | AuthorizedRepository, |
| 25 | RepositoryOperation, |
| 26 | } from '../../lib/git/repository-access/repository-access.js'; |
| 27 | import { createTagObject } |
| 28 | import { isValidRefName } |
| 29 | import { listTags } |
| 30 | import { resolveTargetCommit } |
| 31 | import { ZERO_OID } |
| 32 | import { BranchesService } |
| 33 | import { RepositoryMaterializerService } |
| 34 | import { RepositoryAccessService } |
| 35 | import { RepositoryStorageService } |
| 36 | import { PushTransactionService } |
| 37 | import { UsersService } |
| 38 | import { decodeCursor, encodeCursor |
| 39 | import { InvalidCursorError } |
| 40 | import type { |
| 41 | CreateReleaseRequestDTO, |
| 42 | GetReleasesQueryDTO, |
| 43 | GetReleasesResponseDTO, |
| 44 | ReleaseDTO, |
| 45 | UpdateReleaseRequestDTO, |
| 46 | } from './dto/release.dto.js'; |
| 47 | import { |
| 48 | ReleaseAssetsService, |
| 49 | releaseAssetColumns, |
| 50 | } from './release-assets.service.js'; |
| 51 | import { |
| 52 | InvalidTagNameError, |
| 53 | ReleaseAlreadyExistsError, |
| 54 | ReleaseNotFoundError, |
| 55 | TagTargetNotFoundError, |
| 56 | } from './releases.errors.js'; |
| 57 | |
| 58 | const DEFAULT_PAGE_SIZE = 10 |
| 59 | |
| 60 | type RepositoryRef |
| 61 | |
| 62 | /** Newest published release that is neither a draft nor a prerelease. */ |
| 63 | function latestReleaseId(repositoryId: |
| 64 | return sql`(select ${ |
| 65 | } |
| 66 | |
| 67 | @Injectable() |
| 68 | export class ReleasesService |
| 69 | constructor( |
| 70 | @Inject(DATABASE) |
| 71 | private readonly access |
| 72 | private readonly storage |
| 73 | private readonly materializer |
| 74 | private readonly branches |
| 75 | private readonly pushTransaction |
| 76 | private readonly users |
| 77 | private readonly assets |
| 78 | ) {} |
| 79 | |
| 80 | async listReleases({ |
| 81 | query, |
| 82 | ...target |
| 83 | }: RepositoryRef |
| 84 | query: GetReleasesQueryDTO |
| 85 | }): Promise |
| 86 | const repository = |
| 87 | |
| 88 | const decoded = |
| 89 | if (query.cursor |
| 90 | const pageSize = |
| 91 | |
| 92 | const rows = |
| 93 | repository, |
| 94 | decoded |
| 95 | ? or( |
| 96 | lt(schema.release |
| 97 | and( |
| 98 | eq(schema.release |
| 99 | lt(schema.release |
| 100 | ), |
| 101 | ) |
| 102 | : undefined, |
| 103 | ).limit(pageSize |
| 104 | |
| 105 | const page = |
| 106 | const last = |
| 107 | |
| 108 | return { |
| 109 | releases: await this |
| 110 | nextCursor: |
| 111 | rows.length > pageSize |
| 112 | ? encodeCursor({ date |
| 113 | : null, |
| 114 | }; |
| 115 | } |
| 116 | |
| 117 | async getLatestRelease(target |
| 118 | const repository = |
| 119 | return this.readOne |
| 120 | repository, |
| 121 | eq(schema.release |
| 122 | ); |
| 123 | } |
| 124 | |
| 125 | async getReleaseByTag({ |
| 126 | tagName, |
| 127 | ...target |
| 128 | }: RepositoryRef |
| 129 | const repository = |
| 130 | return this.readOne |
| 131 | } |
| 132 | |
| 133 | /** A tag that does not exist yet is created first, annotated, through the same commit point a push uses. */ |
| 134 | async createRelease({ |
| 135 | body, |
| 136 | ...target |
| 137 | }: RepositoryRef |
| 138 | requesterId: string |
| 139 | body: CreateReleaseRequestDTO |
| 140 | }): Promise |
| 141 | const repository = |
| 142 | if (!(await |
| 143 | throw new InvalidTagNameError( |
| 144 | } |
| 145 | |
| 146 | const [existing] |
| 147 | .select({ id: |
| 148 | .from(schema.release |
| 149 | .where( |
| 150 | and( |
| 151 | eq(schema.release |
| 152 | eq(schema.release |
| 153 | ), |
| 154 | ); |
| 155 | if (existing) throw |
| 156 | |
| 157 | const directory = |
| 158 | const tags = |
| 159 | // ponytail: a draft tags its commit straight away; GitHub waits for publishing, which needs the target stored on the row. |
| 160 | if (!tags.some |
| 161 | const sha = |
| 162 | repository, |
| 163 | directory, |
| 164 | tags, |
| 165 | body.target, |
| 166 | ); |
| 167 | await this.createTag |
| 168 | repository, |
| 169 | directory, |
| 170 | sha, |
| 171 | name: body.tagName |
| 172 | message: body.name |
| 173 | requesterId: target.requesterId |
| 174 | }); |
| 175 | } |
| 176 | |
| 177 | const isDraft = |
| 178 | const created = |
| 179 | // A concurrent create for the same tag loses on the unique index. |
| 180 | const [row] |
| 181 | .insert(schema.release |
| 182 | .values({ |
| 183 | repositoryId: repository.id |
| 184 | tagName: body.tagName |
| 185 | name: body.name |
| 186 | body: body.body |
| 187 | isDraft, |
| 188 | isPrerelease: body.isPrerelease |
| 189 | authorId: target.requesterId |
| 190 | publishedAt: isDraft ? |
| 191 | }) |
| 192 | .onConflictDoNothing() |
| 193 | .returning({ id: |
| 194 | if (!row) |
| 195 | const event = |
| 196 | repositoryId: repository.id |
| 197 | actorId: target.requesterId |
| 198 | payload: { releaseId: row |
| 199 | }; |
| 200 | await publishEvent(tx |
| 201 | if (!isDraft) |
| 202 | await publishEvent(tx |
| 203 | } |
| 204 | return row; |
| 205 | }); |
| 206 | |
| 207 | return this.readOne |
| 208 | } |
| 209 | |
| 210 | async updateRelease({ |
| 211 | id, |
| 212 | body, |
| 213 | ...target |
| 214 | }: RepositoryRef |
| 215 | id: string |
| 216 | requesterId: string |
| 217 | body: UpdateReleaseRequestDTO |
| 218 | }): Promise |
| 219 | const repository = |
| 220 | |
| 221 | const updated = |
| 222 | const [before] |
| 223 | .select({ isDraft: |
| 224 | .from(schema.release |
| 225 | .where(this.ownRelease |
| 226 | .for('update') |
| 227 | if (!before) |
| 228 | |
| 229 | const [row] |
| 230 | .update(schema.release |
| 231 | .set({ |
| 232 | // always set, so an empty edit still finds the row instead of failing to build the query |
| 233 | updatedAt: new Date |
| 234 | ...(body.name |
| 235 | ...(body.body |
| 236 | ...(body.isPrerelease |
| 237 | isPrerelease: body.isPrerelease |
| 238 | }), |
| 239 | ...(body.isDraft |
| 240 | // stamped the first time it is published, and kept through any later unpublishing |
| 241 | ...(body.isDraft |
| 242 | publishedAt: sql`coalesce(${ |
| 243 | }), |
| 244 | }) |
| 245 | .where(this.ownRelease |
| 246 | .returning({ id: |
| 247 | if (!row) |
| 248 | const event = |
| 249 | repositoryId: repository.id |
| 250 | actorId: target.requesterId |
| 251 | payload: { releaseId: row |
| 252 | }; |
| 253 | await publishEvent(tx |
| 254 | if (before.isDraft |
| 255 | await publishEvent(tx |
| 256 | } |
| 257 | return row; |
| 258 | }); |
| 259 | |
| 260 | return this.readOne |
| 261 | } |
| 262 | |
| 263 | /** The tag stays: deleting a release deletes its notes and its assets. */ |
| 264 | async deleteRelease({ |
| 265 | id, |
| 266 | ...target |
| 267 | }: RepositoryRef |
| 268 | const repository = |
| 269 | |
| 270 | const [release] |
| 271 | .select({ |
| 272 | id: schema.release |
| 273 | tagName: schema.release |
| 274 | name: schema.release |
| 275 | }) |
| 276 | .from(schema.release |
| 277 | .where(this.ownRelease |
| 278 | if (!release) |
| 279 | |
| 280 | // storage first: the rows go with the release, and an object nothing points at could never be found again |
| 281 | await this.assets |
| 282 | await this.db |
| 283 | await tx.delete |
| 284 | await publishEvent(tx |
| 285 | type: 'release.deleted', |
| 286 | repositoryId: repository.id |
| 287 | actorId: target.requesterId |
| 288 | payload: { release |
| 289 | }); |
| 290 | }); |
| 291 | } |
| 292 | |
| 293 | /** An annotated tag, as `git tag -a` makes, so the tag carries its own date and author rather than borrowing its commit's. Its one object is packed and pushed through `commitPush`. */ |
| 294 | private async createTag |
| 295 | repository, |
| 296 | directory, |
| 297 | sha, |
| 298 | name, |
| 299 | message, |
| 300 | requesterId, |
| 301 | }: { |
| 302 | repository: AuthorizedRepository |
| 303 | directory: string |
| 304 | sha: string |
| 305 | name: string |
| 306 | message: string |
| 307 | requesterId: string |
| 308 | }) { |
| 309 | const tagger = |
| 310 | const oid = |
| 311 | gitDir: directory, |
| 312 | sha, |
| 313 | name, |
| 314 | message, |
| 315 | tagger, |
| 316 | }); |
| 317 | |
| 318 | const scratch = |
| 319 | try { |
| 320 | const pack = |
| 321 | gitDir: directory, |
| 322 | include: [oid], |
| 323 | exclude: [sha], |
| 324 | prefix: path.join |
| 325 | }); |
| 326 | await this.pushTransaction |
| 327 | repoId: repository.id |
| 328 | transitions: [ |
| 329 | { |
| 330 | ref: `refs/tags/${name |
| 331 | oldOid: ZERO_OID, |
| 332 | newOid: Buffer.from |
| 333 | }, |
| 334 | ], |
| 335 | body: fileBody(pack |
| 336 | packOffset: 0, |
| 337 | pushedBy: requesterId, |
| 338 | }); |
| 339 | } finally { |
| 340 | await rm(scratch |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | private ownRelease(repository |
| 345 | return and( |
| 346 | eq(schema.release |
| 347 | eq(schema.release |
| 348 | ); |
| 349 | } |
| 350 | |
| 351 | private async resolveTarget |
| 352 | repository: AuthorizedRepository |
| 353 | directory: string, |
| 354 | tags: { |
| 355 | requested: string |
| 356 | ) { |
| 357 | const name = |
| 358 | const sha = |
| 359 | gitDir: directory, |
| 360 | defaultBranch: repository.defaultBranch |
| 361 | branches: await this |
| 362 | tags, |
| 363 | requested: name, |
| 364 | }); |
| 365 | if (!sha) |
| 366 | throw new TagTargetNotFoundError( |
| 367 | return sha; |
| 368 | } |
| 369 | |
| 370 | private async readOne |
| 371 | const [row] |
| 372 | if (!row) |
| 373 | |
| 374 | const [release] |
| 375 | return release; |
| 376 | } |
| 377 | |
| 378 | /** Newest first. Drafts are left out for anyone who cannot write to the repository. */ |
| 379 | private select(repository |
| 380 | const canWrite = |
| 381 | |
| 382 | return this.db |
| 383 | .select({ |
| 384 | id: schema.release |
| 385 | tagName: schema.release |
| 386 | name: schema.release |
| 387 | body: schema.release |
| 388 | isDraft: schema.release |
| 389 | isPrerelease: schema.release |
| 390 | isLatest: sql<boolean |
| 391 | authorUsername: schema.user |
| 392 | publishedAt: isoTimestamp(schema |
| 393 | createdAt: isoTimestamp(schema |
| 394 | updatedAt: isoTimestamp(schema |
| 395 | }) |
| 396 | .from(schema.release |
| 397 | .leftJoin(schema.user |
| 398 | .where( |
| 399 | and( |
| 400 | eq(schema.release |
| 401 | canWrite ? undefined : |
| 402 | where, |
| 403 | ), |
| 404 | ) |
| 405 | .orderBy(desc(schema |
| 406 | } |
| 407 | |
| 408 | /** Commits come from git, not the row: a tag can be moved or deleted by a push at any time. Assets still uploading are left out. */ |
| 409 | private async expand |
| 410 | repository: AuthorizedRepository |
| 411 | releases: T |
| 412 | ) { |
| 413 | if (releases.length |
| 414 | |
| 415 | const [tags, |
| 416 | this.openCache(repository |
| 417 | this.db |
| 418 | .select({ |
| 419 | releaseId: schema.releaseAsset |
| 420 | ...releaseAssetColumns, |
| 421 | }) |
| 422 | .from(schema.releaseAsset |
| 423 | .where( |
| 424 | and( |
| 425 | inArray( |
| 426 | schema.releaseAsset.releaseId |
| 427 | releases.map((release |
| 428 | ), |
| 429 | eq(schema.releaseAsset |
| 430 | ), |
| 431 | ) |
| 432 | .orderBy(asc(schema |
| 433 | ]); |
| 434 | const shas = |
| 435 | const viewerCanEdit = |
| 436 | |
| 437 | return releases.map |
| 438 | ...release, |
| 439 | commitSha: shas.get |
| 440 | assets: assets |
| 441 | .filter((asset) |
| 442 | .map(({ releaseId |
| 443 | viewerCanEdit, |
| 444 | })); |
| 445 | } |
| 446 | |
| 447 | private async openCache |
| 448 | const directory = |
| 449 | await this.materializer |
| 450 | repository.id, |
| 451 | directory, |
| 452 | repository.defaultBranch, |
| 453 | ); |
| 454 | return directory; |
| 455 | } |
| 456 | |
| 457 | private authorize( |
| 458 | { username, repo |
| 459 | operation: RepositoryOperation |
| 460 | ) { |
| 461 | return this.access |
| 462 | username, |
| 463 | repo, |
| 464 | actor: requesterId ? |
| 465 | operation, |
| 466 | }); |
| 467 | } |
| 468 | } |