Skip to content

Commit 21dbd32

Browse files
tlongwell-blocknpub1hwqy0rnujtl25dzmlhn8qwux4kr8sjhas3ugltx9j5dm5dwkp2dsqjhytw
andauthored
fix(desktop): derive default clone URL for relay-hosted repos (#2166)
Signed-off-by: Tyler <[email protected]> Co-authored-by: npub1hwqy0rnujtl25dzmlhn8qwux4kr8sjhas3ugltx9j5dm5dwkp2dsqjhytw <bb80478e7c92feaa345bfde6703b86ad86784afd84788facc5951bba35d60a9b@pending-seed.communities.buzz.xyz>
1 parent 8024ce7 commit 21dbd32

4 files changed

Lines changed: 141 additions & 5 deletions

File tree

‎desktop/src/features/projects/hooks.ts‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
22
import * as React from "react";
33

44
import { relayClient } from "@/shared/api/relayClient";
5+
import { getCachedRelayOrigin } from "@/shared/lib/mediaUrl";
56
import { signRelayEvent } from "@/shared/api/tauri";
67
import { getIdentity } from "@/shared/api/tauriIdentity";
78
import {
@@ -37,6 +38,7 @@ import type {
3738
RelayEvent,
3839
} from "@/shared/api/types";
3940
import { summarizeProjectActivityEvents } from "./projectActivity.mjs";
41+
import { effectiveCloneUrls } from "./lib/projectCloneUrl";
4042
import type { ProjectIssue } from "./projectIssues.mjs";
4143
import { projectIssueEventsToIssues } from "./projectIssues.mjs";
4244
import type {
@@ -172,11 +174,27 @@ function isDeletedByA(project: Project, deletionEvents: RelayEvent[]): boolean {
172174
);
173175
}
174176

175-
export function eventToProject(event: RelayEvent): Project {
177+
/**
178+
* Converts a kind:30617 repo announcement into a `Project`.
179+
*
180+
* `relayOrigin` is the resolved relay HTTP origin (from `getCachedRelayOrigin`)
181+
* used to synthesize a canonical clone URL when the announcement omits an
182+
* explicit `clone` tag. Callers outside the relay-connected app (e.g. unit
183+
* tests) may omit it, in which case no default is derived.
184+
*/
185+
export function eventToProject(
186+
event: RelayEvent,
187+
relayOrigin?: string | null,
188+
): Project {
176189
const d = getTag(event, "d") ?? event.id;
177190
const name = getTag(event, "name") || d;
178191
const description = getTag(event, "description") || event.content || "";
179-
const cloneUrls = getCloneUrls(event);
192+
const cloneUrls = effectiveCloneUrls(
193+
getCloneUrls(event),
194+
relayOrigin,
195+
event.pubkey,
196+
d,
197+
);
180198
const webUrl = getTag(event, "web") ?? null;
181199
const setupUsers = getAllTags(event, "auth");
182200
const contributors = [...new Set([...getAllTags(event, "p"), ...setupUsers])];
@@ -235,7 +253,7 @@ export async function fetchProjects(): Promise<Project[]> {
235253
]);
236254

237255
return dedup(events)
238-
.map(eventToProject)
256+
.map((event) => eventToProject(event, getCachedRelayOrigin()))
239257
.filter(
240258
(project) =>
241259
!isHiddenLocally(project) && !isDeletedByA(project, deletionEvents),
@@ -273,7 +291,10 @@ async function fetchProject(projectId: string): Promise<Project | null> {
273291
const deduped = dedup(events).filter(
274292
(event) => !owner || event.pubkey.toLowerCase() === owner,
275293
);
276-
const project = deduped.length > 0 ? eventToProject(deduped[0]) : null;
294+
const project =
295+
deduped.length > 0
296+
? eventToProject(deduped[0], getCachedRelayOrigin())
297+
: null;
277298
if (!project) {
278299
return null;
279300
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import assert from "node:assert/strict";
2+
import { test } from "node:test";
3+
4+
import { deriveRelayCloneUrl, effectiveCloneUrls } from "./projectCloneUrl.ts";
5+
6+
const OWNER = "a".repeat(64);
7+
const ORIGIN = "https://relay.example";
8+
9+
test("deriveRelayCloneUrl builds the canonical relay-hosted path", () => {
10+
assert.equal(
11+
deriveRelayCloneUrl(ORIGIN, OWNER, "flappy-bee"),
12+
`${ORIGIN}/git/${OWNER}/flappy-bee`,
13+
);
14+
});
15+
16+
test("deriveRelayCloneUrl lowercases the owner pubkey", () => {
17+
const upper = "A".repeat(64);
18+
assert.equal(
19+
deriveRelayCloneUrl(ORIGIN, upper, "repo"),
20+
`${ORIGIN}/git/${OWNER}/repo`,
21+
);
22+
});
23+
24+
test("deriveRelayCloneUrl tolerates a trailing slash on the origin", () => {
25+
assert.equal(
26+
deriveRelayCloneUrl(`${ORIGIN}/`, OWNER, "repo"),
27+
`${ORIGIN}/git/${OWNER}/repo`,
28+
);
29+
});
30+
31+
test("deriveRelayCloneUrl fails closed on an unresolved origin", () => {
32+
assert.equal(deriveRelayCloneUrl(null, OWNER, "repo"), null);
33+
assert.equal(deriveRelayCloneUrl(undefined, OWNER, "repo"), null);
34+
assert.equal(deriveRelayCloneUrl("", OWNER, "repo"), null);
35+
});
36+
37+
test("deriveRelayCloneUrl declines a non-hex or wrong-length owner", () => {
38+
assert.equal(deriveRelayCloneUrl(ORIGIN, "short", "repo"), null);
39+
assert.equal(deriveRelayCloneUrl(ORIGIN, "z".repeat(64), "repo"), null);
40+
});
41+
42+
test("deriveRelayCloneUrl declines a missing repo id", () => {
43+
assert.equal(deriveRelayCloneUrl(ORIGIN, OWNER, ""), null);
44+
});
45+
46+
test("effectiveCloneUrls honors explicit clone URLs over the derived default", () => {
47+
const explicit = ["https://github.com/octocat/hello"];
48+
assert.deepEqual(
49+
effectiveCloneUrls(explicit, ORIGIN, OWNER, "repo"),
50+
explicit,
51+
);
52+
});
53+
54+
test("effectiveCloneUrls derives a default when none is advertised", () => {
55+
assert.deepEqual(effectiveCloneUrls([], ORIGIN, OWNER, "flappy-bee"), [
56+
`${ORIGIN}/git/${OWNER}/flappy-bee`,
57+
]);
58+
});
59+
60+
test("effectiveCloneUrls returns empty when no default can be derived", () => {
61+
assert.deepEqual(effectiveCloneUrls([], null, OWNER, "repo"), []);
62+
});
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* Deriving a default clone URL for a NIP-34 repo announcement that omits an
3+
* explicit `clone` tag.
4+
*
5+
* Buzz relays serve their own git repositories at a canonical path —
6+
* `<relay-origin>/git/<owner-pubkey>/<repo-id>` — which is exactly the shape the
7+
* Rust `validate_clone_url` gate enforces. When an announcement carries no
8+
* `clone` tag (e.g. it was created via `buzz repos create` without `--clone`),
9+
* the desktop would otherwise have no URL to fetch from, so the project detail
10+
* view comes up empty. Synthesizing the canonical relay-hosted URL lets those
11+
* repositories load while still deferring to any explicit clone URLs.
12+
*/
13+
14+
/**
15+
* Builds the canonical relay-hosted clone URL for a repository, or `null` when
16+
* the inputs cannot produce a valid URL (unresolved relay origin, missing owner
17+
* pubkey, or missing repo id). Fails closed rather than emitting a broken URL.
18+
*
19+
* `relayOrigin` is expected to be a bare origin (scheme + host, e.g.
20+
* `https://relay.example`); a trailing slash is tolerated.
21+
*/
22+
export function deriveRelayCloneUrl(
23+
relayOrigin: string | null | undefined,
24+
owner: string,
25+
dtag: string,
26+
): string | null {
27+
if (!relayOrigin || !owner || !dtag) return null;
28+
// The Rust validator requires a 64-char hex owner pubkey; anything else is
29+
// not a relay-hosted repo we can address, so decline rather than guess.
30+
if (!/^[0-9a-fA-F]{64}$/.test(owner)) return null;
31+
const origin = relayOrigin.replace(/\/+$/, "");
32+
return `${origin}/git/${owner.toLowerCase()}/${dtag}`;
33+
}
34+
35+
/**
36+
* Returns the effective clone URLs for a project: the explicitly advertised
37+
* ones when present, otherwise a single-element list holding the derived
38+
* relay-hosted default (or an empty list when no default can be derived).
39+
*
40+
* Explicit `clone` tags always win — NIP-34 permits pointing `clone` at an
41+
* external host (e.g. GitHub), which must not be overridden.
42+
*/
43+
export function effectiveCloneUrls(
44+
cloneUrls: string[],
45+
relayOrigin: string | null | undefined,
46+
owner: string,
47+
dtag: string,
48+
): string[] {
49+
if (cloneUrls.length > 0) return cloneUrls;
50+
const derived = deriveRelayCloneUrl(relayOrigin, owner, dtag);
51+
return derived ? [derived] : [];
52+
}

‎desktop/src/features/projects/useCreateProject.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
projectsQueryKey,
88
} from "@/features/projects/hooks";
99
import { relayClient } from "@/shared/api/relayClient";
10+
import { getCachedRelayOrigin } from "@/shared/lib/mediaUrl";
1011
import { signRelayEvent } from "@/shared/api/tauri";
1112
import { getIdentity } from "@/shared/api/tauriIdentity";
1213
import { KIND_REPO_ANNOUNCEMENT } from "@/shared/constants/kinds";
@@ -77,7 +78,7 @@ async function createProject(input: CreateProjectInput): Promise<Project> {
7778
"Failed to create project.",
7879
);
7980

80-
return eventToProject(event);
81+
return eventToProject(event, getCachedRelayOrigin());
8182
}
8283

8384
/** Mutation that creates a project and inserts it into the projects cache. */

0 commit comments

Comments
 (0)