@recoder.xyz/rithub 0.3.1 → 0.3.3
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/README.md +37 -20
- package/package.json +1 -1
- package/src/auth.mjs +9 -1
- package/src/branch.mjs +12 -1
- package/src/cli-work.mjs +4 -2
- package/src/cli.mjs +66 -8
- package/src/deviceLogin.mjs +7 -2
- package/src/push-approval.mjs +123 -0
- package/src/repo.mjs +43 -5
package/README.md
CHANGED
|
@@ -16,7 +16,7 @@ directly. The package stays scoped, but it installs **two** bin names:
|
|
|
16
16
|
`rit` and `rithub` — same binary, either one works.)
|
|
17
17
|
|
|
18
18
|
```
|
|
19
|
-
rit login #
|
|
19
|
+
rit login # opens a browser for a real device-code sign-in
|
|
20
20
|
rit whoami
|
|
21
21
|
rit clone homeplan # bare name = your own account
|
|
22
22
|
rit clone caelum0x/homeplan # or owner/repo, for any repo you can see
|
|
@@ -25,17 +25,31 @@ rit push # creates the repo on Rithub if missing, then pus
|
|
|
25
25
|
rit logout
|
|
26
26
|
```
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
`https://rithub.dev/
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
28
|
+
`rit login` with no `--token` runs a real RFC-8628-shaped device-code sign-in
|
|
29
|
+
(`https://rithub.dev/device`): it prints a short code, opens the browser
|
|
30
|
+
(best-effort -- still prints the URL either way), and polls until you
|
|
31
|
+
confirm there. The token is validated against the real account
|
|
32
|
+
(`GET /api/v1/user`) before anything is saved to
|
|
33
|
+
`~/.config/rithub/config.json` (written `0600`). `--paste-token` falls
|
|
34
|
+
back to the older "copy a token from Settings -> Applications" flow for a
|
|
35
|
+
box with no outbound access to the sign-in endpoints.
|
|
36
|
+
|
|
37
|
+
**Agent identity, enforced at sign-in, not opt-in (2026-12-01):** every
|
|
38
|
+
device sign-in defaults to an **agent** credential -- a force-push, a
|
|
39
|
+
branch delete, or a push to a protected branch made with it is held for a
|
|
40
|
+
human to approve at `https://rithub.dev/approvals`, even with plain `git`,
|
|
41
|
+
even if that token never runs through `rit` at all. A human signing in
|
|
42
|
+
for themselves unticks "this is me" on the `/device` page to get the
|
|
43
|
+
old, unmarked, behaves-exactly-like-before personal token. `rit login
|
|
44
|
+
--agent` removes that choice entirely for a scripted/CI caller where no
|
|
45
|
+
human will be looking at the page. A one-click **"New agent token"**
|
|
46
|
+
button at `https://rithub.dev/user/settings/applications` ("Agent tokens"
|
|
47
|
+
panel) mints one without any device flow at all, and the same panel lists
|
|
48
|
+
every agent-identity token for one-click revoke (which stops that
|
|
49
|
+
credential from pushing anything through Rithub's git-smart-HTTP gateway
|
|
50
|
+
from then on).
|
|
51
|
+
|
|
52
|
+
That device-flow prompt only happens at a real TTY. Run `rit login` with no
|
|
39
53
|
`--token`/`RITHUB_TOKEN` from a non-interactive session -- an agent, CI,
|
|
40
54
|
anything with stdin open but nobody there to answer it -- and it fails
|
|
41
55
|
immediately with a clear message instead of hanging forever (confirmed
|
|
@@ -125,7 +139,7 @@ The original surface (unchanged):
|
|
|
125
139
|
```
|
|
126
140
|
rit init [--repo <name>] [--api-url <url>] [--json]
|
|
127
141
|
rit claim --email <you@example.com> [--json]
|
|
128
|
-
rit login [--token <token>] [--api-url <url>] [--json]
|
|
142
|
+
rit login [--token <token>] [--agent] [--paste-token] [--api-url <url>] [--json]
|
|
129
143
|
rit logout [--json]
|
|
130
144
|
rit whoami [--json]
|
|
131
145
|
rit clone <owner/repo|repo> [dir] [--json]
|
|
@@ -196,13 +210,16 @@ pretending to search and silently returning nothing.
|
|
|
196
210
|
|
|
197
211
|
## Server change this would need
|
|
198
212
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
`
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
213
|
+
Done, 2026-12-01: a real RFC-8628-shaped device-authorization flow now
|
|
214
|
+
exists server-side (`apps/rithub-device-auth`, `POST /api/rit/device/code`
|
|
215
|
+
+ `GET /device` + `POST /api/rit/device/token`), wrapping the engine's own
|
|
216
|
+
OAuth2 "Rit CLI" application via PKCE -- `rit login`'s browser + poll flow
|
|
217
|
+
above, not the old paste-a-token fallback (`--paste-token` keeps that path
|
|
218
|
+
for a box with no outbound access to the device-flow endpoints). Every
|
|
219
|
+
token it hands out is marked agent-identity server-side at the moment
|
|
220
|
+
it's minted, unless the human opts out on the `/device` page -- see
|
|
221
|
+
`apps/rithub-approvals/src/agentSessionTokens.mjs` for the full
|
|
222
|
+
"enforced, not opt-in" design.
|
|
206
223
|
|
|
207
224
|
## Design notes
|
|
208
225
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@recoder.xyz/rithub",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.3",
|
|
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
|
@@ -39,6 +39,14 @@ async function fetchUser(io, apiUrl, token) {
|
|
|
39
39
|
* of hanging on a prompt nobody is there to answer -- confirmed live,
|
|
40
40
|
* 2026-11-30, that the old code could hang indefinitely on stdin that is
|
|
41
41
|
* open but never sends data or EOF. */
|
|
42
|
+
// `options.agent` (`rit login --agent`, 2026-12-01): tells the server this
|
|
43
|
+
// device sign-in is for an agent, before any token exists -- the server
|
|
44
|
+
// marks the resulting token agent-identity at the moment it is minted
|
|
45
|
+
// (apps/rithub-device-auth's handleCallback), not only if/when `rit`
|
|
46
|
+
// later happens to run with RITHUB_AGENT_SESSION_ID set. Every device
|
|
47
|
+
// sign-in defaults to agent anyway (the human opts OUT on the /device
|
|
48
|
+
// page) -- this flag just removes that opt-out for a scripted/CI caller
|
|
49
|
+
// where no human will be looking at the page at all.
|
|
42
50
|
export async function runLogin(options, io) {
|
|
43
51
|
const apiUrl = (options.apiUrl ?? io.env.RITHUB_API_URL ?? DEFAULT_API_URL).replace(/\/+$/, "");
|
|
44
52
|
let token = options.token ?? io.env.RITHUB_TOKEN;
|
|
@@ -59,7 +67,7 @@ export async function runLogin(options, io) {
|
|
|
59
67
|
io.openUrl?.(settingsUrl);
|
|
60
68
|
token = (await io.promptHidden("Token: ")).trim();
|
|
61
69
|
} else {
|
|
62
|
-
const device = await runDeviceLogin(io, apiUrl);
|
|
70
|
+
const device = await runDeviceLogin(io, apiUrl, { agent: Boolean(options.agent) });
|
|
63
71
|
token = device.token;
|
|
64
72
|
}
|
|
65
73
|
}
|
package/src/branch.mjs
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
import { CliError } from "./init.mjs";
|
|
4
4
|
import { resolveRepoArg, requireSession } from "./repo-arg.mjs";
|
|
5
5
|
import { apiCall, buildQuery } from "./http.mjs";
|
|
6
|
+
import { stageHeldDelete } from "./push-approval.mjs";
|
|
6
7
|
|
|
7
8
|
function repoPath(r, suffix) {
|
|
8
9
|
return `/api/v1/repos/${encodeURIComponent(r.owner)}/${encodeURIComponent(r.repo)}${suffix}`;
|
|
@@ -15,12 +16,22 @@ export async function runBranchList(options, io) {
|
|
|
15
16
|
return { owner: r.owner, repo: r.repo, branches: (body ?? []).map((b) => ({ name: b.name, protected: !!b.protected })) };
|
|
16
17
|
}
|
|
17
18
|
|
|
19
|
+
/** `rit branch delete`: a human delete behaves exactly as before. An
|
|
20
|
+
* agent-session delete (`RITHUB_AGENT_SESSION_ID` set) is held for a
|
|
21
|
+
* human to approve instead (RIT-ACT) -- see push-approval.mjs. */
|
|
18
22
|
export async function runBranchDelete(options, io) {
|
|
19
23
|
const r = resolveRepoArg(io, options);
|
|
20
24
|
requireSession(r);
|
|
25
|
+
|
|
26
|
+
const sessionId = io.env.RITHUB_AGENT_SESSION_ID || null;
|
|
27
|
+
if (sessionId) {
|
|
28
|
+
const approval = await stageHeldDelete(io, { session: r.session, owner: r.owner, repo: r.repo, branch: options.name, reason: options.reason, sessionId });
|
|
29
|
+
return { owner: r.owner, repo: r.repo, branch: options.name, deleted: false, held: true, approval };
|
|
30
|
+
}
|
|
31
|
+
|
|
21
32
|
const { status, body } = await apiCall(io, r.apiUrl, repoPath(r, `/branches/${encodeURIComponent(options.name)}`), { method: "DELETE" }, r.session.token);
|
|
22
33
|
if (status !== 204 && status !== 200) throw new CliError(`could not delete branch ${options.name} (HTTP ${status}): ${body?.message ?? "unknown error"}`, { code: "delete_failed" });
|
|
23
|
-
return { owner: r.owner, repo: r.repo, branch: options.name, deleted: true };
|
|
34
|
+
return { owner: r.owner, repo: r.repo, branch: options.name, deleted: true, held: false };
|
|
24
35
|
}
|
|
25
36
|
|
|
26
37
|
/** `rit branch protect <name|pattern> [--required-approvals <n>] [--no-force-push] [--no-delete-protected]`:
|
package/src/cli-work.mjs
CHANGED
|
@@ -192,10 +192,12 @@ async function runBranchCommand(rest, io) {
|
|
|
192
192
|
return printResult(io, a, await runBranchList({ ...a, cwd: io.cwd }, io), (r) => r.branches.map((b) => `${b.name}${b.protected ? " (protected)" : ""}`).join("\n"));
|
|
193
193
|
}
|
|
194
194
|
if (sub === "delete") {
|
|
195
|
-
const a = parseFlags(args, withRepoFlags());
|
|
195
|
+
const a = parseFlags(args, withRepoFlags({ valueFlags: ["--reason"] }));
|
|
196
196
|
const name = a._[0];
|
|
197
197
|
if (!name) throw new CliError("`rit branch delete` needs a name: `rit branch delete <name>`", { code: "invalid_argument" });
|
|
198
|
-
return printResult(io, a, await runBranchDelete({ ...a, name, cwd: io.cwd }, io), (r) =>
|
|
198
|
+
return printResult(io, a, await runBranchDelete({ ...a, name, cwd: io.cwd }, io), (r) =>
|
|
199
|
+
r.held ? `Held for human approval: ${r.approval.html_url} (approve with \`rit push approve ${r.approval.id}\`, deny with \`rit push deny ${r.approval.id}\`).` : `Deleted branch ${r.branch}.`
|
|
200
|
+
);
|
|
199
201
|
}
|
|
200
202
|
if (sub === "protect") {
|
|
201
203
|
const a = parseFlags(args, withRepoFlags({ valueFlags: ["--required-approvals"], boolFlags: ["--allow-push", "--allow-force-push"] }));
|
package/src/cli.mjs
CHANGED
|
@@ -2,6 +2,7 @@ import { runInit, runClaim, CliError, DEFAULT_API_URL } from "./init.mjs";
|
|
|
2
2
|
import { runLogin, runLogout, runWhoami } from "./auth.mjs";
|
|
3
3
|
import { runClone, runRepoCreate, runPush } from "./repo.mjs";
|
|
4
4
|
import { runWorkCommand, runRepoWorkCommand } from "./cli-work.mjs";
|
|
5
|
+
import { runPushApprove, runPushDeny, runPushStatus, runPushList, markAgentSessionIfNeeded } from "./push-approval.mjs";
|
|
5
6
|
|
|
6
7
|
const USAGE = `rit <command> [options] (alias: rithub)
|
|
7
8
|
|
|
@@ -10,8 +11,13 @@ const USAGE = `rit <command> [options] (alias: rithub)
|
|
|
10
11
|
this directory's git remote at it
|
|
11
12
|
claim --email <you@example.com> hands a human ownership of the account
|
|
12
13
|
\`init\` created in this directory
|
|
13
|
-
login [--token <token>]
|
|
14
|
+
login [--token <token>] [--agent] sign in: opens a browser for a real
|
|
14
15
|
device-code sign-in at ${DEFAULT_API_URL}/device
|
|
16
|
+
(defaults to an agent-identity token --
|
|
17
|
+
a held push for --force/protected/
|
|
18
|
+
delete -- unless the human ticks "this
|
|
19
|
+
is me" on that page; --agent removes
|
|
20
|
+
that choice, for a scripted/CI caller)
|
|
15
21
|
(or pass --token / set RITHUB_TOKEN --
|
|
16
22
|
required on a non-interactive session;
|
|
17
23
|
--paste-token falls back to pasting a
|
|
@@ -20,8 +26,19 @@ const USAGE = `rit <command> [options] (alias: rithub)
|
|
|
20
26
|
whoami print the account the saved/env token belongs to
|
|
21
27
|
clone <owner/repo|repo> [dir] clone a repo (bare name = your own account)
|
|
22
28
|
push [--private] [--name <name>] create the repo if missing (needs
|
|
23
|
-
|
|
24
|
-
|
|
29
|
+
[--to <branch>] [--force] "write:user" for that), add a \`rithub\`
|
|
30
|
+
[--reason <text>] remote, push the current branch (or
|
|
31
|
+
\`--to\` another one). With
|
|
32
|
+
RITHUB_AGENT_SESSION_ID set, pushes
|
|
33
|
+
land on agent/<sessionId> automatically
|
|
34
|
+
-- parallel agent sessions never
|
|
35
|
+
collide. A \`--force\` push, or one to a
|
|
36
|
+
protected branch (the repo default, or
|
|
37
|
+
main/master, or RITHUB_PROTECTED_BRANCHES),
|
|
38
|
+
made with a session id set, is held for
|
|
39
|
+
a human to approve instead of landing.
|
|
40
|
+
push approve|deny|status <id> act on a held push/branch-delete
|
|
41
|
+
push list --owner <o> --repo <r> [--status pending|approved|denied]
|
|
25
42
|
|
|
26
43
|
repo <cmd> create|list|view|fork|delete|rename|edit|archive|sync|sync-now|import
|
|
27
44
|
issue <cmd> list|create|view|close|reopen|edit|comment
|
|
@@ -81,6 +98,15 @@ export function parseFlags(argv, { valueFlags = [], boolFlags = [], repeatableFl
|
|
|
81
98
|
|
|
82
99
|
export async function main(argv, io) {
|
|
83
100
|
const [command, ...rest] = argv;
|
|
101
|
+
// RIT-ACT gap 1 (server-side enforcement, 2026-12-01): mark this
|
|
102
|
+
// session's token as agent-attributed before running ANY command, not
|
|
103
|
+
// just `push`/`branch delete` -- so even a session's first action marks
|
|
104
|
+
// the token before a later raw `git push` with the same credential
|
|
105
|
+
// could reach the server unheld. No-op (not even a network call) unless
|
|
106
|
+
// RITHUB_AGENT_SESSION_ID is set. See push-approval.mjs's header comment.
|
|
107
|
+
if (command !== "--version" && command !== "-v" && command !== "help" && command !== "--help" && command !== "-h") {
|
|
108
|
+
await markAgentSessionIfNeeded(io);
|
|
109
|
+
}
|
|
84
110
|
try {
|
|
85
111
|
if (command === "--version" || command === "-v") {
|
|
86
112
|
io.out(io.version ?? "unknown");
|
|
@@ -114,8 +140,8 @@ export async function main(argv, io) {
|
|
|
114
140
|
}
|
|
115
141
|
|
|
116
142
|
if (command === "login") {
|
|
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);
|
|
143
|
+
const args = parseFlags(rest, { valueFlags: ["--token", "--api-url"], boolFlags: ["--json", "--paste-token", "--agent"] });
|
|
144
|
+
const result = await runLogin({ token: args.token, apiUrl: args.apiUrl, pasteToken: args.pasteToken, agent: args.agent }, io);
|
|
119
145
|
io.out(args.json ? JSON.stringify(result) : `Logged in as ${result.username} on ${result.apiUrl}.`);
|
|
120
146
|
return 0;
|
|
121
147
|
}
|
|
@@ -160,14 +186,46 @@ export async function main(argv, io) {
|
|
|
160
186
|
throw new CliError(`unknown \`rit repo\` subcommand "${subcommand}"`, { code: "invalid_argument" });
|
|
161
187
|
}
|
|
162
188
|
|
|
189
|
+
if (command === "push" && ["approve", "deny", "status", "list"].includes(rest[0])) {
|
|
190
|
+
const [subcommand, ...pushRest] = rest;
|
|
191
|
+
if (subcommand === "list") {
|
|
192
|
+
const args = parseFlags(pushRest, { valueFlags: ["--owner", "--repo", "--status", "--api-url"], boolFlags: ["--json"] });
|
|
193
|
+
const approvals = await runPushList({ owner: args.owner, repo: args.repo, status: args.status, apiUrl: args.apiUrl }, io);
|
|
194
|
+
if (args.json) io.out(JSON.stringify(approvals));
|
|
195
|
+
else if (!approvals.length) io.out("No held pushes.");
|
|
196
|
+
else for (const a of approvals) io.out(`${a.id} ${a.status.padEnd(9)} ${a.owner}/${a.repo} ${a.action}${a.forced ? "(force)" : ""} -> ${a.target_ref.replace("refs/heads/", "")} by ${a.requested_by}`);
|
|
197
|
+
return 0;
|
|
198
|
+
}
|
|
199
|
+
const args = parseFlags(pushRest, { valueFlags: ["--api-url"], boolFlags: ["--json"] });
|
|
200
|
+
const [id] = args._;
|
|
201
|
+
if (!id) throw new CliError(`\`rit push ${subcommand}\` needs an id: \`rit push ${subcommand} <id>\``, { code: "invalid_argument" });
|
|
202
|
+
const runner = { approve: runPushApprove, deny: runPushDeny, status: runPushStatus }[subcommand];
|
|
203
|
+
const result = await runner({ id, apiUrl: args.apiUrl }, io);
|
|
204
|
+
if (args.json) {
|
|
205
|
+
io.out(JSON.stringify(result));
|
|
206
|
+
} else if (subcommand === "approve") {
|
|
207
|
+
io.out(`Approved -- landed on ${result.owner}/${result.repo} ${result.target_ref.replace("refs/heads/", "")}.`);
|
|
208
|
+
} else if (subcommand === "deny") {
|
|
209
|
+
io.out(`Denied -- ${result.owner}/${result.repo} ${result.target_ref.replace("refs/heads/", "")} was never touched.`);
|
|
210
|
+
} else {
|
|
211
|
+
io.out(`${result.id} ${result.status} ${result.owner}/${result.repo} ${result.action} -> ${result.target_ref.replace("refs/heads/", "")} requested by ${result.requested_by}${result.decided_by ? ` -- decided by ${result.decided_by}` : ""}`);
|
|
212
|
+
}
|
|
213
|
+
return 0;
|
|
214
|
+
}
|
|
215
|
+
|
|
163
216
|
if (command === "push") {
|
|
164
|
-
const args = parseFlags(rest, { valueFlags: ["--name", "--api-url"], boolFlags: ["--private", "--json"] });
|
|
165
|
-
const result = await runPush({ name: args.name, private: args.private, apiUrl: args.apiUrl, cwd: io.cwd }, io);
|
|
217
|
+
const args = parseFlags(rest, { valueFlags: ["--name", "--to", "--reason", "--api-url"], boolFlags: ["--private", "--force", "--json"] });
|
|
218
|
+
const result = await runPush({ name: args.name, private: args.private, to: args.to, force: args.force, reason: args.reason, apiUrl: args.apiUrl, cwd: io.cwd }, io);
|
|
166
219
|
if (args.json) {
|
|
167
220
|
io.out(JSON.stringify(result));
|
|
168
221
|
} else {
|
|
169
222
|
if (result.created) io.out(`Created ${result.owner}/${result.repo} on Rithub.`);
|
|
170
|
-
|
|
223
|
+
if (result.held) {
|
|
224
|
+
io.out(`Held for human approval: ${result.approval.html_url}`);
|
|
225
|
+
io.out(`Approve with \`rit push approve ${result.approval.id}\`, deny with \`rit push deny ${result.approval.id}\`.`);
|
|
226
|
+
} else {
|
|
227
|
+
io.out(`Pushed ${result.branch} -> ${result.html_url}`);
|
|
228
|
+
}
|
|
171
229
|
}
|
|
172
230
|
return 0;
|
|
173
231
|
}
|
package/src/deviceLogin.mjs
CHANGED
|
@@ -37,8 +37,13 @@ async function postJson(io, apiUrl, path, json) {
|
|
|
37
37
|
* denied). Returns the access token on success; throws CliError otherwise.
|
|
38
38
|
* `io.sleep` is honored if a caller (tests) supplies one, so this never
|
|
39
39
|
* needs a real wall-clock wait under test. */
|
|
40
|
-
export async function runDeviceLogin(io, apiUrl) {
|
|
41
|
-
|
|
40
|
+
export async function runDeviceLogin(io, apiUrl, { agent = false } = {}) {
|
|
41
|
+
// RIT-ACT "enforced, not opt-in" (2026-12-01): `--agent` tells the
|
|
42
|
+
// server up front this device code is for an agent, so the /device
|
|
43
|
+
// page locks to that (no "this is me" checkbox to override it) --
|
|
44
|
+
// the server still defaults every OTHER device code to agent too
|
|
45
|
+
// (safe default), this flag only removes the human's opt-out.
|
|
46
|
+
const { status, body } = await postJson(io, apiUrl, "/api/rit/device/code", agent ? { kind: "agent" } : {});
|
|
42
47
|
if (status !== 200 || !body?.device_code || !body?.user_code) {
|
|
43
48
|
throw new CliError(
|
|
44
49
|
`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).`,
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
// Agent push approval (RIT-ACT): a push made with an agent-session token
|
|
2
|
+
// (`RITHUB_AGENT_SESSION_ID` set) that would be a force-push, a branch
|
|
3
|
+
// delete, or a push to a protected branch is held by the `rithub-approvals`
|
|
4
|
+
// service (apps/rithub-approvals) instead of landing directly. Human
|
|
5
|
+
// pushes (no session id set) are completely unaffected -- see runPush in
|
|
6
|
+
// src/repo.mjs and runBranchDelete in src/branch.mjs, which only call into
|
|
7
|
+
// this module when a session id is present.
|
|
8
|
+
import { CliError } from "./init.mjs";
|
|
9
|
+
import { apiCall } from "./http.mjs";
|
|
10
|
+
import { gitAuthedArgs } from "./repo.mjs";
|
|
11
|
+
import { resolveSession } from "./config.mjs";
|
|
12
|
+
|
|
13
|
+
/** Default protected-branch set an agent-session push is held against when
|
|
14
|
+
* it isn't an explicit `--force`: the repo's own default branch plus the
|
|
15
|
+
* two common defaults, so a brand-new repo is covered even before a human
|
|
16
|
+
* has configured `rit branch protect` on it. Overridable per-invocation
|
|
17
|
+
* (`RITHUB_PROTECTED_BRANCHES=main,release/*`) for repos with a different
|
|
18
|
+
* convention -- comma-separated exact names (no globbing here; use
|
|
19
|
+
* `rit branch protect` for glob-based protection at the engine level,
|
|
20
|
+
* which is a separate, already-shipped feature this one layers on top of). */
|
|
21
|
+
export function protectedBranches(io, defaultBranch) {
|
|
22
|
+
const configured = io.env.RITHUB_PROTECTED_BRANCHES;
|
|
23
|
+
const set = new Set((configured ? configured.split(",") : ["main", "master"]).map((s) => s.trim()).filter(Boolean));
|
|
24
|
+
if (defaultBranch) set.add(defaultBranch);
|
|
25
|
+
return set;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async function approvalsCall(io, apiUrl, path, init, token) {
|
|
29
|
+
const headers = { ...(init?.headers ?? {}), ...(token ? { authorization: `Bearer ${token}` } : {}) };
|
|
30
|
+
const { status, body } = await apiCall(io, apiUrl, `/approvals/api${path}`, { ...init, headers }, undefined);
|
|
31
|
+
return { status, body };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Stages the local HEAD onto `refs/pending/<id>` (an ordinary, always-
|
|
35
|
+
* allowed git push -- never a bespoke transport) and registers + confirms
|
|
36
|
+
* the approval. Returns the created approval record. Throws (and best-
|
|
37
|
+
* effort cancels the half-made record) if the staging push itself fails. */
|
|
38
|
+
export async function stageHeldPush(io, { session, cwd, owner, repo, cloneUrl, branch, forced, reason, sessionId }) {
|
|
39
|
+
const created = await approvalsCall(io, session.apiUrl, "/create", { method: "POST", body: JSON.stringify({ owner, repo, action: "push", branch, forced, reason, session_id: sessionId }) }, session.token);
|
|
40
|
+
if (created.status !== 201) throw new CliError(`could not register the held push (HTTP ${created.status}): ${created.body?.error ?? "unknown error"}`, { code: "approval_failed" });
|
|
41
|
+
const approval = created.body;
|
|
42
|
+
|
|
43
|
+
const pushArgs = gitAuthedArgs(session.token, ["push", cloneUrl, `HEAD:${approval.pending_ref}`]);
|
|
44
|
+
const pushResult = io.run("git", pushArgs, cwd);
|
|
45
|
+
if (!pushResult.ok) {
|
|
46
|
+
await approvalsCall(io, session.apiUrl, `/${approval.id}/cancel`, { method: "POST" }, session.token).catch(() => {});
|
|
47
|
+
throw new CliError(`could not stage the held push: ${pushResult.output.slice(-500)}`, { code: "git_failed" });
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const newrev = io.run("git", ["rev-parse", "HEAD"], cwd);
|
|
51
|
+
const confirmed = await approvalsCall(io, session.apiUrl, `/${approval.id}/confirm`, { method: "POST", body: JSON.stringify({ newrev: newrev.ok ? newrev.output.trim() : undefined }) }, session.token);
|
|
52
|
+
if (confirmed.status !== 200) throw new CliError(`staged but could not confirm the held push (HTTP ${confirmed.status}): ${confirmed.body?.error ?? "unknown error"}`, { code: "approval_failed" });
|
|
53
|
+
return confirmed.body;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Registers a held branch delete -- no staging push needed, the delete
|
|
57
|
+
* intent is the whole action. */
|
|
58
|
+
export async function stageHeldDelete(io, { session, owner, repo, branch, reason, sessionId }) {
|
|
59
|
+
const created = await approvalsCall(io, session.apiUrl, "/create", { method: "POST", body: JSON.stringify({ owner, repo, action: "delete", branch, reason, session_id: sessionId }) }, session.token);
|
|
60
|
+
if (created.status !== 201) throw new CliError(`could not register the held delete (HTTP ${created.status}): ${created.body?.error ?? "unknown error"}`, { code: "approval_failed" });
|
|
61
|
+
return created.body;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export async function runPushApprove(options, io) {
|
|
65
|
+
const session = requireLoggedIn(options, io);
|
|
66
|
+
const { status, body } = await approvalsCall(io, session.apiUrl, `/${options.id}/approve`, { method: "POST" }, session.token);
|
|
67
|
+
if (status !== 200) throw new CliError(`could not approve ${options.id} (HTTP ${status}): ${body?.error ?? "unknown error"}`, { code: "approve_failed" });
|
|
68
|
+
return body;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export async function runPushDeny(options, io) {
|
|
72
|
+
const session = requireLoggedIn(options, io);
|
|
73
|
+
const { status, body } = await approvalsCall(io, session.apiUrl, `/${options.id}/deny`, { method: "POST" }, session.token);
|
|
74
|
+
if (status !== 200) throw new CliError(`could not deny ${options.id} (HTTP ${status}): ${body?.error ?? "unknown error"}`, { code: "deny_failed" });
|
|
75
|
+
return body;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export async function runPushStatus(options, io) {
|
|
79
|
+
const session = requireLoggedIn(options, io);
|
|
80
|
+
const { status, body } = await approvalsCall(io, session.apiUrl, `/${options.id}`, { method: "GET" }, session.token);
|
|
81
|
+
if (status !== 200) throw new CliError(`could not look up ${options.id} (HTTP ${status}): ${body?.error ?? "unknown error"}`, { code: "network_error" });
|
|
82
|
+
return body;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** RIT-ACT gap 1 (server-side enforcement, 2026-12-01): a raw `git push`
|
|
86
|
+
* made outside `rit` entirely (bypassing stageHeldPush above completely)
|
|
87
|
+
* is only ever held server-side if the server already knows this exact
|
|
88
|
+
* bearer token belongs to an agent session -- it has no other way to tell
|
|
89
|
+
* an agent-session token apart from a human's own PAT, since both are
|
|
90
|
+
* ordinary Rithub access tokens. So every `rit` command, when
|
|
91
|
+
* `RITHUB_AGENT_SESSION_ID` is set, marks its own token here (idempotent,
|
|
92
|
+
* cheap, best-effort -- see apps/rithub-approvals/src/agentSessionTokens.mjs
|
|
93
|
+
* and apps/rithub-agent-gateway, which reads the mark this writes). Called
|
|
94
|
+
* from cli.mjs's `main()` before any subcommand runs, so even a session's
|
|
95
|
+
* very first action (e.g. `rit clone`) marks the token before any later
|
|
96
|
+
* raw `git push` with the same credential could land unheld. Never throws
|
|
97
|
+
* -- a network hiccup here must not block the command the human/agent
|
|
98
|
+
* actually asked for. */
|
|
99
|
+
export async function markAgentSessionIfNeeded(io, options = {}) {
|
|
100
|
+
const sessionId = io.env.RITHUB_AGENT_SESSION_ID;
|
|
101
|
+
if (!sessionId) return;
|
|
102
|
+
const session = resolveSession(io, { apiUrl: options.apiUrl });
|
|
103
|
+
if (!session) return;
|
|
104
|
+
try {
|
|
105
|
+
await approvalsCall(io, session.apiUrl, "/agent-session/mark", { method: "POST", body: JSON.stringify({ session_id: sessionId }), signal: AbortSignal.timeout(5000) }, session.token);
|
|
106
|
+
} catch {
|
|
107
|
+
// best-effort -- see header comment
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export async function runPushList(options, io) {
|
|
112
|
+
const session = requireLoggedIn(options, io);
|
|
113
|
+
if (!options.owner || !options.repo) throw new CliError("`rit push list` needs --owner and --repo", { code: "invalid_argument" });
|
|
114
|
+
const { status, body } = await approvalsCall(io, session.apiUrl, `/list?owner=${encodeURIComponent(options.owner)}&repo=${encodeURIComponent(options.repo)}${options.status ? `&status=${encodeURIComponent(options.status)}` : ""}`, { method: "GET" }, session.token);
|
|
115
|
+
if (status !== 200) throw new CliError(`could not list held pushes (HTTP ${status}): ${body?.error ?? "unknown error"}`, { code: "network_error" });
|
|
116
|
+
return body.approvals;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function requireLoggedIn(options, io) {
|
|
120
|
+
const session = resolveSession(io, { apiUrl: options.apiUrl });
|
|
121
|
+
if (!session) throw new CliError("not logged in -- run `rit login` first", { code: "not_logged_in" });
|
|
122
|
+
return session;
|
|
123
|
+
}
|
package/src/repo.mjs
CHANGED
|
@@ -4,6 +4,11 @@
|
|
|
4
4
|
// rather than re-implementing them.
|
|
5
5
|
import { CliError, setGitRemote, currentBranch } from "./init.mjs";
|
|
6
6
|
import { resolveSession } from "./config.mjs";
|
|
7
|
+
// push-approval.mjs also imports gitAuthedArgs from this file -- a
|
|
8
|
+
// circular import, but a safe one: both sides only reference the other's
|
|
9
|
+
// export from inside a function body, never at module-evaluation time, so
|
|
10
|
+
// Node's ESM loader resolves it with no ordering issue.
|
|
11
|
+
import { stageHeldPush, protectedBranches } from "./push-approval.mjs";
|
|
7
12
|
|
|
8
13
|
const NAME_RE = /^[A-Za-z0-9._-]{1,100}$/;
|
|
9
14
|
|
|
@@ -123,7 +128,18 @@ export async function runRepoCreate(options, io) {
|
|
|
123
128
|
* the current directory's name), points a `rithub` remote at it (a
|
|
124
129
|
* separate remote from `origin` on purpose -- a checkout that already has
|
|
125
130
|
* a GitHub `origin` must not lose it), and pushes the current branch with
|
|
126
|
-
* `-u`.
|
|
131
|
+
* `-u`.
|
|
132
|
+
*
|
|
133
|
+
* Agent-session isolation + push approval (RIT-ACT, no change for a human
|
|
134
|
+
* push -- i.e. `RITHUB_AGENT_SESSION_ID` unset -- which still does exactly
|
|
135
|
+
* what it always did): when `RITHUB_AGENT_SESSION_ID` is set, the push
|
|
136
|
+
* target is automatically `agent/<sessionId>` instead of the local branch
|
|
137
|
+
* name, so two agent sessions pushing from the same token can never
|
|
138
|
+
* collide on the same ref -- unless `--to <branch>` explicitly overrides
|
|
139
|
+
* it. A push that is `--force`, or whose resolved target is a protected
|
|
140
|
+
* branch (the repo's default branch, or `main`/`master`, or
|
|
141
|
+
* `RITHUB_PROTECTED_BRANCHES`), is held for a human to approve instead of
|
|
142
|
+
* landing directly -- see push-approval.mjs. */
|
|
127
143
|
export async function runPush(options, io) {
|
|
128
144
|
const cwd = options.cwd ?? io.cwd;
|
|
129
145
|
const session = resolveSession(io, { apiUrl: options.apiUrl });
|
|
@@ -152,12 +168,34 @@ export async function runPush(options, io) {
|
|
|
152
168
|
}
|
|
153
169
|
|
|
154
170
|
setGitRemote(io, cwd, repoInfo.clone_url, "rithub");
|
|
155
|
-
const
|
|
156
|
-
if (!
|
|
171
|
+
const localBranch = currentBranch(io, cwd);
|
|
172
|
+
if (!localBranch) throw new CliError("no commits yet -- commit something first", { code: "no_commits" });
|
|
173
|
+
|
|
174
|
+
const sessionId = io.env.RITHUB_AGENT_SESSION_ID || null;
|
|
175
|
+
const targetBranch = options.to || (sessionId ? `agent/${sessionId}` : localBranch);
|
|
176
|
+
const needsApproval = Boolean(sessionId) && (Boolean(options.force) || protectedBranches(io, repoInfo.default_branch).has(targetBranch));
|
|
177
|
+
|
|
178
|
+
if (needsApproval) {
|
|
179
|
+
const approval = await stageHeldPush(io, {
|
|
180
|
+
session,
|
|
181
|
+
cwd,
|
|
182
|
+
owner: session.username,
|
|
183
|
+
repo: name,
|
|
184
|
+
cloneUrl: repoInfo.clone_url,
|
|
185
|
+
branch: targetBranch,
|
|
186
|
+
forced: !!options.force,
|
|
187
|
+
reason: options.reason,
|
|
188
|
+
sessionId,
|
|
189
|
+
});
|
|
190
|
+
return { owner: session.username, repo: name, branch: targetBranch, created: lookup.status === 404, html_url: repoInfo.html_url, held: true, approval };
|
|
191
|
+
}
|
|
157
192
|
|
|
158
|
-
const pushArgs = gitAuthedArgs(
|
|
193
|
+
const pushArgs = gitAuthedArgs(
|
|
194
|
+
session.token,
|
|
195
|
+
["push", ...(options.force ? ["--force"] : []), "-u", "rithub", targetBranch === localBranch ? localBranch : `${localBranch}:${targetBranch}`]
|
|
196
|
+
);
|
|
159
197
|
const result = io.run("git", pushArgs, cwd);
|
|
160
198
|
if (!result.ok) throw new CliError(`git push failed: ${result.output.slice(-500)}`, { code: "git_failed" });
|
|
161
199
|
|
|
162
|
-
return { owner: session.username, repo: name, branch, created: lookup.status === 404, html_url: repoInfo.html_url };
|
|
200
|
+
return { owner: session.username, repo: name, branch: targetBranch, created: lookup.status === 404, html_url: repoInfo.html_url, held: false };
|
|
163
201
|
}
|