@recoder.xyz/rithub 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/issue.mjs ADDED
@@ -0,0 +1,72 @@
1
+ // `rit issue list/create/view/close/reopen/comment/edit` -- the issue half
2
+ // of the gh-parity command set, all against
3
+ // `/api/v1/repos/{owner}/{repo}/issues`.
4
+ import { CliError } from "./init.mjs";
5
+ import { resolveRepoArg, requireSession } from "./repo-arg.mjs";
6
+ import { apiCall, buildQuery } from "./http.mjs";
7
+
8
+ function issuesPath(r, suffix = "") {
9
+ return `/api/v1/repos/${encodeURIComponent(r.owner)}/${encodeURIComponent(r.repo)}/issues${suffix}`;
10
+ }
11
+
12
+ export async function runIssueList(options, io) {
13
+ const r = resolveRepoArg(io, options);
14
+ const path = issuesPath(r) + buildQuery({ state: options.state ?? "open", q: options.search, page: options.page, limit: options.limit ?? 30 });
15
+ const { status, body } = await apiCall(io, r.apiUrl, path, { method: "GET" }, r.session?.token);
16
+ if (status !== 200) throw new CliError(`could not list issues on ${r.owner}/${r.repo} (HTTP ${status}): ${body?.message ?? "unknown error"}`, { code: "network_error" });
17
+ return { owner: r.owner, repo: r.repo, issues: (body ?? []).map((i) => ({ number: i.number, title: i.title, state: i.state, html_url: i.html_url })) };
18
+ }
19
+
20
+ export async function runIssueView(options, io) {
21
+ const r = resolveRepoArg(io, options);
22
+ const { status, body } = await apiCall(io, r.apiUrl, issuesPath(r, `/${options.number}`), { method: "GET" }, r.session?.token);
23
+ if (status !== 200) throw new CliError(`issue #${options.number} not found on ${r.owner}/${r.repo} (HTTP ${status})`, { code: "not_found" });
24
+ return { owner: r.owner, repo: r.repo, number: body.number, title: body.title, state: body.state, body: body.body, html_url: body.html_url };
25
+ }
26
+
27
+ export async function runIssueCreate(options, io) {
28
+ const r = resolveRepoArg(io, options);
29
+ requireSession(r);
30
+ if (!options.title) throw new CliError("`rit issue create` needs --title <text>", { code: "invalid_argument" });
31
+ const { status, body } = await apiCall(
32
+ io,
33
+ r.apiUrl,
34
+ issuesPath(r),
35
+ { method: "POST", body: JSON.stringify({ title: options.title, body: options.body ?? "" }) },
36
+ r.session.token
37
+ );
38
+ if (status !== 201) throw new CliError(`could not create the issue (HTTP ${status}): ${body?.message ?? "unknown error"}`, { code: "create_failed" });
39
+ return { owner: r.owner, repo: r.repo, number: body.number, html_url: body.html_url };
40
+ }
41
+
42
+ async function setIssueState(options, io, state) {
43
+ const r = resolveRepoArg(io, options);
44
+ requireSession(r);
45
+ const { status, body } = await apiCall(io, r.apiUrl, issuesPath(r, `/${options.number}`), { method: "PATCH", body: JSON.stringify({ state }) }, r.session.token);
46
+ if (status !== 200) throw new CliError(`could not ${state === "closed" ? "close" : "reopen"} issue #${options.number} (HTTP ${status}): ${body?.message ?? "unknown error"}`, { code: "update_failed" });
47
+ return { owner: r.owner, repo: r.repo, number: options.number, state: body.state };
48
+ }
49
+
50
+ export const runIssueClose = (options, io) => setIssueState(options, io, "closed");
51
+ export const runIssueReopen = (options, io) => setIssueState(options, io, "open");
52
+
53
+ export async function runIssueEdit(options, io) {
54
+ const r = resolveRepoArg(io, options);
55
+ requireSession(r);
56
+ const patch = {};
57
+ if (options.title !== undefined) patch.title = options.title;
58
+ if (options.body !== undefined) patch.body = options.body;
59
+ if (Object.keys(patch).length === 0) throw new CliError("`rit issue edit` needs --title and/or --body", { code: "invalid_argument" });
60
+ const { status, body } = await apiCall(io, r.apiUrl, issuesPath(r, `/${options.number}`), { method: "PATCH", body: JSON.stringify(patch) }, r.session.token);
61
+ if (status !== 200) throw new CliError(`could not edit issue #${options.number} (HTTP ${status}): ${body?.message ?? "unknown error"}`, { code: "update_failed" });
62
+ return { owner: r.owner, repo: r.repo, number: options.number, title: body.title };
63
+ }
64
+
65
+ export async function runIssueComment(options, io) {
66
+ const r = resolveRepoArg(io, options);
67
+ requireSession(r);
68
+ if (!options.body) throw new CliError("`rit issue comment` needs --body <text>", { code: "invalid_argument" });
69
+ const { status, body } = await apiCall(io, r.apiUrl, issuesPath(r, `/${options.number}/comments`), { method: "POST", body: JSON.stringify({ body: options.body }) }, r.session.token);
70
+ if (status !== 201) throw new CliError(`could not comment on issue #${options.number} (HTTP ${status}): ${body?.message ?? "unknown error"}`, { code: "create_failed" });
71
+ return { owner: r.owner, repo: r.repo, number: options.number, id: body.id, html_url: body.html_url };
72
+ }
package/src/label.mjs ADDED
@@ -0,0 +1,31 @@
1
+ // `rit label list/create`, against `/api/v1/repos/{owner}/{repo}/labels`.
2
+ import { CliError } from "./init.mjs";
3
+ import { resolveRepoArg, requireSession } from "./repo-arg.mjs";
4
+ import { apiCall } from "./http.mjs";
5
+
6
+ function labelsPath(r) {
7
+ return `/api/v1/repos/${encodeURIComponent(r.owner)}/${encodeURIComponent(r.repo)}/labels`;
8
+ }
9
+
10
+ export async function runLabelList(options, io) {
11
+ const r = resolveRepoArg(io, options);
12
+ const { status, body } = await apiCall(io, r.apiUrl, labelsPath(r), { method: "GET" }, r.session?.token);
13
+ if (status !== 200) throw new CliError(`could not list labels on ${r.owner}/${r.repo} (HTTP ${status}): ${body?.message ?? "unknown error"}`, { code: "network_error" });
14
+ return { owner: r.owner, repo: r.repo, labels: (body ?? []).map((l) => ({ name: l.name, color: l.color, description: l.description })) };
15
+ }
16
+
17
+ export async function runLabelCreate(options, io) {
18
+ const r = resolveRepoArg(io, options);
19
+ requireSession(r);
20
+ if (!options.name) throw new CliError("`rit label create` needs a name: `rit label create <name> --color <hex>`", { code: "invalid_argument" });
21
+ if (!options.color) throw new CliError("`rit label create` needs --color <hex> (e.g. ff0000)", { code: "invalid_argument" });
22
+ const { status, body } = await apiCall(
23
+ io,
24
+ r.apiUrl,
25
+ labelsPath(r),
26
+ { method: "POST", body: JSON.stringify({ name: options.name, color: options.color.replace(/^#/, ""), description: options.description ?? "" }) },
27
+ r.session.token
28
+ );
29
+ if (status !== 201) throw new CliError(`could not create label ${options.name} (HTTP ${status}): ${body?.message ?? "unknown error"}`, { code: "create_failed" });
30
+ return { owner: r.owner, repo: r.repo, id: body.id, name: body.name, color: body.color };
31
+ }
@@ -0,0 +1,31 @@
1
+ // `rit milestone list/create`, against
2
+ // `/api/v1/repos/{owner}/{repo}/milestones`.
3
+ import { CliError } from "./init.mjs";
4
+ import { resolveRepoArg, requireSession } from "./repo-arg.mjs";
5
+ import { apiCall } from "./http.mjs";
6
+
7
+ function milestonesPath(r) {
8
+ return `/api/v1/repos/${encodeURIComponent(r.owner)}/${encodeURIComponent(r.repo)}/milestones`;
9
+ }
10
+
11
+ export async function runMilestoneList(options, io) {
12
+ const r = resolveRepoArg(io, options);
13
+ const { status, body } = await apiCall(io, r.apiUrl, milestonesPath(r), { method: "GET" }, r.session?.token);
14
+ if (status !== 200) throw new CliError(`could not list milestones on ${r.owner}/${r.repo} (HTTP ${status}): ${body?.message ?? "unknown error"}`, { code: "network_error" });
15
+ return { owner: r.owner, repo: r.repo, milestones: (body ?? []).map((m) => ({ id: m.id, title: m.title, state: m.state, due_on: m.due_on })) };
16
+ }
17
+
18
+ export async function runMilestoneCreate(options, io) {
19
+ const r = resolveRepoArg(io, options);
20
+ requireSession(r);
21
+ if (!options.title) throw new CliError("`rit milestone create` needs a title: `rit milestone create <title>`", { code: "invalid_argument" });
22
+ const { status, body } = await apiCall(
23
+ io,
24
+ r.apiUrl,
25
+ milestonesPath(r),
26
+ { method: "POST", body: JSON.stringify({ title: options.title, description: options.description ?? "", due_on: options.due }) },
27
+ r.session.token
28
+ );
29
+ if (status !== 201) throw new CliError(`could not create milestone ${options.title} (HTTP ${status}): ${body?.message ?? "unknown error"}`, { code: "create_failed" });
30
+ return { owner: r.owner, repo: r.repo, id: body.id, title: body.title };
31
+ }
@@ -0,0 +1,18 @@
1
+ // Best-effort "open this URL in the default browser" for `rit login`
2
+ // (interactive sign-in) and `rit browse`. Never throws and never blocks:
3
+ // a headless box or a sandboxed agent with no display simply won't have
4
+ // anything happen, which is fine -- the URL is always printed too.
5
+ import { spawn } from "node:child_process";
6
+
7
+ export function openUrl(url) {
8
+ try {
9
+ const [cmd, args] =
10
+ process.platform === "darwin" ? ["open", [url]] : process.platform === "win32" ? ["cmd", ["/c", "start", "", url]] : ["xdg-open", [url]];
11
+ const child = spawn(cmd, args, { stdio: "ignore", detached: true });
12
+ child.on("error", () => {}); // no browser/display available -- caller already printed the URL
13
+ child.unref();
14
+ return true;
15
+ } catch {
16
+ return false;
17
+ }
18
+ }
package/src/org.mjs ADDED
@@ -0,0 +1,34 @@
1
+ // `rit org list/view`, `rit user view`.
2
+ import { CliError } from "./init.mjs";
3
+ import { resolveSession } from "./config.mjs";
4
+ import { apiCall } from "./http.mjs";
5
+
6
+ function baseApiUrl(io, options, session) {
7
+ return session?.apiUrl ?? (options.apiUrl ?? io.env.RITHUB_API_URL ?? "https://rithub.dev").replace(/\/+$/, "");
8
+ }
9
+
10
+ export async function runOrgList(options, io) {
11
+ const session = resolveSession(io, { apiUrl: options.apiUrl });
12
+ if (!session) throw new CliError("not logged in -- run `rit login` first", { code: "not_logged_in" });
13
+ const { status, body } = await apiCall(io, session.apiUrl, "/api/v1/user/orgs", { method: "GET" }, session.token);
14
+ if (status !== 200) throw new CliError(`could not list organizations (HTTP ${status}): ${body?.message ?? "unknown error"}`, { code: "network_error" });
15
+ return { orgs: (body ?? []).map((o) => ({ name: o.username ?? o.name, html_url: o.website || `${session.apiUrl}/${o.username}` })) };
16
+ }
17
+
18
+ export async function runOrgView(options, io) {
19
+ const session = resolveSession(io, { apiUrl: options.apiUrl });
20
+ const apiUrl = baseApiUrl(io, options, session);
21
+ if (!options.name) throw new CliError("`rit org view` needs a name: `rit org view <org>`", { code: "invalid_argument" });
22
+ const { status, body } = await apiCall(io, apiUrl, `/api/v1/orgs/${encodeURIComponent(options.name)}`, { method: "GET" }, session?.token);
23
+ if (status !== 200) throw new CliError(`org ${options.name} not found (HTTP ${status})`, { code: "not_found" });
24
+ return { name: body.username ?? body.name, description: body.description, html_url: `${apiUrl}/${body.username ?? options.name}` };
25
+ }
26
+
27
+ export async function runUserView(options, io) {
28
+ const session = resolveSession(io, { apiUrl: options.apiUrl });
29
+ const apiUrl = baseApiUrl(io, options, session);
30
+ if (!options.name) throw new CliError("`rit user view` needs a username: `rit user view <username>`", { code: "invalid_argument" });
31
+ const { status, body } = await apiCall(io, apiUrl, `/api/v1/users/${encodeURIComponent(options.name)}`, { method: "GET" }, session?.token);
32
+ if (status !== 200) throw new CliError(`user ${options.name} not found (HTTP ${status})`, { code: "not_found" });
33
+ return { login: body.login, full_name: body.full_name, html_url: `${apiUrl}/${body.login}` };
34
+ }
@@ -0,0 +1,70 @@
1
+ // `rit release create/list/view/delete` (+ asset upload on create), against
2
+ // `/api/v1/repos/{owner}/{repo}/releases`. view/delete take the tag name
3
+ // directly (Rithub's own `/releases/tags/{tag}` route) so callers never have to
4
+ // resolve a numeric release id first.
5
+ import { readFileSync } from "node:fs";
6
+ import { basename } from "node:path";
7
+ import { CliError } from "./init.mjs";
8
+ import { resolveRepoArg, requireSession } from "./repo-arg.mjs";
9
+ import { apiCall, buildQuery, uploadAsset } from "./http.mjs";
10
+
11
+ function releasesPath(r, suffix = "") {
12
+ return `/api/v1/repos/${encodeURIComponent(r.owner)}/${encodeURIComponent(r.repo)}/releases${suffix}`;
13
+ }
14
+
15
+ export async function runReleaseList(options, io) {
16
+ const r = resolveRepoArg(io, options);
17
+ const { status, body } = await apiCall(io, r.apiUrl, releasesPath(r) + buildQuery({ page: options.page, limit: options.limit ?? 30 }), { method: "GET" }, r.session?.token);
18
+ if (status !== 200) throw new CliError(`could not list releases on ${r.owner}/${r.repo} (HTTP ${status}): ${body?.message ?? "unknown error"}`, { code: "network_error" });
19
+ return { owner: r.owner, repo: r.repo, releases: (body ?? []).map((rel) => ({ tag: rel.tag_name, name: rel.name, draft: rel.draft, prerelease: rel.prerelease, html_url: rel.html_url })) };
20
+ }
21
+
22
+ export async function runReleaseView(options, io) {
23
+ const r = resolveRepoArg(io, options);
24
+ const { status, body } = await apiCall(io, r.apiUrl, releasesPath(r, `/tags/${encodeURIComponent(options.tag)}`), { method: "GET" }, r.session?.token);
25
+ if (status !== 200) throw new CliError(`release ${options.tag} not found on ${r.owner}/${r.repo} (HTTP ${status})`, { code: "not_found" });
26
+ return { owner: r.owner, repo: r.repo, tag: body.tag_name, name: body.name, body: body.body, html_url: body.html_url, assets: (body.assets ?? []).map((a) => a.name) };
27
+ }
28
+
29
+ export async function runReleaseDelete(options, io) {
30
+ const r = resolveRepoArg(io, options);
31
+ requireSession(r);
32
+ const { status, body } = await apiCall(io, r.apiUrl, releasesPath(r, `/tags/${encodeURIComponent(options.tag)}`), { method: "DELETE" }, r.session.token);
33
+ if (status !== 204 && status !== 200) throw new CliError(`could not delete release ${options.tag} (HTTP ${status}): ${body?.message ?? "unknown error"}`, { code: "delete_failed" });
34
+ return { owner: r.owner, repo: r.repo, tag: options.tag, deleted: true };
35
+ }
36
+
37
+ /** `rit release create <tag> [--title <t>] [--notes <text>] [--draft]
38
+ * [--prerelease] [--asset <path>...]`: one or more `--asset` files are
39
+ * uploaded after the release itself is created, same order `gh release
40
+ * create` does it in. */
41
+ export async function runReleaseCreate(options, io) {
42
+ const r = resolveRepoArg(io, options);
43
+ requireSession(r);
44
+ if (!options.tag) throw new CliError("`rit release create` needs a tag: `rit release create <tag>`", { code: "invalid_argument" });
45
+ const { status, body } = await apiCall(
46
+ io,
47
+ r.apiUrl,
48
+ releasesPath(r),
49
+ { method: "POST", body: JSON.stringify({ tag_name: options.tag, name: options.title ?? options.tag, body: options.notes ?? "", draft: !!options.draft, prerelease: !!options.prerelease }) },
50
+ r.session.token
51
+ );
52
+ if (status !== 201) throw new CliError(`could not create release ${options.tag} (HTTP ${status}): ${body?.message ?? "unknown error"}`, { code: "create_failed" });
53
+
54
+ const assetPaths = options.assets ?? [];
55
+ const uploaded = [];
56
+ for (const assetPath of assetPaths) {
57
+ const fileName = basename(assetPath);
58
+ let fileBuffer;
59
+ try {
60
+ fileBuffer = readFileSync(assetPath);
61
+ } catch (err) {
62
+ throw new CliError(`could not read asset file ${assetPath}: ${err.message}`, { code: "invalid_argument" });
63
+ }
64
+ const uploadResult = await uploadAsset(io, r.apiUrl, releasesPath(r, `/${body.id}/assets`), r.session.token, fileBuffer, fileName);
65
+ if (uploadResult.status !== 201) throw new CliError(`release ${options.tag} was created, but uploading ${fileName} failed (HTTP ${uploadResult.status}): ${uploadResult.body?.message ?? "unknown error"}`, { code: "upload_failed" });
66
+ uploaded.push(fileName);
67
+ }
68
+
69
+ return { owner: r.owner, repo: r.repo, tag: options.tag, html_url: body.html_url, assets: uploaded };
70
+ }
@@ -0,0 +1,81 @@
1
+ // Every gh-parity command that acts on one repo (issue/change/release/
2
+ // branch/label/milestone/secret/webhook) needs an owner+repo pair. `gh`
3
+ // itself infers it from the current directory's git remote unless
4
+ // `--repo owner/name` is given; this is that same lookup for Rithub.
5
+ import { CliError } from "./init.mjs";
6
+ import { resolveSession } from "./config.mjs";
7
+
8
+ const NAME_RE = /^[A-Za-z0-9._-]{1,100}$/;
9
+
10
+ export function parseOwnerRepo(value) {
11
+ if (typeof value !== "string") return null;
12
+ const parts = value.split("/").filter(Boolean);
13
+ if (parts.length !== 2 || !NAME_RE.test(parts[0]) || !NAME_RE.test(parts[1])) return null;
14
+ return { owner: parts[0], repo: parts[1].replace(/\.git$/, "") };
15
+ }
16
+
17
+ /** Pulls `{ owner, repo }` out of a git remote URL, https or ssh, with or
18
+ * without a `.git` suffix -- `https://rithub.dev/owner/repo.git`,
19
+ * `git@rithub.dev:owner/repo.git`, or the same shapes with a token
20
+ * embedded in the https form. Returns `null` for anything else (a remote
21
+ * pointing at a different host entirely, e.g. a GitHub `origin` living
22
+ * alongside a Rithub `rithub` remote in the same checkout). */
23
+ export function parseRemoteUrl(url, apiHost) {
24
+ if (typeof url !== "string") return null;
25
+ const sshMatch = url.match(/^git@([^:]+):(.+)$/);
26
+ const path = sshMatch ? sshMatch[2] : (() => {
27
+ try {
28
+ const u = new URL(url);
29
+ return u.host === apiHost ? u.pathname.replace(/^\/+/, "") : null;
30
+ } catch {
31
+ return null;
32
+ }
33
+ })();
34
+ if (sshMatch && sshMatch[1] !== apiHost) return null;
35
+ if (!path) return null;
36
+ const parsed = parseOwnerRepo(path.replace(/\.git$/, ""));
37
+ return parsed;
38
+ }
39
+
40
+ /** `--repo owner/name` wins if given; otherwise reads `git remote -v` in
41
+ * `cwd` and uses the first remote that points at the configured Rithub
42
+ * host -- the `rithub` remote `rit push` sets up if present, else whatever
43
+ * matches first. Throws a clear, actionable error rather than letting a
44
+ * later API call fail with a confusing 404. */
45
+ export function resolveRepoArg(io, options) {
46
+ const session = resolveSession(io, { apiUrl: options.apiUrl });
47
+ const apiUrl = session?.apiUrl ?? (options.apiUrl ?? io.env.RITHUB_API_URL ?? "https://rithub.dev").replace(/\/+$/, "");
48
+ const apiHost = new URL(apiUrl).host;
49
+
50
+ if (options.repo) {
51
+ const target = parseOwnerRepo(options.repo);
52
+ if (!target) throw new CliError(`--repo "${options.repo}" doesn't look like owner/name`, { code: "invalid_argument" });
53
+ return { ...target, apiUrl, session };
54
+ }
55
+
56
+ const remotes = io.run("git", ["remote", "-v"], options.cwd ?? io.cwd);
57
+ if (remotes.ok) {
58
+ const lines = remotes.output.split("\n").filter((l) => l.includes("(fetch)"));
59
+ const candidates = lines
60
+ .map((l) => {
61
+ const [name, url] = l.split(/\s+/);
62
+ return { name, parsed: parseRemoteUrl(url, apiHost) };
63
+ })
64
+ .filter((c) => c.parsed);
65
+ const best = candidates.find((c) => c.name === "rithub") ?? candidates.find((c) => c.name === "origin") ?? candidates[0];
66
+ if (best) return { ...best.parsed, apiUrl, session };
67
+ }
68
+
69
+ throw new CliError(
70
+ `could not work out owner/repo -- pass --repo <owner/name>, or run this inside a checkout with a git remote pointing at ${apiHost}`,
71
+ { code: "no_repo" }
72
+ );
73
+ }
74
+
75
+ /** Repo-scoped commands all need a token (issues/PRs/releases are visible
76
+ * for public repos without one, but creating/editing anything needs
77
+ * write access) -- shared check so each command doesn't repeat it. */
78
+ export function requireSession(repoArg) {
79
+ if (!repoArg.session) throw new CliError("not logged in -- run `rit login` first", { code: "not_logged_in" });
80
+ return repoArg.session;
81
+ }
@@ -0,0 +1,132 @@
1
+ // `rit repo list/view/fork/delete/rename/edit/sync/import/archive` -- the
2
+ // rest of the gh-parity repo surface, alongside `rit repo create` and
3
+ // `rit clone`/`rit push` in src/repo.mjs. Kept in its own file (small
4
+ // files over one large one) since src/repo.mjs's `clone`/`create`/`push`
5
+ // already carry careful token-handling comments that don't need repeating
6
+ // nine more times.
7
+ import { CliError } from "./init.mjs";
8
+ import { parseTarget } from "./repo.mjs";
9
+ import { resolveSession } from "./config.mjs";
10
+ import { apiCall, buildQuery } from "./http.mjs";
11
+
12
+ function repoPath(owner, repo, suffix = "") {
13
+ return `/api/v1/repos/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}${suffix}`;
14
+ }
15
+
16
+ function session(io, options, { required = false } = {}) {
17
+ const s = resolveSession(io, { apiUrl: options.apiUrl });
18
+ if (required && !s) throw new CliError("not logged in -- run `rit login` first", { code: "not_logged_in" });
19
+ return s;
20
+ }
21
+
22
+ function requireTarget(options, fallbackOwner) {
23
+ const target = parseTarget(options.name, fallbackOwner);
24
+ if (!target) throw new CliError(`"${options.name}" doesn't look like a repo -- use owner/repo${fallbackOwner ? " or a bare name" : ""}`, { code: "invalid_argument" });
25
+ return target;
26
+ }
27
+
28
+ /** `rit repo list [owner] [--json]`: your own repos with no argument, an
29
+ * owner's visible-to-you repos otherwise. */
30
+ export async function runRepoList(options, io) {
31
+ const s = session(io, options);
32
+ const apiUrl = s?.apiUrl ?? (options.apiUrl ?? io.env.RITHUB_API_URL ?? "https://rithub.dev").replace(/\/+$/, "");
33
+ const path = options.owner ? `/api/v1/users/${encodeURIComponent(options.owner)}/repos` : "/api/v1/user/repos";
34
+ if (!options.owner && !s) throw new CliError("not logged in -- run `rit login` first, or pass an owner: `rit repo list <owner>`", { code: "not_logged_in" });
35
+ const { status, body } = await apiCall(io, apiUrl, path + buildQuery({ page: options.page, limit: options.limit ?? 50 }), { method: "GET" }, s?.token);
36
+ if (status !== 200) throw new CliError(`could not list repos (HTTP ${status}): ${body?.message ?? "unknown error"}`, { code: "network_error" });
37
+ return { repos: (body ?? []).map((repo) => ({ full_name: repo.full_name, private: repo.private, fork: repo.fork, html_url: repo.html_url })) };
38
+ }
39
+
40
+ export async function runRepoView(options, io) {
41
+ const s = session(io, options);
42
+ const apiUrl = s?.apiUrl ?? (options.apiUrl ?? io.env.RITHUB_API_URL ?? "https://rithub.dev").replace(/\/+$/, "");
43
+ const target = requireTarget(options, s?.username);
44
+ const { status, body } = await apiCall(io, apiUrl, repoPath(target.owner, target.repo), { method: "GET" }, s?.token);
45
+ if (status !== 200) throw new CliError(`${target.owner}/${target.repo} was not found (HTTP ${status})`, { code: "not_found" });
46
+ return { owner: target.owner, repo: target.repo, private: body.private, description: body.description, default_branch: body.default_branch, stars: body.stars_count, html_url: body.html_url };
47
+ }
48
+
49
+ export async function runRepoFork(options, io) {
50
+ const s = session(io, options, { required: true });
51
+ const target = requireTarget(options, s.username);
52
+ const { status, body } = await apiCall(io, s.apiUrl, repoPath(target.owner, target.repo, "/forks"), { method: "POST", body: JSON.stringify({}) }, s.token);
53
+ if (status !== 202 && status !== 201) throw new CliError(`could not fork ${target.owner}/${target.repo} (HTTP ${status}): ${body?.message ?? "unknown error"}`, { code: "create_failed" });
54
+ return { owner: target.owner, repo: target.repo, forked_as: body.full_name, html_url: body.html_url };
55
+ }
56
+
57
+ /** `rit repo delete <owner/repo> --confirm`: no interactive y/N prompt on
58
+ * purpose -- an agent with no TTY must not get stuck on one, same reasoning
59
+ * as the `rit login` fix. `--confirm` is the deliberate, scriptable
60
+ * stand-in for it. */
61
+ export async function runRepoDelete(options, io) {
62
+ const s = session(io, options, { required: true });
63
+ const target = requireTarget(options, s.username);
64
+ if (!options.confirm) throw new CliError(`refusing to delete ${target.owner}/${target.repo} without --confirm`, { code: "invalid_argument" });
65
+ const { status, body } = await apiCall(io, s.apiUrl, repoPath(target.owner, target.repo), { method: "DELETE" }, s.token);
66
+ if (status !== 204 && status !== 200) throw new CliError(`could not delete ${target.owner}/${target.repo} (HTTP ${status}): ${body?.message ?? "unknown error"}`, { code: "delete_failed" });
67
+ return { owner: target.owner, repo: target.repo, deleted: true };
68
+ }
69
+
70
+ export async function runRepoRename(options, io) {
71
+ const s = session(io, options, { required: true });
72
+ const target = requireTarget(options, s.username);
73
+ if (!options.newName) throw new CliError("`rit repo rename` needs a new name: `rit repo rename <owner/repo> <new-name>`", { code: "invalid_argument" });
74
+ const { status, body } = await apiCall(io, s.apiUrl, repoPath(target.owner, target.repo), { method: "PATCH", body: JSON.stringify({ name: options.newName }) }, s.token);
75
+ if (status !== 200) throw new CliError(`could not rename ${target.owner}/${target.repo} (HTTP ${status}): ${body?.message ?? "unknown error"}`, { code: "update_failed" });
76
+ return { owner: target.owner, repo: body.name, html_url: body.html_url };
77
+ }
78
+
79
+ export async function runRepoEdit(options, io) {
80
+ const s = session(io, options, { required: true });
81
+ const target = requireTarget(options, s.username);
82
+ const patch = {};
83
+ if (options.description !== undefined) patch.description = options.description;
84
+ if (options.private) patch.private = true;
85
+ if (options.public) patch.private = false;
86
+ if (Object.keys(patch).length === 0) throw new CliError("`rit repo edit` needs --description, --private, or --public", { code: "invalid_argument" });
87
+ const { status, body } = await apiCall(io, s.apiUrl, repoPath(target.owner, target.repo), { method: "PATCH", body: JSON.stringify(patch) }, s.token);
88
+ if (status !== 200) throw new CliError(`could not edit ${target.owner}/${target.repo} (HTTP ${status}): ${body?.message ?? "unknown error"}`, { code: "update_failed" });
89
+ return { owner: target.owner, repo: target.repo, private: body.private, description: body.description };
90
+ }
91
+
92
+ export async function runRepoArchive(options, io) {
93
+ const s = session(io, options, { required: true });
94
+ const target = requireTarget(options, s.username);
95
+ const { status, body } = await apiCall(io, s.apiUrl, repoPath(target.owner, target.repo), { method: "PATCH", body: JSON.stringify({ archived: !options.unarchive }) }, s.token);
96
+ if (status !== 200) throw new CliError(`could not ${options.unarchive ? "unarchive" : "archive"} ${target.owner}/${target.repo} (HTTP ${status}): ${body?.message ?? "unknown error"}`, { code: "update_failed" });
97
+ return { owner: target.owner, repo: target.repo, archived: body.archived };
98
+ }
99
+
100
+ /** `rit repo sync <git-url> [--name <name>] [--private]`: creates a live
101
+ * pull mirror of `<git-url>` on Rithub (`POST /repos/migrate` with
102
+ * `mirror: true`) -- the repo stays in sync on its own; re-run
103
+ * `rit repo sync-now <owner/repo>` to pull immediately instead of waiting
104
+ * for the next scheduled sync. `rit repo import` is the same call with
105
+ * `mirror: false` for a one-time copy instead. */
106
+ async function migrate(options, io, { mirror }) {
107
+ const s = session(io, options, { required: true });
108
+ if (!options.url) throw new CliError(`\`rit repo ${mirror ? "sync" : "import"}\` needs a source git URL: \`rit repo ${mirror ? "sync" : "import"} <git-url>\``, { code: "invalid_argument" });
109
+ const repoName = options.name ?? options.url.replace(/\.git$/, "").split("/").pop();
110
+ const { status, body } = await apiCall(
111
+ io,
112
+ s.apiUrl,
113
+ "/api/v1/repos/migrate",
114
+ { method: "POST", body: JSON.stringify({ clone_addr: options.url, repo_name: repoName, mirror, private: !!options.private, wiki: true, issues: true, labels: true, releases: true, milestones: true, pull_requests: true }) },
115
+ s.token
116
+ );
117
+ if (status !== 201) throw new CliError(`could not ${mirror ? "mirror" : "import"} ${options.url} (HTTP ${status}): ${body?.message ?? "unknown error"}`, { code: "create_failed" });
118
+ return { owner: s.username, repo: repoName, mirror, source: options.url, html_url: body.html_url };
119
+ }
120
+ export const runRepoSync = (options, io) => migrate(options, io, { mirror: true });
121
+ export const runRepoImport = (options, io) => migrate(options, io, { mirror: false });
122
+
123
+ /** `rit repo sync-now <owner/repo>`: triggers an immediate pull on an
124
+ * existing mirror (`POST .../mirror-sync`) instead of waiting for the next
125
+ * scheduled interval. */
126
+ export async function runRepoSyncNow(options, io) {
127
+ const s = session(io, options, { required: true });
128
+ const target = requireTarget(options, s.username);
129
+ const { status, body } = await apiCall(io, s.apiUrl, repoPath(target.owner, target.repo, "/mirror-sync"), { method: "POST" }, s.token);
130
+ if (status !== 200) throw new CliError(`could not sync ${target.owner}/${target.repo} now (HTTP ${status}): ${body?.message ?? "unknown error"}`, { code: "update_failed" });
131
+ return { owner: target.owner, repo: target.repo, synced: true };
132
+ }
package/src/repo.mjs CHANGED
@@ -40,7 +40,7 @@ async function apiCall(io, apiUrl, path, init, token) {
40
40
  * config). `credential.helper=` (empty) is set alongside it so a cached OS
41
41
  * credential (osxkeychain etc.) never silently substitutes a different
42
42
  * identity than the one `rit login` chose. */
43
- function gitAuthedArgs(token, args) {
43
+ export function gitAuthedArgs(token, args) {
44
44
  return token ? ["-c", "credential.helper=", "-c", `http.extraHeader=Authorization: token ${token}`, ...args] : args;
45
45
  }
46
46
 
@@ -75,7 +75,19 @@ export async function runClone(options, io) {
75
75
  }
76
76
 
77
77
  const { status, body } = await apiCall(io, apiUrl, `/api/v1/repos/${encodeURIComponent(target.owner)}/${encodeURIComponent(target.repo)}`, { method: "GET" }, session?.token);
78
- if (status === 404) throw new CliError(`${target.owner}/${target.repo} was not found (or this token can't see it)`, { code: "not_found" });
78
+ if (status === 404) {
79
+ // Rithub answers a private repo you can't see with
80
+ // a plain 404, same as a repo that doesn't exist at all -- there's no
81
+ // way to tell those apart from the HTTP response alone. But "not signed
82
+ // in at all" is a distinct, more useful thing to say than that, and we
83
+ // do know that case for certain.
84
+ throw new CliError(
85
+ session
86
+ ? `${target.owner}/${target.repo} was not found (or ${session.username}'s token can't see it)`
87
+ : `not signed in, and ${target.owner}/${target.repo} was not found while signed out -- if it's private, run \`rit login\` first (or set RITHUB_TOKEN), then retry`,
88
+ { code: "not_found" }
89
+ );
90
+ }
79
91
  if (status !== 200) throw new CliError(`could not look up ${target.owner}/${target.repo} (HTTP ${status}): ${body?.message ?? "unknown error"}`, { code: "network_error" });
80
92
 
81
93
  const cloneUrl = body.clone_url ?? `${apiUrl}/${target.owner}/${target.repo}.git`;
package/src/search.mjs ADDED
@@ -0,0 +1,39 @@
1
+ // `rit search repos <query>` / `rit search issues <query>`, against
2
+ // `/api/v1/repos/search` and `/api/v1/repos/issues/search`.
3
+ // `rit search code` has nowhere to call: Rithub has no
4
+ // code-search REST endpoint, confirmed against this server's own
5
+ // swagger.v1.json, 2026-12-01 -- only repo/issue/user/org/team search
6
+ // exist. We say so plainly instead of silently returning nothing.
7
+ import { CliError } from "./init.mjs";
8
+ import { resolveSession } from "./config.mjs";
9
+ import { apiCall, buildQuery } from "./http.mjs";
10
+
11
+ function baseApiUrl(io, options, session) {
12
+ return session?.apiUrl ?? (options.apiUrl ?? io.env.RITHUB_API_URL ?? "https://rithub.dev").replace(/\/+$/, "");
13
+ }
14
+
15
+ export async function runSearchRepos(options, io) {
16
+ const session = resolveSession(io, { apiUrl: options.apiUrl });
17
+ const apiUrl = baseApiUrl(io, options, session);
18
+ if (!options.query) throw new CliError("`rit search repos` needs a query: `rit search repos <query>`", { code: "invalid_argument" });
19
+ const { status, body } = await apiCall(io, apiUrl, `/api/v1/repos/search${buildQuery({ q: options.query, limit: options.limit ?? 20 })}`, { method: "GET" }, session?.token);
20
+ if (status !== 200) throw new CliError(`search failed (HTTP ${status}): ${body?.message ?? "unknown error"}`, { code: "network_error" });
21
+ return { query: options.query, repos: (body?.data ?? []).map((r) => ({ full_name: r.full_name, description: r.description, stars: r.stars_count, html_url: r.html_url })) };
22
+ }
23
+
24
+ export async function runSearchIssues(options, io) {
25
+ const session = resolveSession(io, { apiUrl: options.apiUrl });
26
+ const apiUrl = baseApiUrl(io, options, session);
27
+ if (!options.query) throw new CliError("`rit search issues` needs a query: `rit search issues <query>`", { code: "invalid_argument" });
28
+ const path = `/api/v1/repos/issues/search${buildQuery({ q: options.query, type: options.type ?? "issues", owner: options.owner, limit: options.limit ?? 20 })}`;
29
+ const { status, body } = await apiCall(io, apiUrl, path, { method: "GET" }, session?.token);
30
+ if (status !== 200) throw new CliError(`search failed (HTTP ${status}): ${body?.message ?? "unknown error"}`, { code: "network_error" });
31
+ return { query: options.query, issues: (body ?? []).map((i) => ({ repository: i.repository?.full_name, number: i.number, title: i.title, state: i.state, html_url: i.html_url })) };
32
+ }
33
+
34
+ export function runSearchCode() {
35
+ throw new CliError(
36
+ "code search isn't available via the Rithub API (no endpoint exists on this server, confirmed against its own swagger) -- use `rit search repos`/`rit search issues`, or the web UI's own search if the server exposes one",
37
+ { code: "unsupported" }
38
+ );
39
+ }
package/src/secret.mjs ADDED
@@ -0,0 +1,35 @@
1
+ // `rit secret set/list` -- repo-level Actions secrets,
2
+ // `/api/v1/repos/{owner}/{repo}/actions/secrets`. Values are write-only:
3
+ // the list endpoint (like GitHub's and every other secrets API) only ever
4
+ // returns names, never values, so there is nothing for `rit secret list`
5
+ // to leak.
6
+ import { CliError } from "./init.mjs";
7
+ import { resolveRepoArg, requireSession } from "./repo-arg.mjs";
8
+ import { apiCall } from "./http.mjs";
9
+
10
+ function secretsPath(r, suffix = "") {
11
+ return `/api/v1/repos/${encodeURIComponent(r.owner)}/${encodeURIComponent(r.repo)}/actions/secrets${suffix}`;
12
+ }
13
+
14
+ export async function runSecretList(options, io) {
15
+ const r = resolveRepoArg(io, options);
16
+ requireSession(r);
17
+ const { status, body } = await apiCall(io, r.apiUrl, secretsPath(r), { method: "GET" }, r.session.token);
18
+ if (status !== 200) throw new CliError(`could not list secrets on ${r.owner}/${r.repo} (HTTP ${status}): ${body?.message ?? "unknown error"}`, { code: "network_error" });
19
+ return { owner: r.owner, repo: r.repo, secrets: (body ?? []).map((s) => s.name) };
20
+ }
21
+
22
+ export async function runSecretSet(options, io) {
23
+ const r = resolveRepoArg(io, options);
24
+ requireSession(r);
25
+ if (!options.name || options.value === undefined) throw new CliError("`rit secret set` needs a name and a value: `rit secret set <NAME> <value>`", { code: "invalid_argument" });
26
+ const { status, body } = await apiCall(
27
+ io,
28
+ r.apiUrl,
29
+ secretsPath(r, `/${encodeURIComponent(options.name)}`),
30
+ { method: "PUT", body: JSON.stringify({ data: options.value }) },
31
+ r.session.token
32
+ );
33
+ if (status !== 201 && status !== 204) throw new CliError(`could not set secret ${options.name} (HTTP ${status}): ${body?.message ?? "unknown error"}`, { code: "update_failed" });
34
+ return { owner: r.owner, repo: r.repo, name: options.name, set: true };
35
+ }
@@ -0,0 +1,37 @@
1
+ // `rit key add/list` -- the account's SSH keys, `/api/v1/user/keys`.
2
+ import { readFileSync } from "node:fs";
3
+ import { CliError } from "./init.mjs";
4
+ import { resolveSession } from "./config.mjs";
5
+ import { apiCall } from "./http.mjs";
6
+
7
+ export async function runKeyList(options, io) {
8
+ const session = resolveSession(io, { apiUrl: options.apiUrl });
9
+ if (!session) throw new CliError("not logged in -- run `rit login` first", { code: "not_logged_in" });
10
+ const { status, body } = await apiCall(io, session.apiUrl, "/api/v1/user/keys", { method: "GET" }, session.token);
11
+ if (status !== 200) throw new CliError(`could not list keys (HTTP ${status}): ${body?.message ?? "unknown error"}`, { code: "network_error" });
12
+ return { keys: (body ?? []).map((k) => ({ id: k.id, title: k.title, fingerprint: k.fingerprint, read_only: k.read_only })) };
13
+ }
14
+
15
+ /** `rit key add <title> <path-to-public-key-or-the-key-text>`. */
16
+ export async function runKeyAdd(options, io) {
17
+ const session = resolveSession(io, { apiUrl: options.apiUrl });
18
+ if (!session) throw new CliError("not logged in -- run `rit login` first", { code: "not_logged_in" });
19
+ if (!options.title || !options.key) throw new CliError("`rit key add` needs a title and a key: `rit key add <title> <key-or-path>`", { code: "invalid_argument" });
20
+ let key = options.key;
21
+ if (key.startsWith("/") || key.startsWith("~") || key.startsWith("./")) {
22
+ try {
23
+ key = readFileSync(key, "utf8").trim();
24
+ } catch {
25
+ // not a readable path -- treat the argument as the literal key text instead
26
+ }
27
+ }
28
+ const { status, body } = await apiCall(
29
+ io,
30
+ session.apiUrl,
31
+ "/api/v1/user/keys",
32
+ { method: "POST", body: JSON.stringify({ title: options.title, key, read_only: !!options.readOnly }) },
33
+ session.token
34
+ );
35
+ if (status !== 201) throw new CliError(`could not add key ${options.title} (HTTP ${status}): ${body?.message ?? "unknown error"}`, { code: "create_failed" });
36
+ return { id: body.id, title: body.title, fingerprint: body.fingerprint };
37
+ }