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