@recoder.xyz/rithub 0.3.0 → 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/bin/rithub.mjs +0 -0
- package/package.json +1 -1
- package/src/auth.mjs +20 -16
- package/src/cli.mjs +7 -5
- package/src/deviceLogin.mjs +80 -0
- package/src/http.mjs +7 -0
- package/src/issue.mjs +6 -1
package/bin/rithub.mjs
CHANGED
|
File without changes
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@recoder.xyz/rithub",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "A gh-style CLI for Rithub: full gh parity -- issue/change(pr)/release/branch/label/milestone/key/secret/webhook/org/user/search/status/browse/api, plus login/clone/repo/push and `rit init`'s no-human agent signup. One command, zero deps.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "Apache-2.0",
|
package/src/auth.mjs
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
// `rit login` / `rit logout` / `rit whoami`. Rithub has
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
//
|
|
1
|
+
// `rit login` / `rit logout` / `rit whoami`. 2026-12-01: Rithub now has a
|
|
2
|
+
// real device-authorization sign-in (apps/rithub-device-auth, see that
|
|
3
|
+
// app's README) -- a TTY with no `--token`/`RITHUB_TOKEN` runs the device
|
|
4
|
+
// flow (deviceLogin.mjs) instead of the old "paste a token" fallback.
|
|
5
|
+
// `--token`/`RITHUB_TOKEN` keep working unchanged (still the only path for
|
|
6
|
+
// a non-interactive caller -- an agent/CI session).
|
|
7
7
|
import { CliError, DEFAULT_API_URL } from "./init.mjs";
|
|
8
8
|
import { loadConfig, saveConfig, clearConfig, resolveSession } from "./config.mjs";
|
|
9
9
|
import { openUrl } from "./open-browser.mjs";
|
|
10
|
+
import { runDeviceLogin } from "./deviceLogin.mjs";
|
|
10
11
|
|
|
11
12
|
async function fetchUser(io, apiUrl, token) {
|
|
12
13
|
const res = await io.fetchImpl(`${apiUrl}/api/v1/user`, {
|
|
@@ -43,21 +44,24 @@ export async function runLogin(options, io) {
|
|
|
43
44
|
let token = options.token ?? io.env.RITHUB_TOKEN;
|
|
44
45
|
if (!token) {
|
|
45
46
|
const settingsUrl = `${apiUrl}/user/settings/applications`;
|
|
46
|
-
// write:user is not a typo for write:repository: Rithub's own
|
|
47
|
-
// POST /api/v1/user/repos (used by `rit repo create` and `rit push`'s
|
|
48
|
-
// auto-create) is gated on write:user, not write:repository -- confirmed
|
|
49
|
-
// live, 2026-11-30, from the server's own 403 scope error. A token
|
|
50
|
-
// missing it still works for `rit clone`/`rit push` to an existing repo.
|
|
51
|
-
const howTo = `Open ${settingsUrl}, generate a token (needs "read:user" and "write:repository" to clone/push; add "write:user" too if you want \`rit repo create\`/\`rit push\` to create repos), then paste it below.`;
|
|
52
47
|
if (!io.isTTY) {
|
|
53
48
|
throw new CliError(
|
|
54
|
-
`not an interactive terminal -- pass a token instead of
|
|
49
|
+
`not an interactive terminal -- pass a token instead of signing in: \`rit login --token <token>\` or \`RITHUB_TOKEN=<token> rit login\`. Get one at ${settingsUrl}.`,
|
|
55
50
|
{ code: "no_tty" }
|
|
56
51
|
);
|
|
57
52
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
53
|
+
if (options.pasteToken) {
|
|
54
|
+
// Explicit escape hatch (`rit login --paste-token`) back to the old
|
|
55
|
+
// paste-a-PAT flow, kept for a box with no outbound access to the
|
|
56
|
+
// device-flow endpoints or a locked-down headless display.
|
|
57
|
+
const howTo = `Open ${settingsUrl}, generate a token (needs "read:user" and "write:repository" to clone/push; add "write:user" too if you want \`rit repo create\`/\`rit push\` to create repos), then paste it below.`;
|
|
58
|
+
io.out(howTo);
|
|
59
|
+
io.openUrl?.(settingsUrl);
|
|
60
|
+
token = (await io.promptHidden("Token: ")).trim();
|
|
61
|
+
} else {
|
|
62
|
+
const device = await runDeviceLogin(io, apiUrl);
|
|
63
|
+
token = device.token;
|
|
64
|
+
}
|
|
61
65
|
}
|
|
62
66
|
token = token.trim();
|
|
63
67
|
if (!token) throw new CliError("no token given", { code: "invalid_argument" });
|
package/src/cli.mjs
CHANGED
|
@@ -10,10 +10,12 @@ const USAGE = `rit <command> [options] (alias: rithub)
|
|
|
10
10
|
this directory's git remote at it
|
|
11
11
|
claim --email <you@example.com> hands a human ownership of the account
|
|
12
12
|
\`init\` created in this directory
|
|
13
|
-
login [--token <token>] sign in: opens a browser
|
|
14
|
-
|
|
13
|
+
login [--token <token>] sign in: opens a browser for a real
|
|
14
|
+
device-code sign-in at ${DEFAULT_API_URL}/device
|
|
15
15
|
(or pass --token / set RITHUB_TOKEN --
|
|
16
|
-
required on a non-interactive session
|
|
16
|
+
required on a non-interactive session;
|
|
17
|
+
--paste-token falls back to pasting a
|
|
18
|
+
token from /user/settings/applications)
|
|
17
19
|
logout forget the saved token
|
|
18
20
|
whoami print the account the saved/env token belongs to
|
|
19
21
|
clone <owner/repo|repo> [dir] clone a repo (bare name = your own account)
|
|
@@ -112,8 +114,8 @@ export async function main(argv, io) {
|
|
|
112
114
|
}
|
|
113
115
|
|
|
114
116
|
if (command === "login") {
|
|
115
|
-
const args = parseFlags(rest, { valueFlags: ["--token", "--api-url"], boolFlags: ["--json"] });
|
|
116
|
-
const result = await runLogin({ token: args.token, apiUrl: args.apiUrl }, io);
|
|
117
|
+
const args = parseFlags(rest, { valueFlags: ["--token", "--api-url"], boolFlags: ["--json", "--paste-token"] });
|
|
118
|
+
const result = await runLogin({ token: args.token, apiUrl: args.apiUrl, pasteToken: args.pasteToken }, io);
|
|
117
119
|
io.out(args.json ? JSON.stringify(result) : `Logged in as ${result.username} on ${result.apiUrl}.`);
|
|
118
120
|
return 0;
|
|
119
121
|
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// `rit login`'s real browser sign-in: RFC 8628-shaped device-authorization
|
|
2
|
+
// flow against the new server-side route this pass adds
|
|
3
|
+
// (apps/rithub-device-auth, proxied at https://rithub.dev/api/rit/device/*
|
|
4
|
+
// and https://rithub.dev/device -- see that app's README for the server
|
|
5
|
+
// side). Internally the server wraps Rithub's own OAuth2 "Rit CLI"
|
|
6
|
+
// application (confirmed registered 2026-11-18, client_id unchanged here),
|
|
7
|
+
// so the token this returns is a real Rithub OAuth2 access token, not a
|
|
8
|
+
// classic PAT -- it works identically against `/api/v1/*` either way
|
|
9
|
+
// (same header shape handled by fetchUser in auth.mjs: `Authorization:
|
|
10
|
+
// token <value>` -- Gitea accepts an OAuth2 access token there too).
|
|
11
|
+
import { CliError } from "./init.mjs";
|
|
12
|
+
|
|
13
|
+
const POLL_FLOOR_MS = 2000; // never hammer the server even if the returned interval is 0/missing
|
|
14
|
+
|
|
15
|
+
function sleep(ms) {
|
|
16
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async function postJson(io, apiUrl, path, json) {
|
|
20
|
+
const res = await io.fetchImpl(`${apiUrl}${path}`, {
|
|
21
|
+
method: "POST",
|
|
22
|
+
headers: { accept: "application/json", "content-type": "application/json", "user-agent": "rithub-cli" },
|
|
23
|
+
body: JSON.stringify(json ?? {}),
|
|
24
|
+
});
|
|
25
|
+
const text = await res.text();
|
|
26
|
+
let body = null;
|
|
27
|
+
try {
|
|
28
|
+
body = text ? JSON.parse(text) : null;
|
|
29
|
+
} catch {
|
|
30
|
+
body = null;
|
|
31
|
+
}
|
|
32
|
+
return { status: res.status, body };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Starts the device flow: prints the code + URL, best-effort opens the
|
|
36
|
+
* browser, then polls until the human approves (or the code expires / is
|
|
37
|
+
* denied). Returns the access token on success; throws CliError otherwise.
|
|
38
|
+
* `io.sleep` is honored if a caller (tests) supplies one, so this never
|
|
39
|
+
* needs a real wall-clock wait under test. */
|
|
40
|
+
export async function runDeviceLogin(io, apiUrl) {
|
|
41
|
+
const { status, body } = await postJson(io, apiUrl, "/api/rit/device/code", {});
|
|
42
|
+
if (status !== 200 || !body?.device_code || !body?.user_code) {
|
|
43
|
+
throw new CliError(
|
|
44
|
+
`could not start device sign-in against ${apiUrl} (HTTP ${status}): ${body?.message ?? "unknown error"}. Pass a token instead: \`rit login --token <token>\` (get one at ${apiUrl}/user/settings/applications).`,
|
|
45
|
+
{ code: "device_flow_unavailable" }
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const verificationUrl = body.verification_uri_complete ?? body.verification_uri;
|
|
50
|
+
io.out(`First, copy your code: ${body.user_code}`);
|
|
51
|
+
io.out(`Then open this URL in your browser: ${body.verification_uri}`);
|
|
52
|
+
io.openUrl?.(verificationUrl); // best-effort; the code + URL above are already printed either way
|
|
53
|
+
|
|
54
|
+
const intervalMs = Math.max(POLL_FLOOR_MS, (body.interval ?? 5) * 1000);
|
|
55
|
+
const deadline = (io.now?.() ?? Date.now()) + (body.expires_in ?? 600) * 1000;
|
|
56
|
+
let currentIntervalMs = intervalMs;
|
|
57
|
+
|
|
58
|
+
while ((io.now?.() ?? Date.now()) < deadline) {
|
|
59
|
+
await (io.sleep ?? sleep)(currentIntervalMs);
|
|
60
|
+
const poll = await postJson(io, apiUrl, "/api/rit/device/token", { device_code: body.device_code });
|
|
61
|
+
const outcome = poll.body?.error ?? (poll.status === 200 && poll.body?.access_token ? null : "unknown_error");
|
|
62
|
+
|
|
63
|
+
if (poll.status === 200 && poll.body?.access_token) {
|
|
64
|
+
return { token: poll.body.access_token, scope: poll.body.scope ?? null };
|
|
65
|
+
}
|
|
66
|
+
if (outcome === "authorization_pending") continue;
|
|
67
|
+
if (outcome === "slow_down") {
|
|
68
|
+
currentIntervalMs += 5000;
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
if (outcome === "expired_token") {
|
|
72
|
+
throw new CliError("the sign-in code expired before it was approved -- run `rit login` again.", { code: "device_flow_expired" });
|
|
73
|
+
}
|
|
74
|
+
if (outcome === "access_denied") {
|
|
75
|
+
throw new CliError("sign-in was denied in the browser.", { code: "device_flow_denied" });
|
|
76
|
+
}
|
|
77
|
+
throw new CliError(`device sign-in failed (HTTP ${poll.status}): ${poll.body?.message ?? outcome ?? "unknown error"}`, { code: "device_flow_error" });
|
|
78
|
+
}
|
|
79
|
+
throw new CliError("the sign-in code expired before it was approved -- run `rit login` again.", { code: "device_flow_expired" });
|
|
80
|
+
}
|
package/src/http.mjs
CHANGED
|
@@ -33,10 +33,17 @@ export async function apiCall(io, apiUrl, path, init, token) {
|
|
|
33
33
|
* parsing attempt -- for endpoints that answer with a diff/patch, not JSON
|
|
34
34
|
* (`rit change diff`). */
|
|
35
35
|
export async function apiCallRaw(io, apiUrl, path, init, token) {
|
|
36
|
+
// Bug found + fixed 2026-12-01: `init.headers` (e.g. runApi's
|
|
37
|
+
// `content-type: application/json` for a PATCH/POST body) was built by
|
|
38
|
+
// the caller but never actually merged in here, so `rit api` with
|
|
39
|
+
// `--method PATCH`/`--input` sent no Content-Type at all -- confirmed
|
|
40
|
+
// live against rithub.dev: the server answered a 422 "Unsupported
|
|
41
|
+
// Content-Type" instead of applying the PATCH.
|
|
36
42
|
const headers = {
|
|
37
43
|
accept: init?.accept ?? "*/*",
|
|
38
44
|
"user-agent": "rithub-cli",
|
|
39
45
|
...(token ? { authorization: `token ${token}` } : {}),
|
|
46
|
+
...(init?.headers ?? {}),
|
|
40
47
|
};
|
|
41
48
|
const res = await io.fetchImpl(`${apiUrl}${path}`, { ...init, headers });
|
|
42
49
|
const text = await res.text();
|
package/src/issue.mjs
CHANGED
|
@@ -43,7 +43,12 @@ async function setIssueState(options, io, state) {
|
|
|
43
43
|
const r = resolveRepoArg(io, options);
|
|
44
44
|
requireSession(r);
|
|
45
45
|
const { status, body } = await apiCall(io, r.apiUrl, issuesPath(r, `/${options.number}`), { method: "PATCH", body: JSON.stringify({ state }) }, r.session.token);
|
|
46
|
-
|
|
46
|
+
// Confirmed live 2026-12-01 against this engine build: a state-only PATCH
|
|
47
|
+
// on /issues/{number} answers 201, not the 200 every other issue PATCH
|
|
48
|
+
// (e.g. runIssueEdit below) returns -- the state change itself lands
|
|
49
|
+
// correctly either way (re-verified by re-fetching the issue), the status
|
|
50
|
+
// code was just the wrong thing to gate on.
|
|
51
|
+
if (status !== 200 && status !== 201) throw new CliError(`could not ${state === "closed" ? "close" : "reopen"} issue #${options.number} (HTTP ${status}): ${body?.message ?? "unknown error"}`, { code: "update_failed" });
|
|
47
52
|
return { owner: r.owner, repo: r.repo, number: options.number, state: body.state };
|
|
48
53
|
}
|
|
49
54
|
|