Public: anyone can read this; the crew can change it.
A fork or pull opened while a repository is being deleted is refused, and a crash never leaves the delete mark behind #15228
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/15034-a-fork-or-pull-opened-while-a-repository → main 4 commits fork 2 threads · 2 unresolved
12 files
changed +353
−5
src/actions/propose_change.ts +1
| @@ -121,6 +121,7 @@ function forkFailure(e: Awaited<ReturnType<typeof forkRepo>> extends infer R ? ( | ||
| 121 | 121 | case "already_forked": |
| 122 | 122 | case "fork_failed": |
| 123 | 123 | case "limit": |
| 124 | case "deleting": | |
| 124 | 125 | return { kind: "fork", reason: e.replace(/_/g, " ") }; |
| 125 | 126 | case "not_found": |
| 126 | 127 | return "not_found"; |
src/core/migration-pins/20261121170000-repo-deleting.pin added +1
| @@ -0,0 +1,1 @@ | ||
| 1 | cb43e57e72dc8fb0e1dfb4bcc5c7efac358713b4a0948a6a286c4e981ccdee33 |
src/core/migrations/20261121170000-repo-deleting.ts added +6
| @@ -0,0 +1,6 @@ | ||
| 1 | // When a delete of this repository began (#15034): while it stands, a fork of it or a pull | |
| 2 | // from or into it is refused, so neither lands between the delete's checks and its removal. | |
| 3 | // Time-bound in `repo_deleting.ts`, so a crash mid-delete never leaves it standing. | |
| 4 | export default ` | |
| 5 | ALTER TABLE repo ADD COLUMN deleting_at TEXT; | |
| 6 | `; |
src/core/pulls.ts +16 −2
| @@ -48,6 +48,7 @@ import { approvalRecord, listReviews, reviewSummary, type ApprovalRecord } from | ||
| 48 | 48 | import { checkStale, checksHold } from "../checks/stale.ts"; |
| 49 | 49 | import { checkSummary } from "./runs.ts"; |
| 50 | 50 | import { writeStamp } from "./write_clock.ts"; |
| 51 | import { isDeleting } from "./repo_deleting.ts"; | |
| 51 | 52 | |
| 52 | 53 | export interface Pull extends Issue { |
| 53 | 54 | readonly baseBranch: string; |
| @@ -166,7 +167,9 @@ export type CreatePullError = | ||
| 166 | 167 | | "unknown_head" |
| 167 | 168 | | "title_too_long" |
| 168 | 169 | | "body_too_long" |
| 169 | | "nothing_to_merge"; | |
| 170 | | "nothing_to_merge" | |
| 171 | /** The base or the head repository is being deleted (`repo_deleting.ts`). */ | |
| 172 | | "deleting"; | |
| 170 | 173 | |
| 171 | 174 | export interface CreatePullInput { |
| 172 | 175 | /** The base (upstream) repo the PR is created in. */ |
| @@ -251,6 +254,11 @@ async function followBase( | ||
| 251 | 254 | if (tip !== undefined) await writeRef(headDir, `refs/heads/${branch}`, tip); |
| 252 | 255 | } |
| 253 | 256 | |
| 257 | /** The repository a pull's head lives in: a fork's, or the base's own. */ | |
| 258 | function headRepoIdOf(input: Pick<CreatePullInput, "repoId" | "headRepoId">): string { | |
| 259 | return input.headRepoId ?? input.repoId; | |
| 260 | } | |
| 261 | ||
| 254 | 262 | export async function createPull( |
| 255 | 263 | db: Database, |
| 256 | 264 | dir: string, |
| @@ -263,6 +271,7 @@ export async function createPull( | ||
| 263 | 271 | input.headRepoId !== undefined && input.headRepoId !== input.repoId; |
| 264 | 272 | if (!crossRepo && input.baseBranch === input.headBranch) |
| 265 | 273 | return err("same_branch"); |
| 274 | if (isDeleting(db, [input.repoId, headRepoIdOf(input)])) return err("deleting"); | |
| 266 | 275 | |
| 267 | 276 | const baseOid = await resolveOid(dir, `refs/heads/${input.baseBranch}`); |
| 268 | 277 | if (!baseOid) return err("unknown_base"); |
| @@ -290,6 +299,11 @@ export async function createPull( | ||
| 290 | 299 | if (!headOid) return err("unknown_head"); |
| 291 | 300 | if ((await aheadCount(at, baseOid, headOid)) === 0) |
| 292 | 301 | return err("nothing_to_merge"); |
| 302 | // After the last await and before the row: a delete that began meanwhile | |
| 303 | // has already looked for pulls naming its repository, and would not see | |
| 304 | // this one (`repo_deleting.ts`). | |
| 305 | if (isDeleting(db, [input.repoId, headRepoIdOf(input)])) | |
| 306 | return err("deleting"); | |
| 293 | 307 | |
| 294 | 308 | // The fix for a sealed report is sealed with it: its diff says where |
| 295 | 309 | // the hole is (`sealed.ts`). |
| @@ -309,7 +323,7 @@ export async function createPull( | ||
| 309 | 323 | issue.value.id, |
| 310 | 324 | ); |
| 311 | 325 | |
| 312 | const headRepoId = input.headRepoId ?? input.repoId; | |
| 326 | const headRepoId = headRepoIdOf(input); | |
| 313 | 327 | db.query( |
| 314 | 328 | `INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, head_repo_id, reverts_issue_id, variant_group) |
| 315 | 329 | VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)`, |
src/core/repo_deleting.ts added +42
| @@ -0,0 +1,42 @@ | ||
| 1 | /** | |
| 2 | * The delete mark (#15034): `deleteRepo` sets it before its first await, and | |
| 3 | * while it stands a fork of the repository, or a pull from or into it, is | |
| 4 | * refused with `deleting` — so neither can land between the delete's checks | |
| 5 | * (no pull names it as head, every borrower detached) and its removal. | |
| 6 | * | |
| 7 | * The mark is time-bound: it stands for `DELETING_MS` from when it was last | |
| 8 | * set, and the delete sets it again after each slow step. A process that dies | |
| 9 | * mid-delete leaves a stale mark nobody reads as standing, so a crash never | |
| 10 | * leaves a repository that can be neither forked nor pulled. | |
| 11 | */ | |
| 12 | ||
| 13 | import type { Database } from "bun:sqlite"; | |
| 14 | ||
| 15 | /** How long a mark stands without being set again. */ | |
| 16 | export const DELETING_MS = 15 * 60_000; | |
| 17 | ||
| 18 | /** Set (or refresh) the mark on `repoId`. Synchronous: call it before any await. */ | |
| 19 | export function markDeleting(db: Database, repoId: string, now: number = Date.now()): void { | |
| 20 | db.query("UPDATE repo SET deleting_at = ?1 WHERE id = ?2").run(new Date(now).toISOString(), repoId); | |
| 21 | } | |
| 22 | ||
| 23 | /** Take the mark back: the delete was refused and the repository stays. */ | |
| 24 | export function clearDeleting(db: Database, repoId: string): void { | |
| 25 | db.query("UPDATE repo SET deleting_at = NULL WHERE id = ?1").run(repoId); | |
| 26 | } | |
| 27 | ||
| 28 | /** Whether any of `repoIds` carries a mark set within `DELETING_MS` of `now`. */ | |
| 29 | export function isDeleting(db: Database, repoIds: readonly string[], now: number = Date.now()): boolean { | |
| 30 | const since = new Date(now - DELETING_MS).toISOString(); | |
| 31 | return repoIds.some( | |
| 32 | (repoId) => | |
| 33 | db | |
| 34 | .query<{ n: number }, [string, string]>( | |
| 35 | "SELECT 1 AS n FROM repo WHERE id = ?1 AND deleting_at IS NOT NULL AND deleting_at > ?2", | |
| 36 | ) | |
| 37 | .get(repoId, since) !== null, | |
| 38 | ); | |
| 39 | } | |
| 40 | ||
| 41 | /** What a refused fork or pull says, in every surface's words. */ | |
| 42 | export const DELETING_SAYS = "the repository is being deleted"; |
src/core/repos.ts +44 −1
| @@ -27,6 +27,7 @@ import { createLog } from "../kernel/log.ts"; | ||
| 27 | 27 | import { err, ok, type Result } from "../kernel/result.ts"; |
| 28 | 28 | import { appsNaming, isAppMember } from "./app_members.ts"; |
| 29 | 29 | import { forgetListings } from "./listings.ts"; |
| 30 | import { clearDeleting, isDeleting, markDeleting } from "./repo_deleting.ts"; | |
| 30 | 31 | import { forgetRepoCursors } from "./origins.ts"; |
| 31 | 32 | import { NOT_AGENT, NOT_ARCHIVED, NOT_UNCLAIMED } from "./offered.ts"; |
| 32 | 33 | import { likeEscape, NOT_STANDING, pageLimit } from "./issues.ts"; |
| @@ -443,7 +444,9 @@ export type ForkRepoError = | ||
| 443 | 444 | | "already_forked" |
| 444 | 445 | | "fork_failed" |
| 445 | 446 | /** The owner's forks in the last day reached `perDay` (#888). */ |
| 446 | | "limit"; | |
| 447 | | "limit" | |
| 448 | /** The parent is being deleted (`repo_deleting.ts`). */ | |
| 449 | | "deleting"; | |
| 447 | 450 | |
| 448 | 451 | const DAY_MS = 86_400_000; |
| 449 | 452 | |
| @@ -464,6 +467,9 @@ export function forksSince(db: Database, ownerId: string, since: string): string | ||
| 464 | 467 | /** A marker the row's transaction throws when the day's forks are made. */ |
| 465 | 468 | class OverLimit extends Error {} |
| 466 | 469 | |
| 470 | /** A marker the row's transaction throws when the parent's delete began during the copy. */ | |
| 471 | class ParentDeleting extends Error {} | |
| 472 | ||
| 467 | 473 | /** |
| 468 | 474 | * Fork a repo into another owner. The git objects are shared with the parent |
| 469 | 475 | * via alternates (see forkBareRepo), so this is instant and storage-bounded. |
| @@ -509,6 +515,7 @@ export async function forkRepo( | ||
| 509 | 515 | ((options.perDay !== undefined && forksSince(db, ownerId, since).length >= options.perDay) || |
| 510 | 516 | (options.forgePerDay !== undefined && contributionForksSince(db, since) >= options.forgePerDay)); |
| 511 | 517 | if (overLimit()) return err("limit"); |
| 518 | if (isDeleting(db, [parent.id], now)) return err("deleting"); | |
| 512 | 519 | if (!isSafeName(forkName)) return err("invalid_name"); |
| 513 | 520 | const owner = db |
| 514 | 521 | .query< |
| @@ -554,6 +561,10 @@ export async function forkRepo( | ||
| 554 | 561 | // Counted again with the write: the git copy above took a moment, |
| 555 | 562 | // and another fork may have landed in it. |
| 556 | 563 | if (overLimit()) throw new OverLimit(); |
| 564 | // Asked again with the write: a delete that began during the copy | |
| 565 | // listed its borrowers without this one, and would pull its objects | |
| 566 | // out from under it. | |
| 567 | if (isDeleting(db, [parent.id])) throw new ParentDeleting(); | |
| 557 | 568 | db.query( |
| 558 | 569 | `INSERT INTO repo (id, owner_id, name, visibility, description, default_branch, fork_parent_id, sealed_for) |
| 559 | 570 | VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)`, |
| @@ -581,6 +592,7 @@ export async function forkRepo( | ||
| 581 | 592 | ); |
| 582 | 593 | } catch (error) { |
| 583 | 594 | if (error instanceof OverLimit) return err("limit"); |
| 595 | if (error instanceof ParentDeleting) return err("deleting"); | |
| 584 | 596 | throw error; |
| 585 | 597 | } |
| 586 | 598 | return ok(getRepoById(db, repoId)!); |
| @@ -1151,11 +1163,40 @@ export function listReposByOwner( | ||
| 1151 | 1163 | * rule. A sealed repository's row goes with its base's (`ON DELETE CASCADE`), |
| 1152 | 1164 | * so its directory is removed after the base's row, leaving no directory |
| 1153 | 1165 | * without a row to hold the name. |
| 1166 | * The whole delete runs under its mark (`repo_deleting.ts`): a fork of it, or | |
| 1167 | * a pull from or into it, opened meanwhile is refused with `deleting`. | |
| 1154 | 1168 | */ |
| 1155 | 1169 | export async function deleteRepo( |
| 1156 | 1170 | db: Database, |
| 1157 | 1171 | reposRoot: string, |
| 1158 | 1172 | repo: Repo, |
| 1173 | ): Promise<Result<void, "delete_failed" | "has_pulls">> { | |
| 1174 | // Before the first await: from here no fork of it and no pull from or into | |
| 1175 | // it lands (`repo_deleting.ts`), so the checks below stay true until the end. | |
| 1176 | markDeleting(db, repo.id); | |
| 1177 | let deleted: Result<void, "delete_failed" | "has_pulls">; | |
outdated
If removeRepo throws, the mark is left standing for DELETING_MS. Use try/catch, clearing the mark on a throw and rethrowing, and test it. |
||
| 1178 | try { | |
| 1179 | deleted = await removeRepo(db, reposRoot, repo); | |
| 1180 | } catch (error) { | |
| 1181 | // A delete that threw is refused too: its mark must not stand. | |
| 1182 | clearDeleting(db, repo.id); | |
| 1183 | throw error; | |
| 1184 | } | |
| 1185 | // A refused delete leaves the repository as it was, mark and all. | |
| 1186 | if (!deleted.ok) clearDeleting(db, repo.id); | |
| 1187 | return deleted; | |
| 1188 | } | |
| 1189 | ||
| 1190 | /** | |
| 1191 | * `deleteRepo`'s work, under its mark. Everything that can refuse the delete | |
| 1192 | * comes before the row goes; the sealed repositories' directories go only | |
| 1193 | * after it, so a refused delete destroys nothing but its own directory's | |
| 1194 | * files where `rm` got partway. | |
| 1195 | */ | |
| 1196 | async function removeRepo( | |
| 1197 | db: Database, | |
| 1198 | reposRoot: string, | |
| 1199 | repo: Repo, | |
| 1159 | 1200 | ): Promise<Result<void, "delete_failed" | "has_pulls">> { |
| 1160 | 1201 | // A pull names its head by this row, and a pull with no head row reads as |
| 1161 | 1202 | // one from its base's own branch, which runs without approval. So a fork a |
| @@ -1183,6 +1224,8 @@ export async function deleteRepo( | ||
| 1183 | 1224 | return err("delete_failed"); |
| 1184 | 1225 | } |
| 1185 | 1226 | clearForkParent(db, fork.id); |
| 1227 | // A repack can take a while: the mark is time-bound, so it is set again. | |
| 1228 | markDeleting(db, repo.id); | |
| 1186 | 1229 | } |
| 1187 | 1230 | |
| 1188 | 1231 | const dir = repoDir(reposRoot, repo.ownerName, repo.name); |
src/http/api/upstream.ts +3
| @@ -6,6 +6,7 @@ | ||
| 6 | 6 | * `actions/repos.ts`. |
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | import { DELETING_SAYS } from "../../core/repo_deleting.ts"; | |
| 9 | 10 | import { nestedForks } from "../../actions/fork_nested.ts"; |
| 10 | 11 | import { forksSince } from "../../core/repos.ts"; |
| 11 | 12 | import { forkRepo, FORKS_PER_DAY, repositoryLimitMessage } from "../../actions/repos.ts"; |
| @@ -167,6 +168,8 @@ export function upstreamRoutes(router: Router<Handler>): void { | ||
| 167 | 168 | ? tryAgain("limit", 429, `at most ${FORKS_PER_DAY} forks a day: try again tomorrow`, forkWait(ctx.app.db, ctx.auth.user!.id)) |
| 168 | 169 | : forked.error === "fork_failed" |
| 169 | 170 | ? apiError("fork_failed", 422, "the fork could not be created") |
| 171 | : forked.error === "deleting" | |
| 172 | ? apiError("deleting", 409, DELETING_SAYS) | |
| 170 | 173 | : respondAccess(forked.error, 422); |
| 171 | 174 | return json(forked.value, 201); |
| 172 | 175 | }); |
src/http/mcp/tools/fork.ts +2
| @@ -1,4 +1,5 @@ | ||
| 1 | 1 | import { forkRepo, FORKS_PER_DAY, repositoryLimitMessage } from "../../../actions/repos.ts"; |
| 2 | import { DELETING_SAYS } from "../../../core/repo_deleting.ts"; | |
| 2 | 3 | import { VERIFY_FIRST } from "../../../actions/verify_words.ts"; |
| 3 | 4 | import { GUEST_REFUSALS, isGuestRefusal } from "../../../core/contribute.ts"; |
| 4 | 5 | import { NOT_A_MEMBER } from "../../../core/roles.ts"; |
| @@ -70,6 +71,7 @@ export const tool: Tool = { | ||
| 70 | 71 | if (e === "limit") return bad(e, `at most ${FORKS_PER_DAY} forks a day: try again tomorrow`); |
| 71 | 72 | if (e === "already_forked") return bad(e, "you already have a fork of this repository: commit to it"); |
| 72 | 73 | if (e === "not_found") return bad(e, "no such repository"); |
| 74 | if (e === "deleting") return bad(e, DELETING_SAYS); | |
| 73 | 75 | return bad(e, "the fork could not be made"); |
| 74 | 76 | }, |
| 75 | 77 | }; |
src/tui/screens/common.ts +2
| @@ -7,6 +7,7 @@ | ||
| 7 | 7 | * of them. Nothing here knows about repos or issues — that is the point. |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | import { DELETING_SAYS } from "../../core/repo_deleting.ts"; | |
| 10 | 11 | import { CHECK_STALE_REFUSAL } from "../../checks/stale.ts"; |
| 11 | 12 | import { PASSWORD_TOO_LONG } from "../../core/hash-gate.ts"; |
| 12 | 13 | import { refusalText } from "../../http/refusal.ts"; |
| @@ -1016,6 +1017,7 @@ const MESSAGES: Record<string, string> = { | ||
| 1016 | 1017 | unknown_head: "the head branch does not exist", |
| 1017 | 1018 | same_branch: "the base and head branches are the same", |
| 1018 | 1019 | nothing_to_merge: "there is nothing to merge", |
| 1020 | deleting: DELETING_SAYS, | |
| 1019 | 1021 | invalid_branch: "that branch name is not allowed", |
| 1020 | 1022 | conflict: "the branches conflict", |
| 1021 | 1023 | closed: "this pull is closed", |
src/web/pages/pulls.ts +2
| @@ -5,6 +5,7 @@ | ||
| 5 | 5 | * `src/web/studio/lists.ts`). |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | import { DELETING_SAYS } from "../../core/repo_deleting.ts"; | |
| 8 | 9 | import { openPull } from "../../actions/pulls.ts"; |
| 9 | 10 | import { VERIFY_FIRST } from "../../actions/verify_words.ts"; |
| 10 | 11 | import { GUEST_REFUSALS } from "../../core/contribute.ts"; |
| @@ -216,6 +217,7 @@ const COMPOSE_ERRORS: Record<string, string> = { | ||
| 216 | 217 | unknown_head: "That compare branch does not exist.", |
| 217 | 218 | nothing_to_merge: "The compare branch has no commits beyond base.", |
| 218 | 219 | invalid_branch: "That branch name is not valid.", |
| 220 | deleting: `Not opened: ${DELETING_SAYS}.`, | |
| 219 | 221 | sealed_in_public: |
| 220 | 222 | "This fixes a sealed security report, and this branch is public: anyone can read the fix, and with it where the hole is. Work on it privately — open the report and choose “Work on this privately” — or publish anyway below.", |
| 221 | 223 | }; |
src/web/pages/repo.ts +5 −2
| @@ -8,6 +8,7 @@ | ||
| 8 | 8 | * path segment and can never smuggle a path of its own. |
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | import { DELETING_SAYS } from "../../core/repo_deleting.ts"; | |
| 11 | 12 | import { downloadHeaders } from "../../drop/store.ts"; |
| 12 | 13 | import { WAVE_CAP } from "../render/waveform.ts"; |
| 13 | 14 | import { repoLine } from "../describe.ts"; |
| @@ -392,8 +393,10 @@ export async function forkSubmit(ctx: Ctx): Promise<Response> { | ||
| 392 | 393 | : forked.error === "storage_limit" |
| 393 | 394 | ? `Not made: ${storageLimitMessage(ctx.app, ctx.auth.user.id)}.` |
| 394 | 395 | : forked.error === "limit" |
| 395 | ? `That is today's forks made (${FORKS_PER_DAY} a day). Try again tomorrow.` | |
| 396 | : "That fork could not be created."; | |
| 396 | ? `That is today's forks made (${FORKS_PER_DAY} a day). Try again tomorrow.` | |
| 397 | : forked.error === "deleting" | |
| 398 | ? `Not made: ${DELETING_SAYS}.` | |
| 399 | : "That fork could not be created."; | |
| 397 | 400 | return flashTo(ctx, repoBase(repo), { tone: "danger", message: reason }); |
| 398 | 401 | } |
| 399 | 402 | return flashTo(ctx, repoBase(forked.value), { |
test/repo-deleting.test.ts added +229
| @@ -0,0 +1,229 @@ | ||
| 1 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 2 | import { chmod, mkdtemp, rm } from "node:fs/promises"; | |
| 3 | import { existsSync } from "node:fs"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import { createApp, type App } from "../src/app.ts"; | |
| 7 | import { createPull } from "../src/core/pulls.ts"; | |
| 8 | import { clearDeleting, DELETING_MS, DELETING_SAYS, isDeleting, markDeleting } from "../src/core/repo_deleting.ts"; | |
| 9 | import { createRepo, deleteRepo, forkRepo, getRepoById, type Repo } from "../src/core/repos.ts"; | |
| 10 | import { repoDir } from "../src/git/repos.ts"; | |
| 11 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 12 | import { commitFiles } from "../src/git/write.ts"; | |
| 13 | import { signUpProven as signUp } from "./helpers/verified.ts"; | |
| 14 | import { tokenFor } from "./helpers/tokens.ts"; | |
| 15 | import { promote } from "./helpers/members.ts"; | |
| 16 | ||
| 17 | /** | |
| 18 | * The delete mark (#15034): `deleteRepo` sets it before its first await, so a | |
| 19 | * fork or a pull opened while the delete runs is refused with `deleting`; a | |
| 20 | * refused delete takes it back and destroys nothing; and a mark a crash left | |
| 21 | * behind stops standing on its own. | |
| 22 | */ | |
| 23 | ||
| 24 | /** As root, a read-only directory does not stop a write: the failure cannot be made. */ | |
| 25 | const unprivileged = process.getuid?.() !== 0; | |
| 26 | ||
| 27 | describe("a repository being deleted", () => { | |
| 28 | let dataDir: string; | |
| 29 | let app: App; | |
| 30 | let servers: Servers; | |
| 31 | let n = 0; | |
| 32 | ||
| 33 | const user = async (prefix: string) => { | |
| 34 | n += 1; | |
| 35 | const u = await signUp(app, { | |
| 36 | username: `${prefix}${n}`, | |
| 37 | email: `${prefix}${n}@forge.test`, | |
| 38 | password: "a-long-passphrase", | |
| 39 | }); | |
| 40 | if (!u.ok) throw new Error("signup failed"); | |
| 41 | return u.value; | |
| 42 | }; | |
| 43 | ||
| 44 | const commit = async (repo: Repo, branch: string, subject: string) => { | |
| 45 | const c = await commitFiles(repoDir(app.reposRoot, repo.ownerName, repo.name), { | |
| 46 | branch, | |
| 47 | message: subject, | |
| 48 | files: [{ path: `${subject}.txt`, content: subject }], | |
| 49 | author: { name: "T", email: "[email protected]" }, | |
| 50 | }); | |
| 51 | if (!c.ok) throw new Error("commit failed"); | |
| 52 | }; | |
| 53 | ||
| 54 | /** A public parent with one commit on main. */ | |
| 55 | const parent = async () => { | |
| 56 | const owner = await user("owner"); | |
| 57 | const made = await createRepo(app.db, app.reposRoot, { | |
| 58 | ownerId: owner.id, | |
| 59 | name: "parent", | |
| 60 | visibility: "public", | |
| 61 | }); | |
| 62 | if (!made.ok) throw new Error("create failed"); | |
| 63 | await commit(made.value, "main", "one"); | |
| 64 | return { owner, repo: made.value }; | |
| 65 | }; | |
| 66 | ||
| 67 | const fork = async (of: Repo) => { | |
| 68 | const who = await user("forker"); | |
| 69 | const r = await forkRepo(app.db, app.reposRoot, of, who.id); | |
| 70 | if (!r.ok) throw new Error(`fork failed: ${r.error}`); | |
| 71 | return { who, repo: r.value }; | |
| 72 | }; | |
| 73 | ||
| 74 | /** A pull from `head`'s `work` branch into `base`'s main. */ | |
| 75 | const pullFrom = (base: Repo, head: Repo, authorId: string) => | |
| 76 | createPull(app.db, repoDir(app.reposRoot, base.ownerName, base.name), { | |
| 77 | repoId: base.id, | |
| 78 | authorId, | |
| 79 | title: "work", | |
| 80 | baseBranch: "main", | |
| 81 | headBranch: "work", | |
| 82 | headRepoId: head.id, | |
| 83 | headDir: repoDir(app.reposRoot, head.ownerName, head.name), | |
| 84 | }); | |
| 85 | ||
| 86 | beforeAll(async () => { | |
| 87 | dataDir = await mkdtemp(join(tmpdir(), "forge-deleting-")); | |
| 88 | app = createApp({ dataDir, port: 0 }); | |
| 89 | servers = await serve(app); | |
| 90 | }); | |
| 91 | ||
| 92 | afterAll(async () => { | |
| 93 | servers.stop(); | |
| 94 | app.close(); | |
| 95 | await rm(dataDir, { recursive: true, force: true }); | |
| 96 | }); | |
| 97 | ||
| 98 | test("the mark is set before the delete's first await, and a fork asked for meanwhile is refused", async () => { | |
| 99 | const { repo } = await parent(); | |
| 100 | const other = await user("late"); | |
| 101 | const deleting = deleteRepo(app.db, app.reposRoot, repo); | |
| 102 | expect(isDeleting(app.db, [repo.id])).toBe(true); | |
| 103 | expect(await forkRepo(app.db, app.reposRoot, repo, other.id)).toEqual({ ok: false, error: "deleting" }); | |
| 104 | expect(await deleting).toEqual({ ok: true, value: undefined }); | |
| 105 | expect(existsSync(repoDir(app.reposRoot, other.username, repo.name))).toBe(false); | |
| 106 | }); | |
| 107 | ||
| 108 | test("a fork whose copy was under way when the delete began is refused at its write, and leaves no directory", async () => { | |
| 109 | const { repo } = await parent(); | |
| 110 | const other = await user("inflight"); | |
| 111 | // The mark alone, set while the copy runs: the write asks again. | |
| 112 | const forking = forkRepo(app.db, app.reposRoot, repo, other.id); | |
| 113 | markDeleting(app.db, repo.id); | |
| 114 | expect(await forking).toEqual({ ok: false, error: "deleting" }); | |
| 115 | expect(existsSync(repoDir(app.reposRoot, other.username, repo.name))).toBe(false); | |
| 116 | }); | |
| 117 | ||
| 118 | test("a fork racing the whole delete never lands a row that borrows a removed store", async () => { | |
| 119 | const { repo } = await parent(); | |
| 120 | const other = await user("racer"); | |
| 121 | const forking = forkRepo(app.db, app.reposRoot, repo, other.id); | |
| 122 | const deleting = deleteRepo(app.db, app.reposRoot, repo); | |
| 123 | const forked = await forking; | |
| 124 | expect(await deleting).toEqual({ ok: true, value: undefined }); | |
| 125 | // Refused at its write, or its copy failed as the store went: either way, no fork. | |
| 126 | expect(forked.ok).toBe(false); | |
| 127 | expect(existsSync(repoDir(app.reposRoot, other.username, repo.name))).toBe(false); | |
| 128 | }); | |
| 129 | ||
| 130 | test("a pull from a fork being deleted is refused, and so is one into a base being deleted", async () => { | |
| 131 | const { repo: base } = await parent(); | |
| 132 | const { who, repo: head } = await fork(base); | |
| 133 | await commit(head, "work", "two"); | |
| 134 | markDeleting(app.db, head.id); | |
| 135 | expect(await pullFrom(base, head, who.id)).toEqual({ ok: false, error: "deleting" }); | |
| 136 | clearDeleting(app.db, head.id); | |
| 137 | markDeleting(app.db, base.id); | |
| 138 | expect(await pullFrom(base, head, who.id)).toEqual({ ok: false, error: "deleting" }); | |
| 139 | }); | |
| 140 | ||
| 141 | test("a pull whose head was being read when the delete began is refused before its row", async () => { | |
| 142 | const { repo: base } = await parent(); | |
| 143 | const { who, repo: head } = await fork(base); | |
| 144 | await commit(head, "work", "two"); | |
| 145 | // The mark alone, set after createPull's first check passed: the check | |
| 146 | // after its last await is what refuses. | |
| 147 | const opening = pullFrom(base, head, who.id); | |
| 148 | markDeleting(app.db, head.id); | |
| 149 | expect(await opening).toEqual({ ok: false, error: "deleting" }); | |
| 150 | const rows = app.db | |
| 151 | .query<{ n: number }, [string]>("SELECT COUNT(*) AS n FROM pull WHERE head_repo_id = ?1") | |
| 152 | .get(head.id); | |
| 153 | expect(rows?.n).toBe(0); | |
| 154 | }); | |
| 155 | ||
| 156 | test("a delete that throws takes its mark back before the error goes on", async () => { | |
| 157 | const { repo } = await parent(); | |
| 158 | // A name no path may hold: the delete throws as it reaches the directory. | |
| 159 | const broken: Repo = { ...repo, name: "../escape" }; | |
| 160 | await expect(deleteRepo(app.db, app.reposRoot, broken)).rejects.toThrow(); | |
| 161 | expect(isDeleting(app.db, [repo.id])).toBe(false); | |
| 162 | expect(getRepoById(app.db, repo.id)).toBeDefined(); | |
| 163 | }); | |
| 164 | ||
| 165 | test("the API answers a fork of a repository being deleted with 409 deleting", async () => { | |
| 166 | const { repo } = await parent(); | |
| 167 | const other = await user("api"); | |
| 168 | promote(app.db, other); | |
| 169 | markDeleting(app.db, repo.id); | |
| 170 | const res = await fetch(`${servers.url}/api/v1/repos/${repo.ownerName}/${repo.name}/forks`, { | |
| 171 | method: "POST", | |
| 172 | headers: { | |
| 173 | authorization: `Bearer ${tokenFor(app, other.username, "rw")}`, | |
| 174 | "content-type": "application/json", | |
| 175 | }, | |
| 176 | body: "{}", | |
| 177 | }); | |
| 178 | expect(res.status).toBe(409); | |
| 179 | expect(JSON.stringify(await res.json())).toContain(DELETING_SAYS); | |
| 180 | expect(existsSync(repoDir(app.reposRoot, other.username, repo.name))).toBe(false); | |
| 181 | }); | |
| 182 | ||
| 183 | test("a refused delete takes its mark back: the repository forks and pulls again", async () => { | |
| 184 | const { repo: base } = await parent(); | |
| 185 | const { who, repo: head } = await fork(base); | |
| 186 | await commit(head, "work", "two"); | |
| 187 | const opened = await pullFrom(base, head, who.id); | |
| 188 | expect(opened.ok).toBe(true); | |
| 189 | expect(await deleteRepo(app.db, app.reposRoot, head)).toEqual({ ok: false, error: "has_pulls" }); | |
| 190 | expect(isDeleting(app.db, [head.id])).toBe(false); | |
| 191 | const again = await user("again"); | |
| 192 | expect((await forkRepo(app.db, app.reposRoot, head, again.id)).ok).toBe(true); | |
| 193 | }); | |
| 194 | ||
| 195 | test("a mark a crash left behind stops standing once its time is up", async () => { | |
| 196 | const { repo } = await parent(); | |
| 197 | const crashedAt = Date.now() - DELETING_MS - 1000; | |
| 198 | markDeleting(app.db, repo.id, crashedAt); | |
| 199 | // Nothing cleared it: the process that set it is gone. | |
| 200 | expect(getRepoById(app.db, repo.id)).toBeDefined(); | |
| 201 | expect(isDeleting(app.db, [repo.id])).toBe(false); | |
| 202 | const other = await user("after"); | |
| 203 | expect((await forkRepo(app.db, app.reposRoot, repo, other.id)).ok).toBe(true); | |
| 204 | // The same mark, still within its time, refuses. | |
| 205 | markDeleting(app.db, repo.id, Date.now() - DELETING_MS + 60_000); | |
| 206 | const third = await user("within"); | |
| 207 | expect(await forkRepo(app.db, app.reposRoot, repo, third.id)).toEqual({ ok: false, error: "deleting" }); | |
| 208 | }); | |
| 209 | ||
| 210 | test.skipIf(!unprivileged)("a delete refused at a detach destroys no sealed repository", async () => { | |
| 211 | const { repo: base } = await parent(); | |
| 212 | const reporter = await user("sealer"); | |
| 213 | const sealed = await forkRepo(app.db, app.reposRoot, base, reporter.id, undefined, { sealed: true }); | |
| 214 | if (!sealed.ok) throw new Error(`sealed fork failed: ${sealed.error}`); | |
| 215 | const { repo: plain } = await fork(base); | |
| 216 | const sealedDir = repoDir(app.reposRoot, sealed.value.ownerName, sealed.value.name); | |
| 217 | const pack = join(repoDir(app.reposRoot, plain.ownerName, plain.name), "objects", "pack"); | |
| 218 | await chmod(pack, 0o555); | |
| 219 | try { | |
| 220 | expect(await deleteRepo(app.db, app.reposRoot, base)).toEqual({ ok: false, error: "delete_failed" }); | |
| 221 | } finally { | |
| 222 | await chmod(pack, 0o755); | |
| 223 | } | |
| 224 | expect(existsSync(join(sealedDir, "HEAD"))).toBe(true); | |
| 225 | expect(getRepoById(app.db, sealed.value.id)?.sealedFor).toBe(base.id); | |
| 226 | expect(getRepoById(app.db, base.id)).toBeDefined(); | |
| 227 | expect(isDeleting(app.db, [base.id])).toBe(false); | |
| 228 | }); | |
| 229 | }); |
new above
offline
This re-check after the last await is untested: no test would fail if it were removed. Add one that marks the repo while createPull is in flight.