@recoder.xyz/rithub 0.2.1 → 0.3.1

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/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
+ }
package/src/status.mjs ADDED
@@ -0,0 +1,23 @@
1
+ // `rit status`: issues assigned to you, changes you opened, and changes
2
+ // waiting on your review, across every repo you can see -- the one gh-style
3
+ // command that is deliberately NOT scoped to a single `--repo`.
4
+ import { CliError } from "./init.mjs";
5
+ import { resolveSession } from "./config.mjs";
6
+ import { apiCall, buildQuery } from "./http.mjs";
7
+
8
+ async function search(io, apiUrl, token, params) {
9
+ const { status, body } = await apiCall(io, apiUrl, `/api/v1/repos/issues/search${buildQuery(params)}`, { method: "GET" }, token);
10
+ if (status !== 200) throw new CliError(`status lookup failed (HTTP ${status}): ${body?.message ?? "unknown error"}`, { code: "network_error" });
11
+ return (body ?? []).map((i) => ({ repository: i.repository?.full_name, number: i.number, title: i.title, html_url: i.html_url }));
12
+ }
13
+
14
+ export async function runStatus(options, io) {
15
+ const session = resolveSession(io, { apiUrl: options.apiUrl });
16
+ if (!session) throw new CliError("not logged in -- run `rit login` first", { code: "not_logged_in" });
17
+ const [assignedIssues, createdChanges, reviewRequested] = await Promise.all([
18
+ search(io, session.apiUrl, session.token, { type: "issues", state: "open", assigned: true, limit: 20 }),
19
+ search(io, session.apiUrl, session.token, { type: "pulls", state: "open", created: true, limit: 20 }),
20
+ search(io, session.apiUrl, session.token, { type: "pulls", state: "open", review_requested: true, limit: 20 }),
21
+ ]);
22
+ return { username: session.username, assignedIssues, createdChanges, reviewRequested };
23
+ }
@@ -0,0 +1,37 @@
1
+ // `rit webhook list/create`, against `/api/v1/repos/{owner}/{repo}/hooks`.
2
+ import { CliError } from "./init.mjs";
3
+ import { resolveRepoArg, requireSession } from "./repo-arg.mjs";
4
+ import { apiCall } from "./http.mjs";
5
+
6
+ function hooksPath(r) {
7
+ return `/api/v1/repos/${encodeURIComponent(r.owner)}/${encodeURIComponent(r.repo)}/hooks`;
8
+ }
9
+
10
+ export async function runWebhookList(options, io) {
11
+ const r = resolveRepoArg(io, options);
12
+ requireSession(r);
13
+ const { status, body } = await apiCall(io, r.apiUrl, hooksPath(r), { method: "GET" }, r.session.token);
14
+ if (status !== 200) throw new CliError(`could not list webhooks on ${r.owner}/${r.repo} (HTTP ${status}): ${body?.message ?? "unknown error"}`, { code: "network_error" });
15
+ return { owner: r.owner, repo: r.repo, webhooks: (body ?? []).map((h) => ({ id: h.id, url: h.config?.url, events: h.events, active: h.active })) };
16
+ }
17
+
18
+ /** `rit webhook create --url <url> [--events push,issues,pull_request] [--secret <text>]`. */
19
+ export async function runWebhookCreate(options, io) {
20
+ const r = resolveRepoArg(io, options);
21
+ requireSession(r);
22
+ if (!options.url) throw new CliError("`rit webhook create` needs --url <url>", { code: "invalid_argument" });
23
+ const events = (options.events ?? "push").split(",").map((e) => e.trim()).filter(Boolean);
24
+ const payload = {
25
+ // "rithub" (what its own API docs list for a plain JSON webhook) is
26
+ // rejected live with "Invalid hook type: rithub" -- confirmed
27
+ // 2026-12-01. "gogs" is the one from the same enum that the server
28
+ // actually accepts and sends the same JSON payload shape for.
29
+ type: "gogs",
30
+ active: true,
31
+ events,
32
+ config: { url: options.url, content_type: "json", ...(options.secret ? { secret: options.secret } : {}) },
33
+ };
34
+ const { status, body } = await apiCall(io, r.apiUrl, hooksPath(r), { method: "POST", body: JSON.stringify(payload) }, r.session.token);
35
+ if (status !== 201) throw new CliError(`could not create the webhook (HTTP ${status}): ${body?.message ?? "unknown error"}`, { code: "create_failed" });
36
+ return { owner: r.owner, repo: r.repo, id: body.id, url: options.url, events };
37
+ }