shadok-ai 0.19.50 → 0.19.52
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 +33 -7
- package/context/connect-done.html +1 -1
- package/context/github-skill/SKILL.md +94 -0
- package/context/github-skill/scripts/github.mjs +258 -0
- package/context/github-skill/test/github.test.mjs +149 -0
- package/context/social-skill/scripts/social.mjs +2 -2
- package/dist/account-expiry.d.ts +1 -1
- package/dist/account-expiry.js +7 -1
- package/dist/account-expiry.js.map +1 -1
- package/dist/connect-routes.d.ts +16 -1
- package/dist/connect-routes.js +38 -3
- package/dist/connect-routes.js.map +1 -1
- package/dist/connected-accounts.d.ts +46 -6
- package/dist/connected-accounts.js +21 -6
- package/dist/connected-accounts.js.map +1 -1
- package/dist/github.d.ts +121 -0
- package/dist/github.js +274 -0
- package/dist/github.js.map +1 -0
- package/dist/server.js +183 -7
- package/dist/server.js.map +1 -1
- package/dist/social-drafts.js +2 -0
- package/dist/social-drafts.js.map +1 -1
- package/package.json +1 -1
- package/public/index.html +130 -82
- package/public/secrets-panel.js +26 -0
- package/public/social-card.js +60 -2
package/README.md
CHANGED
|
@@ -178,7 +178,12 @@ Enter to skip; you can add it later from the web UI).
|
|
|
178
178
|
- **Secret vault** — stored under `~/.shadok-ai`, never in your repo, injected
|
|
179
179
|
as env vars into the agents that need them. Add or **update** a secret from the
|
|
180
180
|
panel, tick the **profiles** it should feed right there, and reload the running
|
|
181
|
-
agents on those profiles in one click so a rotated value takes effect now.
|
|
181
|
+
agents on those profiles in one click so a rotated value takes effect now.
|
|
182
|
+
On an existing secret's card, the *Shared with* / *Injected into* chips **are
|
|
183
|
+
the save**: a tick is written straight away, and a refused one goes back to
|
|
184
|
+
what the cockpit holds with the reason on the card — there is no Save button
|
|
185
|
+
to forget. (Only the creation form waits, since a secret must exist before it
|
|
186
|
+
can be bound to anything.) On
|
|
182
187
|
an instance with groups, **Any profile** feeds a secret to every agent that can
|
|
183
188
|
read it, whatever its profile (only a person can pick it, not an agent). An
|
|
184
189
|
agent that **obtains** a credential (a CLI login, a key it just provisioned)
|
|
@@ -732,6 +737,23 @@ Enter to skip; you can add it later from the web UI).
|
|
|
732
737
|
agent-auth mode). Operator setup for Google (Cloud
|
|
733
738
|
project, consent screen, the two broker secrets) is in
|
|
734
739
|
[`broker/README.md`](broker/README.md).
|
|
740
|
+
- **Connected accounts (GitHub)** — **Connect GitHub** (shown when the broker
|
|
741
|
+
offers it) authorizes the shadok GitHub App for your account; then agents can
|
|
742
|
+
`git clone` / `git push` and run `gh` on the repositories you picked. Picking
|
|
743
|
+
is a separate step on github.com — authorizing alone reaches no repository —
|
|
744
|
+
so the row says *No repository chosen yet* with a **Choose repositories**
|
|
745
|
+
link until you have, then lists what agents reach (*3 repositories of acme*)
|
|
746
|
+
with **Manage repositories**; coming back to the tab refreshes it, no
|
|
747
|
+
reconnect needed. **There is no approval card**: your repositories' own
|
|
748
|
+
protection on GitHub (branch protection, required reviews) is the guardrail.
|
|
749
|
+
*Shared with* / *Profiles* decide which agents may use it, as for every
|
|
750
|
+
account. No token is ever put in an agent's environment: every agent's git
|
|
751
|
+
is configured with a credential helper (the `shadok-github` skill) that asks
|
|
752
|
+
the cockpit for the current token at each use, and `github.mjs gh …` hands
|
|
753
|
+
`gh` the token for one call. GitHub's 8-hour tokens are renewed by the
|
|
754
|
+
cockpit, and the ~6-month authorization is renewed ahead of its deadline by
|
|
755
|
+
the hourly watch; you are warned only if that fails. Commits made through
|
|
756
|
+
`github.mjs clone` are authored as your login with GitHub's noreply address.
|
|
735
757
|
- **Self-update** — polls npm and can update and reload itself in place.
|
|
736
758
|
|
|
737
759
|
### Telegram (optional)
|
|
@@ -1578,28 +1600,32 @@ Light/dark **mode** is an orthogonal, **per-browser** choice in the same menu
|
|
|
1578
1600
|
endpoint — the accent is the instance's shared identity, the mode is personal),
|
|
1579
1601
|
`/profiles`, `/secrets`, `/secrets/audit` (GET, **admin only** — the secret
|
|
1580
1602
|
audit log: who created/updated/deleted a secret, which profile it was attached
|
|
1581
|
-
to
|
|
1603
|
+
to — from the profile's editor or from a tick on the secret's *Injected into*
|
|
1604
|
+
chips, both directions of the same link — and which agent **received** it at spawn or in a cron guard, with the person
|
|
1582
1605
|
or door that caused it; names only, never a value; filters `secret`, `action`,
|
|
1583
1606
|
`actor`, `agent`, `since`, `limit`. Also in the vault panel → *Audit log*, and
|
|
1584
1607
|
🕘 on each secret), `/telegram`, `/defaults`, `/first-agent` (GET —
|
|
1585
1608
|
`{pending, reason}`: whether this instance's lead agent is being started
|
|
1586
1609
|
right now), `/version`, `/autoupdate`,
|
|
1587
|
-
`/connect/status` (GET — `{available, providers: {linkedin, google},
|
|
1588
|
-
googleTesting}`: what the broker offers; `available` is LinkedIn's),
|
|
1610
|
+
`/connect/status` (GET — `{available, providers: {linkedin, google, github},
|
|
1611
|
+
googleTesting, githubInstallUrl}`: what the broker offers; `available` is LinkedIn's),
|
|
1589
1612
|
`/connect/<provider>/start` · `/connect/<provider>/complete` (POST, browser
|
|
1590
|
-
person only — the OAuth hand-back, `<provider>` = `linkedin` or `
|
|
1613
|
+
person only — the OAuth hand-back, `<provider>` = `linkedin`, `google` or `github`;
|
|
1591
1614
|
Google's `start` also takes `areas`, a list among `calendar`, `drive`, `gmail`,
|
|
1592
1615
|
`gmail_read`, `drive_all` — only those `/connect/status` → `googleAreas` says the broker accepts;
|
|
1593
1616
|
`/connect/<provider>/done` (GET) is the page the broker returns the browser
|
|
1594
1617
|
to), `/connected-accounts` (GET — the accounts you may use, never a token, plus
|
|
1595
1618
|
`softLayer`; a Google account carries `areas`, `policies` and
|
|
1596
|
-
`refreshExpiresAt`;
|
|
1619
|
+
`refreshExpiresAt`; a GitHub account `github` (login, noreply email,
|
|
1620
|
+
installations), `reachesRepos` and `installUrl`; your own accounts also carry `expiringSoon` / `expired`,
|
|
1597
1621
|
for the banner; `GET /:id/log` — that account's last 50 log entries, newest
|
|
1598
1622
|
first; `PUT /:id` changes `share`, LinkedIn's `policy` or Google's
|
|
1599
1623
|
`policies.mail` / `policies.calendar` / `policies.drive`; `DELETE /:id` disconnects, and revokes
|
|
1600
1624
|
a Google grant at Google — owner or admin, browser only),
|
|
1601
1625
|
`/social/accounts` (GET), `/social/posts` (POST) and `/google/actions` (POST
|
|
1602
|
-
`{account, action, args}`)
|
|
1626
|
+
`{account, action, args}`) and `/github/token` (POST `{account?, owner?}` →
|
|
1627
|
+
the CURRENT access token of a GitHub account the agent may use, for its git
|
|
1628
|
+
credential helper and `gh`) — the **agent** side, authenticated by
|
|
1603
1629
|
`x-shadok-session-key` —, and `/social/drafts/:id/publish` ·
|
|
1604
1630
|
`/social/drafts/:id/discard` (POST, browser person only — a LinkedIn post, an
|
|
1605
1631
|
email or a calendar change alike),
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
<script nonce="__CSP_NONCE__">
|
|
22
22
|
(async () => {
|
|
23
23
|
const msg = document.getElementById("msg");
|
|
24
|
-
const LABELS = { linkedin: "LinkedIn", google: "Google" };
|
|
24
|
+
const LABELS = { linkedin: "LinkedIn", google: "Google", github: "GitHub" };
|
|
25
25
|
const m = /^\/connect\/([a-z]+)\/done$/.exec(location.pathname);
|
|
26
26
|
const provider = m && LABELS[m[1]] ? m[1] : "linkedin";
|
|
27
27
|
const label = LABELS[provider];
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: shadok-github
|
|
3
|
+
description: Clone, pull and push GitHub repositories and run `gh` (pull requests, issues, checks) with the GitHub account the person connected in the cockpit. Use when you need to clone a private repository, push a branch, open or review a pull request, or call the GitHub API on their repositories — and when git or gh says it has no GitHub credential.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# shadok-github
|
|
7
|
+
|
|
8
|
+
Works with a GitHub account the person connected in the cockpit (Secrets →
|
|
9
|
+
Connected accounts → Connect GitHub). It reaches exactly the repositories they
|
|
10
|
+
picked for the shadok GitHub App, nothing else.
|
|
11
|
+
|
|
12
|
+
**There is no approval step.** What protects their repositories is GitHub's
|
|
13
|
+
own protection (branch protection, required reviews). Work the way a careful
|
|
14
|
+
contributor does: push a branch and open a pull request; never force-push a
|
|
15
|
+
shared branch, never push straight to the default branch unless asked.
|
|
16
|
+
|
|
17
|
+
## Pick the account
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
node ~/.claude/skills/shadok-github/scripts/github.mjs accounts
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
One line per account: `<id> <login> repos=<owner(all|n)> email=<noreply> reconnect-by=<date>`.
|
|
24
|
+
`repos=NONE` means the person connected GitHub but has not chosen any
|
|
25
|
+
repository yet: tell them to click **Choose repositories** in Secrets →
|
|
26
|
+
Connected accounts. `EXPIRED`: they must reconnect it. Nothing listed: ask them
|
|
27
|
+
to connect a GitHub account, or to share theirs with this agent's group.
|
|
28
|
+
|
|
29
|
+
## git: clone, pull, push
|
|
30
|
+
|
|
31
|
+
git already asks the cockpit for a credential by itself (this script is its
|
|
32
|
+
credential helper for `https://github.com`), so plain `git pull` / `git push`
|
|
33
|
+
on an https remote just work. Clone with the script, which also sets who your
|
|
34
|
+
commits are authored by:
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
node ~/.claude/skills/shadok-github/scripts/github.mjs clone <owner>/<repo> [dir]
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
That writes into the new repository `user.name` = the login, `user.email` = the
|
|
41
|
+
account's noreply address (`<id>+<login>@users.noreply.github.com` — GitHub
|
|
42
|
+
credits the person without exposing their email), and `shadok.githubAccount`
|
|
43
|
+
= the account id. In a repository cloned some other way, run this once inside
|
|
44
|
+
it to do the same:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
node ~/.claude/skills/shadok-github/scripts/github.mjs identity
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Use **https** remotes (`https://github.com/owner/repo.git`): an `git@github.com:`
|
|
51
|
+
SSH remote does not use this account.
|
|
52
|
+
|
|
53
|
+
## gh: pull requests, issues, checks, the API
|
|
54
|
+
|
|
55
|
+
Run gh through the script — it hands gh the current token for that one call:
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
node ~/.claude/skills/shadok-github/scripts/github.mjs gh pr create --fill
|
|
59
|
+
node ~/.claude/skills/shadok-github/scripts/github.mjs gh pr list -R <owner>/<repo>
|
|
60
|
+
node ~/.claude/skills/shadok-github/scripts/github.mjs gh api repos/<owner>/<repo>/actions/runs
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
A bare `gh` does not use this account.
|
|
64
|
+
|
|
65
|
+
## Several accounts
|
|
66
|
+
|
|
67
|
+
With one account shared with you, it is always that one. With several, the
|
|
68
|
+
account is picked by, in order: `--account <id>` (`gh --account <id> …`,
|
|
69
|
+
`clone … --account <id>`), `SHADOK_GITHUB_ACCOUNT`, `git config
|
|
70
|
+
shadok.githubAccount` (set by `clone` / `identity`), then the account whose
|
|
71
|
+
repositories belong to the repository's owner. If git or gh says "Several
|
|
72
|
+
GitHub accounts are shared with this agent", pick one with
|
|
73
|
+
`git config shadok.githubAccount <id>` in the repository.
|
|
74
|
+
|
|
75
|
+
## A script that needs the token itself
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
node ~/.claude/skills/shadok-github/scripts/github.mjs token [--account <id>]
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
It prints the token on stdout — pipe it straight where it is needed. **Never
|
|
82
|
+
print it, log it, write it to a file, or put it in a command line you show**:
|
|
83
|
+
it is the person's credential. It lasts at most 8 hours; ask again rather
|
|
84
|
+
than keeping it.
|
|
85
|
+
|
|
86
|
+
## When it fails
|
|
87
|
+
|
|
88
|
+
- `No GitHub account is shared with this agent` — nothing connected, or not
|
|
89
|
+
shared with your group/profile: ask the person.
|
|
90
|
+
- `needs a reconnect` — only the person can fix it (Secrets → Connected
|
|
91
|
+
accounts → Reconnect).
|
|
92
|
+
- `Repository not found` / 403 from GitHub on a repo you expected — the
|
|
93
|
+
repository is not among those they picked: ask them to add it with
|
|
94
|
+
**Manage repositories** (no reconnect needed).
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// git + gh on a GitHub account the person connected in the cockpit.
|
|
3
|
+
//
|
|
4
|
+
// No token is ever stored here or in the agent's environment: GitHub user
|
|
5
|
+
// tokens last 8 hours and the cockpit renews them, so every use asks the
|
|
6
|
+
// server for the CURRENT one (`POST /github/token`, authenticated by the
|
|
7
|
+
// agent's session key). git reaches this script as its credential helper —
|
|
8
|
+
// the spawn sets `credential.https://github.com.helper` to
|
|
9
|
+
// `node <this file> credential` — and `gh` is run through `github.mjs gh`,
|
|
10
|
+
// which hands it the token for that one invocation only.
|
|
11
|
+
import { execFileSync, spawn } from "node:child_process";
|
|
12
|
+
import { realpathSync } from "node:fs";
|
|
13
|
+
import { fileURLToPath } from "node:url";
|
|
14
|
+
|
|
15
|
+
const USAGE = `usage: github.mjs <command>
|
|
16
|
+
accounts
|
|
17
|
+
clone <owner/repo | https URL> [dir] [--account <id>]
|
|
18
|
+
identity [--account <id>] (in a repository: commit as the account, use it for this repo)
|
|
19
|
+
gh [--account <id>] <gh arguments…> (e.g. github.mjs gh pr create --fill)
|
|
20
|
+
token [--account <id>] [--owner <owner>] (prints the token for a script — never echo or log it)
|
|
21
|
+
credential get (git's credential helper; git calls it, not you)`;
|
|
22
|
+
|
|
23
|
+
const die = (m) => {
|
|
24
|
+
console.error(m);
|
|
25
|
+
process.exit(1);
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
function connection() {
|
|
29
|
+
const port = process.env.SHADOK_PORT;
|
|
30
|
+
const key = process.env.SHADOK_SESSION_KEY;
|
|
31
|
+
if (!port) return { error: "Not inside a shadok-ai agent (SHADOK_PORT unset)." };
|
|
32
|
+
if (!key) return { error: "SHADOK_SESSION_KEY is unset — ask the human to reload this agent." };
|
|
33
|
+
return { port, key };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** One request to the cockpit → `{ok, status, body}`; never throws. */
|
|
37
|
+
async function call(method, route, payload, timeoutMs = 30_000) {
|
|
38
|
+
const c = connection();
|
|
39
|
+
if (c.error) return { ok: false, status: 0, body: { error: c.error } };
|
|
40
|
+
try {
|
|
41
|
+
const res = await fetch(`http://127.0.0.1:${c.port}${route}`, {
|
|
42
|
+
method,
|
|
43
|
+
headers: { "content-type": "application/json", "x-shadok-session-key": c.key },
|
|
44
|
+
...(payload ? { body: JSON.stringify(payload) } : {}),
|
|
45
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
46
|
+
});
|
|
47
|
+
const body = await res.json().catch(() => ({}));
|
|
48
|
+
return { ok: res.ok, status: res.status, body };
|
|
49
|
+
} catch (e) {
|
|
50
|
+
return { ok: false, status: 0, body: { error: `The cockpit did not answer (${e?.message ?? e}). It may be restarting — say so rather than working around it.` } };
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** What a refusal says, with the accounts it lists when it lists some. */
|
|
55
|
+
function refusal(r) {
|
|
56
|
+
const lines = [r.body?.error ?? `Refused (${r.status}): no reason given`];
|
|
57
|
+
for (const a of Array.isArray(r.body?.accounts) ? r.body.accounts : []) {
|
|
58
|
+
lines.push(` ${a.id} ${a.login}${a.owners?.length ? ` repos of: ${a.owners.join(", ")}` : ""}`);
|
|
59
|
+
}
|
|
60
|
+
return lines.join("\n");
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function gitConfig(key) {
|
|
64
|
+
try {
|
|
65
|
+
return execFileSync("git", ["config", "--get", key], { encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] }).trim() || null;
|
|
66
|
+
} catch {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** The account named on purpose: --account, then SHADOK_GITHUB_ACCOUNT, then
|
|
72
|
+
* `git config shadok.githubAccount` (this repository, global, or `git -c`). */
|
|
73
|
+
function explicitAccount(flag) {
|
|
74
|
+
return flag || (process.env.SHADOK_GITHUB_ACCOUNT ?? "").trim() || gitConfig("shadok.githubAccount") || undefined;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** `owner` out of `owner/repo`, `owner/repo.git`, or a github.com URL. */
|
|
78
|
+
export function ownerOf(hint) {
|
|
79
|
+
if (typeof hint !== "string") return null;
|
|
80
|
+
let s = hint.trim();
|
|
81
|
+
const url = /^(?:https?:\/\/(?:[^@/]+@)?|git@)github\.com[/:](.+)$/i.exec(s);
|
|
82
|
+
if (url) s = url[1];
|
|
83
|
+
const m = /^([A-Za-z0-9][A-Za-z0-9-]{0,38})(?:\/|$)/.exec(s);
|
|
84
|
+
return m ? m[1] : null;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** git's credential protocol: `key=value` lines up to a blank line. */
|
|
88
|
+
export function parseCredentialInput(text) {
|
|
89
|
+
const out = {};
|
|
90
|
+
for (const line of String(text).split(/\r?\n/)) {
|
|
91
|
+
if (!line) break;
|
|
92
|
+
const i = line.indexOf("=");
|
|
93
|
+
if (i > 0) out[line.slice(0, i)] = line.slice(i + 1);
|
|
94
|
+
}
|
|
95
|
+
return out;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** What the helper answers git. A GitHub App user token authenticates as any
|
|
99
|
+
* username; `x-access-token` is the documented one. */
|
|
100
|
+
export function credentialOutput(token, expiresAtMs) {
|
|
101
|
+
const lines = ["username=x-access-token", `password=${token}`];
|
|
102
|
+
if (typeof expiresAtMs === "number" && Number.isFinite(expiresAtMs) && expiresAtMs < Number.MAX_SAFE_INTEGER) {
|
|
103
|
+
lines.push(`password_expiry_utc=${Math.floor(expiresAtMs / 1000)}`);
|
|
104
|
+
}
|
|
105
|
+
return lines.join("\n") + "\n";
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function parseFlags(argv, known) {
|
|
109
|
+
const o = { rest: [] };
|
|
110
|
+
for (let i = 0; i < argv.length; i++) {
|
|
111
|
+
const a = argv[i];
|
|
112
|
+
const eq = a.startsWith("--") ? a.indexOf("=") : -1;
|
|
113
|
+
const name = eq > 0 ? a.slice(2, eq) : a.startsWith("--") ? a.slice(2) : null;
|
|
114
|
+
if (name && known.includes(name)) {
|
|
115
|
+
o[name] = eq > 0 ? a.slice(eq + 1) : argv[++i];
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
o.rest.push(a);
|
|
119
|
+
}
|
|
120
|
+
return o;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
async function token(account, owner, timeoutMs) {
|
|
124
|
+
return call("POST", "/github/token", { ...(account ? { account } : {}), ...(owner ? { owner } : {}) }, timeoutMs);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const day = (ms) => (typeof ms === "number" ? new Date(ms).toISOString().slice(0, 10) : "none");
|
|
128
|
+
|
|
129
|
+
async function accounts() {
|
|
130
|
+
const r = await call("GET", "/social/accounts");
|
|
131
|
+
if (!r.ok) die(refusal(r));
|
|
132
|
+
const mine = (r.body.accounts ?? []).filter((a) => a.provider === "github");
|
|
133
|
+
if (!mine.length) {
|
|
134
|
+
console.log("No GitHub account is shared with this agent. Ask the person to connect one in Secrets → Connected accounts, or to share theirs with this agent's group.");
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
for (const a of mine) {
|
|
138
|
+
const owners = (a.owners ?? []).map((o) => `${o.owner}(${o.selection === "all" ? "all" : o.repos ?? "?"})`);
|
|
139
|
+
console.log(
|
|
140
|
+
[
|
|
141
|
+
a.id,
|
|
142
|
+
a.login,
|
|
143
|
+
`repos=${owners.length ? owners.join(",") : "NONE — the person must choose repositories (Secrets → Connected accounts)"}`,
|
|
144
|
+
`email=${a.email}`,
|
|
145
|
+
a.status === "expired" ? "EXPIRED — the person must reconnect it" : `reconnect-by=${day(a.expiresAt)}`,
|
|
146
|
+
].join(" "),
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** git's helper: answer `get` for https://github.com, stay silent otherwise.
|
|
152
|
+
* Never exits non-zero and never prompts: git falls through to its next
|
|
153
|
+
* helper (or its own refusal) when this one says nothing. */
|
|
154
|
+
async function credential(action) {
|
|
155
|
+
const chunks = [];
|
|
156
|
+
for await (const c of process.stdin) chunks.push(c);
|
|
157
|
+
if (action !== "get") return;
|
|
158
|
+
const req = parseCredentialInput(Buffer.concat(chunks).toString("utf8"));
|
|
159
|
+
if (req.protocol !== "https" || (req.host ?? "").toLowerCase() !== "github.com") return;
|
|
160
|
+
if (connection().error) return;
|
|
161
|
+
const r = await token(explicitAccount(), ownerOf(req.path ?? ""), 15_000);
|
|
162
|
+
if (!r.ok) {
|
|
163
|
+
process.stderr.write(`shadok-github: ${refusal(r)}\n`);
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
process.stdout.write(credentialOutput(r.body.token, r.body.expiresAt));
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function setIdentity(dir, t) {
|
|
170
|
+
const g = (k, v) => execFileSync("git", ["-C", dir, "config", k, v], { stdio: "ignore" });
|
|
171
|
+
g("shadok.githubAccount", t.account);
|
|
172
|
+
g("user.name", t.login);
|
|
173
|
+
if (t.email) g("user.email", t.email);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
async function clone(argv) {
|
|
177
|
+
const o = parseFlags(argv, ["account"]);
|
|
178
|
+
const [target, dir] = o.rest;
|
|
179
|
+
if (!target) die("clone needs <owner/repo> or an https://github.com/… URL.");
|
|
180
|
+
const owner = ownerOf(target);
|
|
181
|
+
if (!owner) die(`'${target}' is not a GitHub repository (owner/repo or https://github.com/owner/repo).`);
|
|
182
|
+
const path = /^https?:\/\//i.test(target) ? target.replace(/^https?:\/\/(?:[^@/]+@)?github\.com\//i, "") : target;
|
|
183
|
+
const url = `https://github.com/${path.replace(/\.git$/, "")}.git`;
|
|
184
|
+
const r = await token(explicitAccount(o.account), owner);
|
|
185
|
+
if (!r.ok) die(refusal(r));
|
|
186
|
+
const dest = dir || path.replace(/\.git$/, "").split("/").pop();
|
|
187
|
+
// The account picked rides along with `-c` so the helper uses it for the
|
|
188
|
+
// clone, then is written into the repository for every later fetch / push.
|
|
189
|
+
const child = spawn("git", ["-c", `shadok.githubAccount=${r.body.account}`, "clone", "--", url, dest], { stdio: "inherit" });
|
|
190
|
+
const code = await new Promise((res) => child.on("close", res));
|
|
191
|
+
if (code !== 0) process.exit(code ?? 1);
|
|
192
|
+
setIdentity(dest, r.body);
|
|
193
|
+
console.log(`Cloned as ${r.body.login}; commits in ${dest} are authored by ${r.body.login} <${r.body.email}>.`);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
async function identity(argv) {
|
|
197
|
+
const o = parseFlags(argv, ["account"]);
|
|
198
|
+
const top = gitConfig("remote.origin.url");
|
|
199
|
+
const r = await token(explicitAccount(o.account), ownerOf(top ?? ""));
|
|
200
|
+
if (!r.ok) die(refusal(r));
|
|
201
|
+
setIdentity(".", r.body);
|
|
202
|
+
console.log(`This repository now pushes with ${r.body.login} and commits as ${r.body.login} <${r.body.email}>.`);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** The repository `gh` will act on, as an owner hint: -R/--repo, else origin. */
|
|
206
|
+
function ghOwner(args) {
|
|
207
|
+
for (let i = 0; i < args.length; i++) {
|
|
208
|
+
if (args[i] === "-R" || args[i] === "--repo") return ownerOf(args[i + 1] ?? "");
|
|
209
|
+
if (args[i].startsWith("--repo=")) return ownerOf(args[i].slice(7));
|
|
210
|
+
}
|
|
211
|
+
return ownerOf(gitConfig("remote.origin.url") ?? "");
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
async function gh(argv) {
|
|
215
|
+
let account;
|
|
216
|
+
let args = argv;
|
|
217
|
+
if (args[0] === "--account") {
|
|
218
|
+
account = args[1];
|
|
219
|
+
args = args.slice(2);
|
|
220
|
+
} else if (args[0]?.startsWith("--account=")) {
|
|
221
|
+
account = args[0].slice(10);
|
|
222
|
+
args = args.slice(1);
|
|
223
|
+
}
|
|
224
|
+
if (args[0] === "--") args = args.slice(1);
|
|
225
|
+
if (!args.length) die("gh needs arguments, e.g. github.mjs gh pr list");
|
|
226
|
+
const r = await token(explicitAccount(account), ghOwner(args));
|
|
227
|
+
if (!r.ok) die(refusal(r));
|
|
228
|
+
// For this one invocation only: nothing is written to gh's config.
|
|
229
|
+
const child = spawn("gh", args, { stdio: "inherit", env: { ...process.env, GH_TOKEN: r.body.token } });
|
|
230
|
+
child.on("error", (e) => die(e.code === "ENOENT" ? "gh is not installed here — use git, or the GitHub REST API with `github.mjs token`." : String(e)));
|
|
231
|
+
child.on("close", (code) => process.exit(code ?? 1));
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// Run only as a script (the test imports the pure helpers above). Compared
|
|
235
|
+
// through realpath: in a group HOME `~/.claude/skills/shadok-github` is a
|
|
236
|
+
// SYMLINK to the machine-wide copy, so argv[1] and import.meta.url differ.
|
|
237
|
+
const real = (p) => {
|
|
238
|
+
try {
|
|
239
|
+
return realpathSync(p);
|
|
240
|
+
} catch {
|
|
241
|
+
return p;
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
const invoked = !!process.argv[1] && real(process.argv[1]) === real(fileURLToPath(import.meta.url));
|
|
245
|
+
if (invoked) {
|
|
246
|
+
const [cmd, ...rest] = process.argv.slice(2);
|
|
247
|
+
if (cmd === "accounts") await accounts();
|
|
248
|
+
else if (cmd === "credential") await credential(rest[0]);
|
|
249
|
+
else if (cmd === "clone") await clone(rest);
|
|
250
|
+
else if (cmd === "identity") await identity(rest);
|
|
251
|
+
else if (cmd === "gh") await gh(rest);
|
|
252
|
+
else if (cmd === "token") {
|
|
253
|
+
const o = parseFlags(rest, ["account", "owner"]);
|
|
254
|
+
const r = await token(explicitAccount(o.account), ownerOf(o.owner ?? ""));
|
|
255
|
+
if (!r.ok) die(refusal(r));
|
|
256
|
+
process.stdout.write(r.body.token + "\n");
|
|
257
|
+
} else die(USAGE);
|
|
258
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { test } from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import http from "node:http";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { execFile } from "node:child_process";
|
|
6
|
+
import { fileURLToPath } from "node:url";
|
|
7
|
+
import { credentialOutput, ownerOf, parseCredentialInput } from "../scripts/github.mjs";
|
|
8
|
+
|
|
9
|
+
const SCRIPT = path.join(path.dirname(fileURLToPath(import.meta.url)), "..", "scripts", "github.mjs");
|
|
10
|
+
const FAKE = "ghu_FAKE_TEST_VALUE";
|
|
11
|
+
|
|
12
|
+
async function stub(status, body) {
|
|
13
|
+
const seen = [];
|
|
14
|
+
const server = http.createServer((req, res) => {
|
|
15
|
+
let raw = "";
|
|
16
|
+
req.on("data", (c) => (raw += c));
|
|
17
|
+
req.on("end", () => {
|
|
18
|
+
seen.push({ method: req.method, url: req.url, key: req.headers["x-shadok-session-key"], body: raw ? JSON.parse(raw) : undefined });
|
|
19
|
+
res.writeHead(status, { "content-type": "application/json" });
|
|
20
|
+
res.end(JSON.stringify(body));
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
await new Promise((r) => server.listen(0, "127.0.0.1", r));
|
|
24
|
+
return { port: server.address().port, seen, close: () => new Promise((r) => server.close(r)) };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** A clean env: no inherited git config, no account override. */
|
|
28
|
+
function envFor(port, extra = {}) {
|
|
29
|
+
const env = { PATH: process.env.PATH, HOME: process.env.HOME, GIT_CONFIG_NOSYSTEM: "1", GIT_CONFIG_GLOBAL: "/dev/null", ...extra };
|
|
30
|
+
if (port) Object.assign(env, { SHADOK_PORT: String(port), SHADOK_SESSION_KEY: "k-test" });
|
|
31
|
+
return env;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function run(cmd, args, env, stdin = "") {
|
|
35
|
+
return new Promise((resolve) => {
|
|
36
|
+
const child = execFile(cmd, args, { env, timeout: 20_000 }, (err, stdout, stderr) =>
|
|
37
|
+
resolve({ code: err ? (typeof err.code === "number" ? err.code : 1) : 0, stdout, stderr }),
|
|
38
|
+
);
|
|
39
|
+
child.stdin.end(stdin);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
test("parseCredentialInput reads key=value lines up to the blank line", () => {
|
|
44
|
+
assert.deepEqual(parseCredentialInput("protocol=https\nhost=github.com\npath=acme/app.git\n\nignored=1\n"), {
|
|
45
|
+
protocol: "https",
|
|
46
|
+
host: "github.com",
|
|
47
|
+
path: "acme/app.git",
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("credentialOutput: x-access-token, the password, and its expiry in seconds", () => {
|
|
52
|
+
assert.equal(credentialOutput("t", 1_700_000_000_999), "username=x-access-token\npassword=t\npassword_expiry_utc=1700000000\n");
|
|
53
|
+
// A lasting token (non-expiring app) carries no expiry line.
|
|
54
|
+
assert.equal(credentialOutput("t", Number.MAX_SAFE_INTEGER), "username=x-access-token\npassword=t\n");
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test("ownerOf reads owner/repo, .git, and github URLs", () => {
|
|
58
|
+
assert.equal(ownerOf("acme/app"), "acme");
|
|
59
|
+
assert.equal(ownerOf("acme/app.git"), "acme");
|
|
60
|
+
assert.equal(ownerOf("https://github.com/acme/app.git"), "acme");
|
|
61
|
+
assert.equal(ownerOf("https://x-access-token@github.com/acme/app"), "acme");
|
|
62
|
+
assert.equal(ownerOf("git@github.com:acme/app.git"), "acme");
|
|
63
|
+
assert.equal(ownerOf(""), null);
|
|
64
|
+
assert.equal(ownerOf("../etc"), null);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test("credential get answers github.com with the server's current token", async () => {
|
|
68
|
+
const s = await stub(200, { account: "a1", login: "octo", email: "1+octo@users.noreply.github.com", token: FAKE, expiresAt: 1_700_000_000_000 });
|
|
69
|
+
try {
|
|
70
|
+
const r = await run("node", [SCRIPT, "credential", "get"], envFor(s.port), "protocol=https\nhost=github.com\npath=acme/app.git\n\n");
|
|
71
|
+
assert.equal(r.code, 0);
|
|
72
|
+
assert.equal(r.stdout, `username=x-access-token\npassword=${FAKE}\npassword_expiry_utc=1700000000\n`);
|
|
73
|
+
assert.equal(s.seen.length, 1);
|
|
74
|
+
assert.equal(s.seen[0].url, "/github/token");
|
|
75
|
+
assert.equal(s.seen[0].key, "k-test");
|
|
76
|
+
// The owner from git's path is the hint for the account pick.
|
|
77
|
+
assert.deepEqual(s.seen[0].body, { owner: "acme" });
|
|
78
|
+
} finally {
|
|
79
|
+
await s.close();
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test("credential stays silent for another host, for store/erase, and outside an agent", async () => {
|
|
84
|
+
const s = await stub(200, { token: FAKE });
|
|
85
|
+
try {
|
|
86
|
+
const other = await run("node", [SCRIPT, "credential", "get"], envFor(s.port), "protocol=https\nhost=gitlab.com\n\n");
|
|
87
|
+
assert.equal(other.stdout, "");
|
|
88
|
+
const store = await run("node", [SCRIPT, "credential", "store"], envFor(s.port), "protocol=https\nhost=github.com\nusername=x\npassword=y\n\n");
|
|
89
|
+
assert.equal(store.stdout, "");
|
|
90
|
+
const plain = await run("node", [SCRIPT, "credential", "get"], envFor(null), "protocol=https\nhost=github.com\n\n");
|
|
91
|
+
assert.equal(plain.code, 0);
|
|
92
|
+
assert.equal(plain.stdout, "");
|
|
93
|
+
assert.equal(s.seen.length, 0);
|
|
94
|
+
} finally {
|
|
95
|
+
await s.close();
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test("credential refusal: nothing on stdout, the reason (and the accounts) on stderr, exit 0", async () => {
|
|
100
|
+
const s = await stub(409, { error: "Several GitHub accounts are shared with this agent — pick one.", accounts: [{ id: "a1", login: "octo", owners: ["acme"] }] });
|
|
101
|
+
try {
|
|
102
|
+
const r = await run("node", [SCRIPT, "credential", "get"], envFor(s.port), "protocol=https\nhost=github.com\n\n");
|
|
103
|
+
assert.equal(r.code, 0);
|
|
104
|
+
assert.equal(r.stdout, "");
|
|
105
|
+
assert.match(r.stderr, /shadok-github: Several GitHub accounts/);
|
|
106
|
+
assert.match(r.stderr, /a1 {2}octo {2}repos of: acme/);
|
|
107
|
+
} finally {
|
|
108
|
+
await s.close();
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test("an explicit account comes from SHADOK_GITHUB_ACCOUNT", async () => {
|
|
113
|
+
const s = await stub(200, { account: "b2", login: "x", email: "", token: FAKE, expiresAt: 1 });
|
|
114
|
+
try {
|
|
115
|
+
await run("node", [SCRIPT, "credential", "get"], envFor(s.port, { SHADOK_GITHUB_ACCOUNT: "b2" }), "protocol=https\nhost=github.com\n\n");
|
|
116
|
+
assert.deepEqual(s.seen[0].body, { account: "b2" });
|
|
117
|
+
} finally {
|
|
118
|
+
await s.close();
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
test("git itself gets the credential through the helper the spawn configures", async () => {
|
|
123
|
+
// A FUTURE expiry, and it is load-bearing: `credentialOutput` reports it as
|
|
124
|
+
// `password_expiry_utc`, and git's `fill` SKIPS a credential whose expiry has
|
|
125
|
+
// passed — it keeps the username, finds no password, and (prompts disabled)
|
|
126
|
+
// dies with "could not read Password". A fixed past instant therefore made
|
|
127
|
+
// this test assert the opposite of what it checks, on any git that honours
|
|
128
|
+
// the attribute. Production always sends a real deadline (~8 h out).
|
|
129
|
+
const s = await stub(200, { account: "a1", login: "octo", email: "", token: FAKE, expiresAt: Date.now() + 8 * 3_600_000 });
|
|
130
|
+
try {
|
|
131
|
+
// The exact env shape `githubGitEnv` (src/github.ts) gives every agent.
|
|
132
|
+
const env = envFor(s.port, {
|
|
133
|
+
GIT_CONFIG_COUNT: "2",
|
|
134
|
+
GIT_CONFIG_KEY_0: "shadok.githubAccount",
|
|
135
|
+
GIT_CONFIG_VALUE_0: "a1",
|
|
136
|
+
GIT_CONFIG_KEY_1: "credential.https://github.com.helper",
|
|
137
|
+
GIT_CONFIG_VALUE_1: `!node '${SCRIPT}' credential`,
|
|
138
|
+
GIT_TERMINAL_PROMPT: "0",
|
|
139
|
+
});
|
|
140
|
+
const r = await run("git", ["credential", "fill"], env, "protocol=https\nhost=github.com\n\n");
|
|
141
|
+
assert.equal(r.code, 0, r.stderr);
|
|
142
|
+
assert.match(r.stdout, /^username=x-access-token$/m);
|
|
143
|
+
assert.match(r.stdout, new RegExp(`^password=${FAKE}$`, "m"));
|
|
144
|
+
// `git config shadok.githubAccount` (here from GIT_CONFIG_*) names the account.
|
|
145
|
+
assert.deepEqual(s.seen[0].body, { account: "a1" });
|
|
146
|
+
} finally {
|
|
147
|
+
await s.close();
|
|
148
|
+
}
|
|
149
|
+
});
|
|
@@ -44,8 +44,8 @@ async function call(method, route, payload) {
|
|
|
44
44
|
const day = (ms) => new Date(ms).toISOString().slice(0, 10);
|
|
45
45
|
|
|
46
46
|
async function accounts() {
|
|
47
|
-
// The route lists every connected account; Google's belong to
|
|
48
|
-
const list = ((await call("GET", "/social/accounts")).accounts ?? []).filter((a) => a.provider
|
|
47
|
+
// The route lists every connected account; Google's and GitHub's belong to their own skills.
|
|
48
|
+
const list = ((await call("GET", "/social/accounts")).accounts ?? []).filter((a) => a.provider === "linkedin" || a.provider === undefined);
|
|
49
49
|
if (!list.length) {
|
|
50
50
|
console.log("No LinkedIn account is available to this agent. Ask the person to connect one in Secrets → Connected accounts, or to share theirs with this agent's group.");
|
|
51
51
|
return;
|
package/dist/account-expiry.d.ts
CHANGED
|
@@ -22,7 +22,7 @@ export interface ExpiryActions {
|
|
|
22
22
|
}
|
|
23
23
|
/**
|
|
24
24
|
* Warn once a day from 7 days (Google: 2) before the account's deadline;
|
|
25
|
-
* expire at the deadline (`accountDeadline` — for Google the refresh token's,
|
|
25
|
+
* expire at the deadline (`accountDeadline` — for Google and GitHub the refresh token's,
|
|
26
26
|
* and an account with no known deadline is never warned nor expired by date).
|
|
27
27
|
* An account already marked expired is neither warned nor expired again. The
|
|
28
28
|
* draft TTL is `expireDrafts`' — one definition.
|
package/dist/account-expiry.js
CHANGED
|
@@ -22,7 +22,7 @@ export const STALE_PUBLISHING_SENTENCE = "The cockpit restarted while publishing
|
|
|
22
22
|
export const STALE_GOOGLE_SENTENCE = "The cockpit restarted while sending; check Gmail's Sent folder or the calendar before retrying.";
|
|
23
23
|
/**
|
|
24
24
|
* Warn once a day from 7 days (Google: 2) before the account's deadline;
|
|
25
|
-
* expire at the deadline (`accountDeadline` — for Google the refresh token's,
|
|
25
|
+
* expire at the deadline (`accountDeadline` — for Google and GitHub the refresh token's,
|
|
26
26
|
* and an account with no known deadline is never warned nor expired by date).
|
|
27
27
|
* An account already marked expired is neither warned nor expired again. The
|
|
28
28
|
* draft TTL is `expireDrafts`' — one definition.
|
|
@@ -64,6 +64,12 @@ function isoDay(ms) {
|
|
|
64
64
|
return new Date(ms).toISOString().slice(0, 10);
|
|
65
65
|
}
|
|
66
66
|
export function expiryWarningText(a) {
|
|
67
|
+
if (a.provider === "github") {
|
|
68
|
+
// The watch renews a GitHub authorisation itself (`needsKeepalive`): a
|
|
69
|
+
// warning means that renewal failed.
|
|
70
|
+
const d = accountDeadline({ provider: "github", expiresAt: a.expiresAt, refreshExpiresAt: a.refreshExpiresAt }) ?? a.expiresAt;
|
|
71
|
+
return `Your GitHub connection '${a.label}' must be reconnected by ${isoDay(d)} (the cockpit could not renew it). Reconnect it in Secrets → Connected accounts so agents keep reaching your repositories.`;
|
|
72
|
+
}
|
|
67
73
|
if (a.provider === "google") {
|
|
68
74
|
const d = accountDeadline({ provider: "google", expiresAt: a.expiresAt, refreshExpiresAt: a.refreshExpiresAt }) ?? a.expiresAt;
|
|
69
75
|
return `Your Google connection '${a.label}' must be reconnected by ${isoDay(d)} (test app authorisations last 7 days). Reconnect it in Secrets → Connected accounts so agents keep working in it.`;
|