From 146b8d529bfee62dc88dfe17f03faa80f58dbbe6 Mon Sep 17 00:00:00 2001 From: dawn Date: Mon, 19 Oct 2026 16:57:42 +0300 Subject: [PATCH] web: write repo settings, collaborators and deletes to knot2 records Signed-off-by: dawn --- web/src/lib/api/cob.ts | 25 ++ web/src/lib/api/repoCreate.ts | 18 +- web/src/lib/api/repoSettings.test.ts | 296 +++++++++++------- web/src/lib/api/repoSettings.ts | 163 +++++----- .../settings/RepoGeneralForm.svelte | 32 +- .../[repo]/settings/access/+page.svelte | 3 +- .../[repo]/settings/access/new/+page.svelte | 2 +- .../[repo]/settings/pipelines/+page.svelte | 12 +- .../[repo]/settings/rename/+page.svelte | 7 +- 9 files changed, 335 insertions(+), 223 deletions(-) diff --git a/web/src/lib/api/cob.ts b/web/src/lib/api/cob.ts index 78ddfe043..0dff8a6c2 100644 --- a/web/src/lib/api/cob.ts +++ b/web/src/lib/api/cob.ts @@ -167,6 +167,31 @@ export const eraseCob = async ( }); }; +// a grant write answers with an empty body, so unlike a cob there's no uri to attest +export const createGrant = async ( + agent: OAuthUserAgent, + knot: KnotOrigin, + repo: Did, + collection: Nsid, + subject: Did +): Promise => { + await knotProcedure(agent, knot, repo, CREATE_NSID, { + collection, + rkey: subject, + record: { $type: collection, createdAt: new Date().toISOString() } + }); +}; + +export const eraseGrant = async ( + agent: OAuthUserAgent, + knot: KnotOrigin, + repo: Did, + collection: Nsid, + subject: Did +): Promise => { + await knotProcedure(agent, knot, repo, DELETE_NSID, { collection, rkey: subject }); +}; + export const readCob = async (knot: KnotOrigin, uri: AtUri): Promise> => { const { repo, collection, rkey } = parseCanonicalResourceUri(uri); const response = await fetch( diff --git a/web/src/lib/api/repoCreate.ts b/web/src/lib/api/repoCreate.ts index 0723a1118..0adee1d97 100644 --- a/web/src/lib/api/repoCreate.ts +++ b/web/src/lib/api/repoCreate.ts @@ -7,7 +7,7 @@ import { hostForKnot, serviceUrlFor } from "$lib/api/_request"; import { createBobbinClient } from "$lib/api/client"; import { mainSchema as createRepoSchema } from "$lib/api/lexicons/types/org/tangled/repo/createRepo"; import type { Main as DeclarationRecord } from "$lib/api/lexicons/types/org/tangled/repo/declaration"; -import { mainSchema as deleteRepoSchema } from "$lib/api/lexicons/types/sh/tangled/repo/delete"; +import { mainSchema as deleteRepoSchema } from "$lib/api/lexicons/types/org/tangled/repo/deleteRepo"; import { createRecord, deleteRecord, readRecord, type WrittenRecord } from "$lib/api/write"; import { pingIndexNow } from "$lib/api/indexnow"; import { serviceDidForHost } from "$lib/auth/agent"; @@ -77,7 +77,6 @@ const deleteDeclaration = async ( const deleteKnotRepo = async ( agent: OAuthUserAgent, input: RepoCreationInput, - rkey: string, repoDid: Did ): Promise => { const knotHost = hostForKnot(input.knot); @@ -89,16 +88,7 @@ const deleteKnotRepo = async ( for (const delay of [0, 2_000, 5_000]) { if (delay > 0) await sleep(delay); try { - await ok( - ctx.xrpc.call(deleteRepoSchema, { - input: { - repo: repoDid, - did: input.ownerDid, - name: rkey, - rkey: recordKeyOf(rkey) - } - }) - ); + await ok(ctx.xrpc.call(deleteRepoSchema, { input: { repo: repoDid } })); return null; } catch (cause) { lastError = messageOf(cause); @@ -167,9 +157,7 @@ export const createRepo = async ( await awaitIndexedRecord(createBobbinClient({ serviceUrl: bobbinUrl }), written); } catch (cause) { const recordRollback = written ? await deleteDeclaration(agent, rkey, written.cid) : null; - const knotRollback = recordRollback - ? null - : await deleteKnotRepo(agent, input, rkey, repoDid); + const knotRollback = recordRollback ? null : await deleteKnotRepo(agent, input, repoDid); const rollback = [recordRollback, knotRollback].filter(Boolean).join("; "); throw new Error( `Failed to create repository: ${messageOf(cause)}${rollback ? `. Also, ${rollback}.` : ""}`, diff --git a/web/src/lib/api/repoSettings.test.ts b/web/src/lib/api/repoSettings.test.ts index cf916e3ce..e42c3d125 100644 --- a/web/src/lib/api/repoSettings.test.ts +++ b/web/src/lib/api/repoSettings.test.ts @@ -2,70 +2,85 @@ import { beforeEach, describe, expect, it, vi } from "vitest"; import { ClientResponseError } from "@atcute/client"; import type { OAuthUserAgent } from "@atcute/oauth-browser-client"; import type { BobbinContext } from "$lib/api/client"; -import type { RecordView, LegacyRepoRecord as RepoRecord } from "$lib/api/records"; -import { recordKeyOf } from "$lib/api/syntax"; +import type { RepoRecord } from "$lib/api/records"; -const putRecord = vi.fn(async () => ({ - uri: "at://did:plc:owner/sh.tangled.repo/core", +const manifest: RepoRecord = { + $type: "org.tangled.repo.manifest", + declaration: { owner: "did:plc:owner", name: "core" }, + createdAt: "2026-01-01T00:00:00Z" as never, + description: "a forge", + website: "https://tangled.org" +}; +const manifestUri = "at://did:plc:repo/org.tangled.repo.manifest/self"; +const written = { + uri: manifestUri, cid: "bafyreihkszkjxz4fuml3wol5lujacnrlfhwutc2o7yj2hdsc3kr6usccyu" -})); -const deleteRecord = vi.fn(async () => undefined); +}; + +const updateCob = vi.fn( + async ( + _agent: unknown, + _knot: unknown, + _uri: unknown, + mutate: (m: RepoRecord) => RepoRecord + ) => ({ + ...written, + value: mutate(manifest) + }) +); +const createGrant = vi.fn(async () => undefined); +const eraseGrant = vi.fn(async () => undefined); +vi.mock("./cob", () => ({ updateCob, createGrant, eraseGrant })); + +const declaration = { + value: { + $type: "org.tangled.repo.declaration", + repo: "did:plc:repo", + createdAt: "2026-01-01T00:00:00Z" + }, + written: { uri: "at://did:plc:owner/org.tangled.repo.declaration/core", cid: "bafyold" } +}; +const readRecord = vi.fn(async (_agent: unknown, _collection: unknown, rkey: string) => + rkey === "core" ? declaration : null +); const createRecord = vi.fn(async () => ({ - uri: "at://did:plc:owner/sh.tangled.repo/core", - cid: "bafyreihkszkjxz4fuml3wol5lujacnrlfhwutc2o7yj2hdsc3kr6usccyu" + uri: "at://did:plc:owner/org.tangled.repo.declaration/forge", + cid: "bafynew" })); +const deleteRecord = vi.fn(async () => undefined); +vi.mock("./write", () => ({ createRecord, deleteRecord, readRecord })); + const awaitIndexedRecord = vi.fn(async () => undefined); const awaitDeletedRecord = vi.fn(async () => true); -const pingIndexNow = vi.fn(); - -const readRecord = vi.fn(async () => ({ - value: view.value, - written: { uri: view.uri, cid: view.cid } -})); - -vi.mock("./write", () => ({ createRecord, putRecord, deleteRecord, readRecord })); vi.mock("./awaitRecord", () => ({ awaitIndexedRecord, awaitDeletedRecord })); +const pingIndexNow = vi.fn(); vi.mock("$lib/api/indexnow", () => ({ pingIndexNow })); const call = vi.fn(async (_schema: unknown, _options: unknown) => ({ ok: true, data: undefined })); -const post = vi.fn( - async (_name: unknown, _options: { as: null; input: Record }) => ({ - ok: true, - data: undefined - }) -); vi.mock("./client", async (importOriginal) => ({ ...(await importOriginal()), createBobbinClient: () => ({ xrpc: { call } }) as unknown as BobbinContext })); vi.mock("./_request", async (importOriginal) => ({ ...(await importOriginal()), - serviceClient: () => ({ xrpc: { call, post } }) as unknown as BobbinContext + serviceClient: () => ({ xrpc: { call } }) as unknown as BobbinContext })); const api = await import("./repoSettings"); const agent = { sub: "did:plc:owner" } as unknown as OAuthUserAgent; const bobbin = "https://bobbin.test"; -const view: RecordView = { - uri: "at://did:plc:owner/sh.tangled.repo/core", - cid: "bafyreihajxafvvlkhv3bpuanc7ol7yuyhiq43katensu647j2x4pqsswti", - value: { - $type: "sh.tangled.repo", - knot: "knot.test", - createdAt: "2026-01-01T00:00:00Z" as never, - description: "a forge", - website: "https://tangled.org" - } -}; +const repo = { uri: manifestUri, knot: "knot.test", rkey: "core" }; + +beforeEach(() => vi.clearAllMocks()); -describe("editedRepoRecord", () => { +describe("editedManifest", () => { it("keeps untouched fields and drops the ones an edit clears", () => { expect( - api.editedRepoRecord(view.value, { description: undefined, topics: ["svelte"] }) + api.editedManifest(manifest, { description: undefined, topics: ["svelte"] }) ).toEqual({ - $type: "sh.tangled.repo", - knot: "knot.test", + $type: "org.tangled.repo.manifest", + declaration: { owner: "did:plc:owner", name: "core" }, createdAt: "2026-01-01T00:00:00Z", website: "https://tangled.org", topics: ["svelte"] @@ -73,97 +88,163 @@ describe("editedRepoRecord", () => { }); it("drops an empty list rather than writing it", () => { - expect(api.editedRepoRecord(view.value, { topics: [] })).not.toHaveProperty("topics"); + expect(api.editedManifest(manifest, { topics: [] })).not.toHaveProperty("topics"); + }); + + it("keeps the default branch when the edit has none", () => { + const onMain = { ...manifest, defaultBranch: "main" }; + expect(api.editedManifest(onMain, { defaultBranch: undefined }).defaultBranch).toBe("main"); + expect(api.editedManifest(onMain, { defaultBranch: "trunk" }).defaultBranch).toBe("trunk"); }); }); -describe("saveRepoRecord", () => { - it("swaps on the cid it read and waits for the appview", async () => { - await api.saveRepoRecord(agent, bobbin, recordKeyOf("core"), { - description: "a forge for git" - }); +describe("editManifest", () => { + it("edits the manifest on the knot and waits for the appview", async () => { + await api.editManifest(agent, bobbin, repo, { defaultBranch: "trunk" }); - expect(putRecord).toHaveBeenCalledWith( + expect(updateCob).toHaveBeenCalledWith( agent, - "sh.tangled.repo", - "core", - expect.objectContaining({ description: "a forge for git" }), - "bafyreihajxafvvlkhv3bpuanc7ol7yuyhiq43katensu647j2x4pqsswti" + "knot.test", + manifestUri, + expect.any(Function) + ); + expect((await updateCob.mock.results[0].value).value).toMatchObject({ + defaultBranch: "trunk" + }); + expect(awaitIndexedRecord).toHaveBeenCalledWith( + expect.anything(), + expect.objectContaining(written) ); - expect(awaitIndexedRecord).toHaveBeenCalled(); }); }); -describe("knot procedures", () => { - it("names the repo by DID", async () => { - await api.setDefaultBranch(agent, "knot.test", "did:plc:repo", "trunk"); - expect(call.mock.calls.at(-1)?.[1]).toEqual({ - input: { repo: "did:plc:repo", defaultBranch: "trunk" } +describe("renameRepo", () => { + it("only edits the manifest when the record key stays the same", async () => { + await api.renameRepo(agent, bobbin, repo, "Core"); + + expect((await updateCob.mock.results[0].value).value.declaration.name).toBe("Core"); + expect(createRecord).not.toHaveBeenCalled(); + expect(deleteRecord).not.toHaveBeenCalled(); + }); + + it("moves the declaration around the manifest edit", async () => { + const order: string[] = []; + createRecord.mockImplementationOnce(async () => { + order.push("new declaration"); + return { uri: "at://did:plc:owner/org.tangled.repo.declaration/forge", cid: "bafynew" }; + }); + updateCob.mockImplementationOnce(async (_a, _k, _u, mutate) => { + order.push("manifest"); + return { ...written, value: mutate(manifest) }; }); + deleteRecord.mockImplementationOnce(async () => { + order.push("old declaration"); + }); + + await api.renameRepo(agent, bobbin, repo, "Forge"); + + expect(order).toEqual(["new declaration", "manifest", "old declaration"]); + expect(createRecord).toHaveBeenCalledWith( + agent, + "org.tangled.repo.declaration", + declaration.value, + "forge" + ); + expect(deleteRecord).toHaveBeenCalledWith( + agent, + "org.tangled.repo.declaration", + "core", + "bafyold" + ); + }); + + it("takes the new declaration back down when the knot refuses the rename", async () => { + updateCob.mockRejectedValueOnce( + new Error("the record key forge already belongs to another repository") + ); + + await expect(api.renameRepo(agent, bobbin, repo, "Forge")).rejects.toThrow( + /another repository/ + ); + + expect(deleteRecord).toHaveBeenCalledTimes(1); + expect(deleteRecord).toHaveBeenCalledWith( + agent, + "org.tangled.repo.declaration", + "forge", + "bafynew" + ); + }); + + it("refuses a name the account already has without writing anything", async () => { + readRecord.mockImplementation(async () => declaration); + + await expect(api.renameRepo(agent, bobbin, repo, "Taken")).rejects.toThrow( + /already exists/ + ); + expect(createRecord).not.toHaveBeenCalled(); + expect(updateCob).not.toHaveBeenCalled(); + readRecord.mockImplementation(async (_a, _c, rkey: string) => + rkey === "core" ? declaration : null + ); + }); +}); + +describe("collaborators", () => { + it("are grant records on the repo, keyed by the collaborator", async () => { await api.addCollaborator(agent, "knot.test", "did:plc:repo", "did:plc:friend"); - expect(call.mock.calls.at(-1)?.[1]).toEqual({ - input: { repo: "did:plc:repo", subject: "did:plc:friend" } - }); + expect(createGrant).toHaveBeenCalledWith( + agent, + "knot.test", + "did:plc:repo", + "org.tangled.graph.collaborator", + "did:plc:friend" + ); + + await api.removeCollaborator(agent, "knot.test", "did:plc:repo", "did:plc:friend"); + expect(eraseGrant).toHaveBeenCalledWith( + agent, + "knot.test", + "did:plc:repo", + "org.tangled.graph.collaborator", + "did:plc:friend" + ); }); }); describe("deleteRepo", () => { - beforeEach(() => vi.clearAllMocks()); - const location = { - uri: view.uri, + rkey: "core", + ownerDid: "did:plc:owner", knot: "knot.test", name: "core", ownerHandle: "alice", - ownerDid: "did:plc:owner", - repoDid: "did:plc:repo", - cid: view.cid + repoDid: "did:plc:repo" }; - it("drops the record before the knot repository", async () => { + + it("drops the declaration before asking the knot", async () => { const order: string[] = []; deleteRecord.mockImplementationOnce(async () => { - order.push("record"); - return undefined; + order.push("declaration"); }); - post.mockImplementationOnce(async () => { + call.mockImplementationOnce(async () => { order.push("knot"); return { ok: true, data: undefined }; }); await api.deleteRepo(agent, bobbin, location); - expect(order).toEqual(["record", "knot"]); - expect(deleteRecord).toHaveBeenCalledWith( - agent, - "sh.tangled.repo", - "core", - "bafyreihajxafvvlkhv3bpuanc7ol7yuyhiq43katensu647j2x4pqsswti" - ); - expect(post.mock.calls.at(-1)?.[0]).toBe("sh.tangled.repo.delete"); - expect(post.mock.calls.at(-1)?.[1]).toEqual({ - as: null, - input: { - repo: "did:plc:repo", - did: "did:plc:owner", - name: "core", - rkey: "core" - } - }); - expect(awaitDeletedRecord).toHaveBeenCalled(); - }); - - it("names a repository whose record predates repo DIDs", async () => { - await api.deleteRepo(agent, bobbin, { ...location, repoDid: undefined }); - - expect(post.mock.calls.at(-1)?.[1]?.input).toEqual({ - did: "did:plc:owner", - name: "core", - rkey: "core" - }); + expect(order).toEqual(["declaration", "knot"]); + expect(deleteRecord).toHaveBeenCalledWith(agent, "org.tangled.repo.declaration", "core"); + expect(call.mock.calls[0][1]).toEqual({ input: { repo: "did:plc:repo" } }); + expect(awaitDeletedRecord).toHaveBeenCalledWith( + expect.anything(), + "at://did:plc:owner/org.tangled.repo.declaration/core" + ); }); - it("carries on when the record is already gone", async () => { + it("carries on when the declaration is already gone", async () => { deleteRecord.mockRejectedValueOnce( new ClientResponseError({ status: 400, @@ -173,23 +254,24 @@ describe("deleteRepo", () => { await api.deleteRepo(agent, bobbin, location); - expect(post).toHaveBeenCalled(); + expect(call).toHaveBeenCalled(); }); - it("stops when the record moved under a concurrent edit", async () => { + it("stops when the pds refuses for another reason", async () => { deleteRecord.mockRejectedValueOnce( - new ClientResponseError({ - status: 400, - data: { error: "InvalidSwap", message: "record moved" } - }) + new ClientResponseError({ status: 500, data: { error: "InternalServerError" } }) ); - await expect(api.deleteRepo(agent, bobbin, location)).rejects.toThrow(/InvalidSwap/); - expect(post).not.toHaveBeenCalled(); + await expect(api.deleteRepo(agent, bobbin, location)).rejects.toThrow( + /InternalServerError/ + ); + expect(call).not.toHaveBeenCalled(); }); - it("reports a knot that refuses after the record is gone", async () => { - post.mockRejectedValueOnce(new Error("repo already exists: core")); + it("reports a knot that refuses after the declaration is gone", async () => { + call.mockRejectedValueOnce( + new Error("only repository owner or a knot admin may delete it") + ); await expect(api.deleteRepo(agent, bobbin, location)).rejects.toThrow( /did not delete core/ diff --git a/web/src/lib/api/repoSettings.ts b/web/src/lib/api/repoSettings.ts index 556fb963c..b526ac0dd 100644 --- a/web/src/lib/api/repoSettings.ts +++ b/web/src/lib/api/repoSettings.ts @@ -1,148 +1,169 @@ import { ClientResponseError, ok } from "@atcute/client"; -import type { Cid, ResourceUri } from "@atcute/lexicons/syntax"; +import type { ResourceUri } from "@atcute/lexicons/syntax"; import type { OAuthUserAgent } from "@atcute/oauth-browser-client"; import { awaitDeletedRecord, awaitIndexedRecord } from "$lib/api/awaitRecord"; import { serviceClient } from "$lib/api/_request"; import { createBobbinClient } from "$lib/api/client"; +import { createGrant, eraseGrant, updateCob, type CobRef } from "$lib/api/cob"; import { pingIndexNow } from "$lib/api/indexnow"; -import { mainSchema as addCollaboratorSchema } from "$lib/api/lexicons/types/sh/tangled/repo/addCollaborator"; -import { cidOf, didOf, nsidOf, recordKeyOf, type Rkey } from "$lib/api/syntax"; -import { - mainSchema as deleteRepoSchema, - type $input as DeleteRepoInput -} from "$lib/api/lexicons/types/sh/tangled/repo/delete"; -import { mainSchema as removeCollaboratorSchema } from "$lib/api/lexicons/types/sh/tangled/repo/removeCollaborator"; -import { mainSchema as setDefaultBranchSchema } from "$lib/api/lexicons/types/sh/tangled/repo/setDefaultBranch"; -import type { LegacyRepoRecord as RepoRecord } from "$lib/api/records"; -import { rkeyFromUri } from "$lib/api/uri"; -import { deleteRecord, putRecord, readRecord, type WrittenRecord } from "$lib/api/write"; +import { mainSchema as deleteRepoSchema } from "$lib/api/lexicons/types/org/tangled/repo/deleteRepo"; +import type { Main as DeclarationRecord } from "$lib/api/lexicons/types/org/tangled/repo/declaration"; +import type { RepoRecord } from "$lib/api/records"; +import { knotFor } from "$lib/api/repo"; +import { atUriOf, didOf, nsidOf, recordKeyOf } from "$lib/api/syntax"; +import { createRecord, deleteRecord, readRecord } from "$lib/api/write"; -const REPO_COLLECTION = nsidOf("sh.tangled.repo"); +const DECLARATION_COLLECTION = nsidOf("org.tangled.repo.declaration"); +const COLLABORATOR_COLLECTION = nsidOf("org.tangled.graph.collaborator"); -export interface RepoRecordEdit { - name?: string; +export interface ManifestEdit { description?: string; website?: string; topics?: readonly string[]; spindle?: string; - labels?: readonly string[]; + defaultBranch?: string; } const empty = (value: unknown): boolean => value === undefined || value === "" || (Array.isArray(value) && value.length === 0); -export const editedRepoRecord = (record: RepoRecord, edit: RepoRecordEdit): RepoRecord => +// the knot refuses to clear the default branch once it's set, so an empty one keeps what's there +export const editedManifest = (manifest: RepoRecord, edit: ManifestEdit): RepoRecord => Object.fromEntries( - Object.entries({ ...record, ...edit, $type: "sh.tangled.repo" }).filter( - ([, value]) => !empty(value) - ) + Object.entries({ + ...manifest, + ...edit, + defaultBranch: edit.defaultBranch || manifest.defaultBranch, + $type: "org.tangled.repo.manifest" + }).filter(([, value]) => !empty(value)) ) as RepoRecord; -export const saveRepoRecord = async ( +export interface RepoHome { + uri: string; + knot: string; +} + +const saveManifest = async ( agent: OAuthUserAgent, bobbinUrl: string, - rkey: Rkey, - edit: RepoRecordEdit -): Promise => { - const current = await readRecord(agent, REPO_COLLECTION, rkey); - if (!current) throw new Error("This repository has no record in your account to edit."); - const written = await putRecord( + repo: RepoHome, + mutate: (manifest: RepoRecord) => RepoRecord +): Promise => { + const written = await updateCob( agent, - REPO_COLLECTION, - rkey, - editedRepoRecord(current.value, edit), - current.written?.cid + knotFor(repo.knot), + atUriOf(repo.uri), + mutate ); await awaitIndexedRecord(createBobbinClient({ serviceUrl: bobbinUrl }), written); return written; }; -export const setDefaultBranch = async ( +// the knot moves HEAD itself when the default branch changes +export const editManifest = ( agent: OAuthUserAgent, - knot: string, - repoDid: string, - defaultBranch: string + bobbinUrl: string, + repo: RepoHome, + edit: ManifestEdit +): Promise => + saveManifest(agent, bobbinUrl, repo, (manifest) => editedManifest(manifest, edit)); + +export interface RenamedRepo extends RepoHome { + rkey: string; +} + +// the declaration lives at the lowercased name, so it moves. new one up first, old one down last, +// so a rename that dies halfway never leaves the repo without a declaration +export const renameRepo = async ( + agent: OAuthUserAgent, + bobbinUrl: string, + repo: RenamedRepo, + name: string ): Promise => { - await ok( - serviceClient(agent, knot).xrpc.call(setDefaultBranchSchema, { - input: { repo: didOf(repoDid), defaultBranch } - }) - ); + const from = recordKeyOf(repo.rkey); + const to = recordKeyOf(name.toLowerCase()); + const rename = (manifest: RepoRecord): RepoRecord => ({ + ...manifest, + declaration: { ...manifest.declaration, name } + }); + if (from === to) { + await saveManifest(agent, bobbinUrl, repo, rename); + return; + } + if (await readRecord(agent, DECLARATION_COLLECTION, to)) { + throw new Error(`A repository named "${name}" already exists in your account.`); + } + const current = await readRecord(agent, DECLARATION_COLLECTION, from); + if (!current) throw new Error("This repository has no declaration in your account to move."); + const moved = await createRecord(agent, DECLARATION_COLLECTION, current.value, to); + try { + await saveManifest(agent, bobbinUrl, repo, rename); + } catch (cause) { + await deleteRecord(agent, DECLARATION_COLLECTION, to, moved.cid).catch(() => {}); + throw cause; + } + await deleteRecord(agent, DECLARATION_COLLECTION, from, current.written?.cid); }; -export const addCollaborator = async ( +export const addCollaborator = ( agent: OAuthUserAgent, knot: string, repoDid: string, subject: string -): Promise => { - await ok( - serviceClient(agent, knot).xrpc.call(addCollaboratorSchema, { - input: { repo: didOf(repoDid), subject: didOf(subject) } - }) - ); -}; +): Promise => + createGrant(agent, knotFor(knot), didOf(repoDid), COLLABORATOR_COLLECTION, didOf(subject)); -export const removeCollaborator = async ( +export const removeCollaborator = ( agent: OAuthUserAgent, knot: string, repoDid: string, subject: string -): Promise => { - await ok( - serviceClient(agent, knot).xrpc.call(removeCollaboratorSchema, { - input: { repo: didOf(repoDid), subject: didOf(subject) } - }) - ); -}; +): Promise => + eraseGrant(agent, knotFor(knot), didOf(repoDid), COLLABORATOR_COLLECTION, didOf(subject)); const isRecordGone = (cause: unknown): boolean => cause instanceof ClientResponseError && (cause.status === 404 || cause.error === "RecordNotFound"); export interface RepoLocation { - uri: string; + rkey: string; knot: string; name: string; - ownerHandle: string; ownerDid: string; + ownerHandle: string; repoDid?: string; - cid?: Cid; } +// the knot refuses to delete a repo while its declaration is still on the owner's pds, so that goes first export const deleteRepo = async ( agent: OAuthUserAgent, bobbinUrl: string, repo: RepoLocation ): Promise => { - const rkey = rkeyFromUri(repo.uri); + if (!repo.repoDid) throw new Error("This repository has no DID on its knot yet."); try { - await deleteRecord(agent, REPO_COLLECTION, rkey, repo.cid ? cidOf(repo.cid) : undefined); + await deleteRecord(agent, DECLARATION_COLLECTION, recordKeyOf(repo.rkey)); } catch (cause) { if (!isRecordGone(cause)) throw cause; } try { await ok( - serviceClient(agent, repo.knot).xrpc.post(deleteRepoSchema.nsid, { - as: null, - input: { - did: didOf(repo.ownerDid), - name: repo.name, - rkey: recordKeyOf(rkey), - ...(repo.repoDid ? { repo: didOf(repo.repoDid) } : {}) - } as DeleteRepoInput + serviceClient(agent, repo.knot).xrpc.call(deleteRepoSchema, { + input: { repo: didOf(repo.repoDid) } }) ); } catch (cause) { const detail = cause instanceof Error ? cause.message : String(cause); throw new Error( - `The record is gone, but ${repo.knot} did not delete ${repo.name}: ${detail}`, + `The declaration is gone, but ${repo.knot} did not delete ${repo.name}: ${detail}`, { cause } ); } + // bobbin keeps the manifest of a repo the knot dropped, but it lists repos by their declaration + const declaration = `at://${repo.ownerDid}/${DECLARATION_COLLECTION}/${repo.rkey}`; const confirmed = await awaitDeletedRecord( createBobbinClient({ serviceUrl: bobbinUrl }), - repo.uri as ResourceUri + declaration as ResourceUri ); if (!confirmed) { throw new Error("Deleted, but the appview has not caught up yet."); diff --git a/web/src/lib/components/settings/RepoGeneralForm.svelte b/web/src/lib/components/settings/RepoGeneralForm.svelte index 7b4f592ee..5c74a1aa6 100644 --- a/web/src/lib/components/settings/RepoGeneralForm.svelte +++ b/web/src/lib/components/settings/RepoGeneralForm.svelte @@ -3,10 +3,9 @@ import { resolve } from "$app/paths"; import { createAction } from "$lib/action.svelte"; import { deleteRepoLabel, type LabelView } from "$lib/api/labels"; - import { recordKeyOf } from "$lib/api/syntax"; import { knotFor } from "$lib/api/repo"; import type { RecordView, RepoRecord } from "$lib/api/records"; - import { deleteRepo, saveRepoRecord, setDefaultBranch } from "$lib/api/repoSettings"; + import { deleteRepo, editManifest } from "$lib/api/repoSettings"; import { getAuth } from "$lib/auth.svelte"; import { guardUnsavedChanges } from "$lib/unsavedChanges"; import type { RepoInfo } from "$lib/components/repo/types"; @@ -65,13 +64,12 @@ const topicList = $derived(form.topics.map((tag) => tag.value)); const sameList = (a: readonly string[], b: readonly string[]) => a.length === b.length && a.every((value, index) => value === b[index]); - const recordChanged = $derived( + const dirty = $derived( form.description !== saved.description || form.website !== saved.website || - !sameList(topicList, saved.topics) + !sameList(topicList, saved.topics) || + form.defaultBranch !== saved.defaultBranch ); - const branchChanged = $derived(form.defaultBranch !== saved.defaultBranch); - const dirty = $derived(recordChanged || branchChanged); const branchOptions = $derived( branches.length > 0 ? branches : [saved.defaultBranch].filter(Boolean) @@ -81,17 +79,12 @@ const agent = auth.agent; if (!agent) return; try { - if (recordChanged) { - await saveRepoRecord(agent, bobbinUrl, recordKeyOf(repo.rkey), { - description: form.description.trim() || undefined, - website: form.website.trim() || undefined, - topics: topicList - }); - } - if (branchChanged) { - if (!repo.repoDid) throw new Error("This repository has no DID on its knot yet."); - await setDefaultBranch(agent, repo.knot, repo.repoDid, form.defaultBranch); - } + await editManifest(agent, bobbinUrl, repo, { + description: form.description.trim() || undefined, + website: form.website.trim() || undefined, + topics: topicList, + defaultBranch: form.defaultBranch || undefined + }); } finally { await invalidateAll(); } @@ -120,10 +113,13 @@ let confirmDelete = $state(false); let confirmName = $state(""); + $effect(() => { + if (!confirmDelete) confirmName = ""; + }); const destroy = createAction(async () => { const agent = auth.agent; if (!agent) return; - await deleteRepo(agent, bobbinUrl, { ...repo, cid: record.cid }); + await deleteRepo(agent, bobbinUrl, repo); confirmDelete = false; await goto(resolve(`/${repo.ownerHandle}` as "/"), { invalidateAll: true }); }); diff --git a/web/src/routes/[handle]/[repo]/settings/access/+page.svelte b/web/src/routes/[handle]/[repo]/settings/access/+page.svelte index 24cb647f4..b03f954f1 100644 --- a/web/src/routes/[handle]/[repo]/settings/access/+page.svelte +++ b/web/src/routes/[handle]/[repo]/settings/access/+page.svelte @@ -55,8 +55,7 @@ title="Collaborators" description={[ "Any user added as a collaborator will be able to push commits and tags to this repository,", - "upload releases, and workflows.", - "An invited user holds no access until they accept the offer." + "upload releases, and workflows." ]} separator > diff --git a/web/src/routes/[handle]/[repo]/settings/access/new/+page.svelte b/web/src/routes/[handle]/[repo]/settings/access/new/+page.svelte index 7c2c8ad58..5ecf09db4 100644 --- a/web/src/routes/[handle]/[repo]/settings/access/new/+page.svelte +++ b/web/src/routes/[handle]/[repo]/settings/access/new/+page.svelte @@ -36,7 +36,7 @@ backLabel="Access" backHref={back} title="Add collaborator" - description="Collaborators can push to this repository once they accept the invite." + description="Collaborators can push to this repository as soon as you add them." > diff --git a/web/src/routes/[handle]/[repo]/settings/pipelines/+page.svelte b/web/src/routes/[handle]/[repo]/settings/pipelines/+page.svelte index d6743dc2e..94ba330c7 100644 --- a/web/src/routes/[handle]/[repo]/settings/pipelines/+page.svelte +++ b/web/src/routes/[handle]/[repo]/settings/pipelines/+page.svelte @@ -5,8 +5,8 @@ import { serviceUrlFor } from "$lib/api/_request"; import { createBobbinClient } from "$lib/api/client"; import { getPreferences } from "$lib/api/preferences"; - import { saveRepoRecord } from "$lib/api/repoSettings"; - import { recordKeyOf } from "$lib/api/syntax"; + import { editManifest } from "$lib/api/repoSettings"; + import { serviceDidForHost } from "$lib/auth/agent"; import { spindleTargetOf } from "$lib/api/spindlewrite"; import { listSecrets, removeSecret, type Secret } from "$lib/api/secrets"; import { getAuth } from "$lib/auth.svelte"; @@ -63,8 +63,12 @@ const save = createAction(async () => { const agent = auth.agent; if (!agent) return; - await saveRepoRecord(agent, data.publicConfig.bobbinUrl, recordKeyOf(repo.rkey), { - spindle: chosen || undefined + // the manifest names the spindle by did, the picker by host + await editManifest(agent, data.publicConfig.bobbinUrl, repo, { + spindle: + !chosen || chosen.startsWith("did:") + ? chosen || undefined + : serviceDidForHost(chosen) }); await invalidateAll(); }); diff --git a/web/src/routes/[handle]/[repo]/settings/rename/+page.svelte b/web/src/routes/[handle]/[repo]/settings/rename/+page.svelte index 38f5bf60a..f54b3e766 100644 --- a/web/src/routes/[handle]/[repo]/settings/rename/+page.svelte +++ b/web/src/routes/[handle]/[repo]/settings/rename/+page.svelte @@ -3,8 +3,7 @@ import { resolve } from "$app/paths"; import { createAction } from "$lib/action.svelte"; import { validateRepoName } from "$lib/api/repoCreate"; - import { saveRepoRecord } from "$lib/api/repoSettings"; - import { recordKeyOf } from "$lib/api/syntax"; + import { renameRepo } from "$lib/api/repoSettings"; import { getAuth } from "$lib/auth.svelte"; import Button from "$lib/components/ui/Button.svelte"; import CopyButton from "$lib/components/ui/CopyButton.svelte"; @@ -32,9 +31,7 @@ const agent = auth.agent; if (!agent) return; const next = validateRepoName(name.trim()); - await saveRepoRecord(agent, data.publicConfig.bobbinUrl, recordKeyOf(repo.rkey), { - name: next === repo.rkey ? undefined : next - }); + await renameRepo(agent, data.publicConfig.bobbinUrl, repo, next); await goto(resolve(`/${repo.ownerHandle}/${next}/settings` as "/"), { invalidateAll: true }); -- knotmirror 0.0.1