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