notusci 0.1.14 → 0.1.15
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/package.json +1 -1
- package/src/cli.mjs +31 -9
- package/src/core.mjs +17 -5
package/package.json
CHANGED
package/src/cli.mjs
CHANGED
|
@@ -7,7 +7,7 @@ import { homedir, hostname } from "node:os";
|
|
|
7
7
|
import { delimiter, dirname, join } from "node:path";
|
|
8
8
|
import { setTimeout as sleep } from "node:timers/promises";
|
|
9
9
|
import {
|
|
10
|
-
CliError, HELP, NPX, addTakeoverGuard, apiUrl, authFile, billingText, boundedWaitSeconds, buildSource, dependencyHint, encodeSource, jsonText, parseCommand, pickWorkflow, renderError,
|
|
10
|
+
CliError, HELP, NPX, addTakeoverGuard, apiUrl, githubPlan, authFile, billingText, boundedWaitSeconds, buildSource, dependencyHint, encodeSource, jsonText, parseCommand, pickWorkflow, renderError,
|
|
11
11
|
repositoryFromRemote, runPath, setCodexServer, setCursorServer, shouldWaitAgain, sourceBase, sourceIdentity,
|
|
12
12
|
} from "./core.mjs";
|
|
13
13
|
|
|
@@ -222,31 +222,53 @@ async function init(s, input) {
|
|
|
222
222
|
}
|
|
223
223
|
}
|
|
224
224
|
if (input.billing) return billing(s, { repo, timeoutSeconds: 90 });
|
|
225
|
-
const githubFirst = input.githubFirst && await githubFirstSetup(s, root, listed(me, repo)
|
|
225
|
+
const githubFirst = input.githubFirst && await githubFirstSetup(s, root, me, listed(me, repo), workflow, input.githubFirst, deadline);
|
|
226
226
|
print({ status: "ready", user: me.user, repo: listed(me, repo).repo, scopes: me.scopes, workflow, mcp, ...(githubFirst && { githubFirst }), next: `${NPX} verify` });
|
|
227
227
|
}
|
|
228
228
|
|
|
229
|
+
/** The account's GitHub plan from the local `gh` login, when it can see it. @param {string} owner @param {string} login */
|
|
230
|
+
function ghPlan(owner, login) {
|
|
231
|
+
try {
|
|
232
|
+
return githubPlan(execFileSync("gh", ["api", owner.toLowerCase() === login.toLowerCase() ? "user" : `orgs/${owner}`, "--jq", ".plan.name"],
|
|
233
|
+
{ encoding: "utf8", stdio: ["ignore", "pipe", "ignore"], timeout: 10_000 }));
|
|
234
|
+
} catch { return undefined; }
|
|
235
|
+
}
|
|
236
|
+
|
|
229
237
|
/**
|
|
230
|
-
* Spec 070: guards the workflow's job so GitHub skips it during takeover, then turns GitHub-first on.
|
|
238
|
+
* Spec 070, init's default: guards the workflow's job so GitHub skips it during takeover, then turns GitHub-first on.
|
|
231
239
|
* A workflow with more than one job stays on GitHub, watched only. Never commits or pushes.
|
|
232
|
-
* @param {Session} s @param {string} root @param {
|
|
240
|
+
* @param {Session} s @param {string} root @param {any} me @param {{repo: string, pushWorkflow: string | null}} listing
|
|
241
|
+
* @param {string} workflow @param {string | true} flag a plan from --github-first, or true for the default @param {number} deadline
|
|
233
242
|
*/
|
|
234
|
-
async function githubFirstSetup(s, root,
|
|
243
|
+
async function githubFirstSetup(s, root, me, listing, workflow, flag, deadline) {
|
|
244
|
+
const { repo } = listing;
|
|
245
|
+
const off = (/** @type {string} */ reason) => ({ status: "off", reason });
|
|
246
|
+
if (!me.scopes.includes("ship:prod")) return off("changing repo settings needs the ship:prod scope; rerun init with it");
|
|
247
|
+
const usage = await api(s, "/api/github-usage", { query: { repo }, deadline });
|
|
248
|
+
// A repository already on NotusCI only keeps it unless asked.
|
|
249
|
+
if (flag === true && listing.pushWorkflow && !usage.watching.includes(repo)) return off(`pushes already run on NotusCI (${listing.pushWorkflow}); pass --github-first PLAN to switch`);
|
|
235
250
|
const path = join(root, workflow);
|
|
236
251
|
const before = readFileSync(path, "utf8");
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
if (
|
|
252
|
+
let guarded;
|
|
253
|
+
try { guarded = addTakeoverGuard(before); }
|
|
254
|
+
catch (error) { if (error instanceof CliError && error.body.code === "workflow_refused") return off(error.body.error); throw error; }
|
|
255
|
+
const detected = typeof flag === "string" ? undefined : usage.plan ?? ghPlan(repo.split("/")[0], me.user.login);
|
|
256
|
+
const plan = typeof flag === "string" ? flag : detected ?? "free";
|
|
257
|
+
const takeover = guarded.jobs === 1;
|
|
258
|
+
if (takeover && guarded.text !== before) writeFileSync(path, guarded.text);
|
|
240
259
|
const saved = await api(s, `/api/repos/${repo}/settings`, { method: "PUT", deadline,
|
|
241
260
|
body: { githubFirst: { plan }, ...(takeover && { pushWorkflow: workflow }) } });
|
|
242
261
|
const diff = execFileSync("git", ["diff", "--", workflow], { cwd: root, encoding: "utf8" });
|
|
243
262
|
return {
|
|
244
|
-
plan,
|
|
263
|
+
status: "on", plan, planSource: typeof flag === "string" ? "flag" : usage.plan ? "stated" : detected ? "gh" : "default",
|
|
264
|
+
takeover: takeover ? "on" : `off: ${workflow} has ${guarded.jobs} jobs and NotusCI runs one; pushes stay on GitHub, watched`,
|
|
245
265
|
...(diff && { diff }), variables: saved.variables,
|
|
246
266
|
next: [
|
|
247
267
|
...(diff ? [`Commit and push ${workflow}`] : []),
|
|
248
268
|
"Tell the human: in branch protection, require the NotusCI check instead of the GitHub job",
|
|
249
269
|
...(saved.next ? [`Tell the human: ${saved.next}`] : []),
|
|
270
|
+
...(!detected && typeof flag !== "string" ? ["Plan assumed free (switches earliest); if the account pays GitHub, rerun init --github-first pro|team|enterprise"] : []),
|
|
271
|
+
`Undo: revert ${workflow}, then ${NPX} repo set --settings '{"githubFirst":null,"pushWorkflow":null}'`,
|
|
250
272
|
],
|
|
251
273
|
};
|
|
252
274
|
}
|
package/src/core.mjs
CHANGED
|
@@ -29,7 +29,7 @@ function usage(error) {
|
|
|
29
29
|
|
|
30
30
|
/** @type {Record<string, Record<string, {type: "string" | "boolean"}>>} */
|
|
31
31
|
const OPTIONS = {
|
|
32
|
-
init: { scope: { type: "string" }, "no-mcp": { type: "boolean" }, billing: { type: "boolean" }, "github-first": { type: "string" } },
|
|
32
|
+
init: { scope: { type: "string" }, "no-mcp": { type: "boolean" }, billing: { type: "boolean" }, "github-first": { type: "string" }, "no-github-first": { type: "boolean" } },
|
|
33
33
|
login: { scope: { type: "string" } },
|
|
34
34
|
doctor: {}, logout: {}, token: {}, whoami: {}, repos: {}, version: {}, "--version": {},
|
|
35
35
|
target: { repo: { type: "string" }, file: { type: "string" }, "auto-promote": { type: "string" }, "auto-release": { type: "string" } },
|
|
@@ -50,7 +50,7 @@ const OPTIONS = {
|
|
|
50
50
|
|
|
51
51
|
export const HELP = {
|
|
52
52
|
commands: [
|
|
53
|
-
"init [--scope SCOPES] [--no-mcp] [--billing] [--github-first free|pro|team|enterprise]", "billing [--repo OWNER/REPO] [--yearly] [--timeout SECONDS]", "billing status [--repo OWNER/REPO] [--json]", "usage [--repo OWNER/REPO] [--cap USD]", "doctor",
|
|
53
|
+
"init [--scope SCOPES] [--no-mcp] [--billing] [--github-first free|pro|team|enterprise | --no-github-first]", "billing [--repo OWNER/REPO] [--yearly] [--timeout SECONDS]", "billing status [--repo OWNER/REPO] [--json]", "usage [--repo OWNER/REPO] [--cap USD]", "doctor",
|
|
54
54
|
"login [--scope SCOPES]", "logout", "whoami", "repos", "token list", "token revoke ID",
|
|
55
55
|
"target set [--repo OWNER/REPO] [--file TARGET.json] [--auto-promote on|off] [--auto-release on|off]",
|
|
56
56
|
"secret set NAME [--repo OWNER/REPO] (value on stdin)", "secret list [--repo OWNER/REPO]", "secret rm NAME [--repo OWNER/REPO]",
|
|
@@ -115,8 +115,10 @@ export function parseCommand(args) {
|
|
|
115
115
|
const unknown = scopes.filter(scope => !SCOPES.includes(scope));
|
|
116
116
|
if (unknown.length) throw usage(`unknown scope ${unknown.join(",")}; scopes are ${SCOPES.join(",")}`);
|
|
117
117
|
const githubFirst = values["github-first"];
|
|
118
|
-
if (githubFirst !== undefined && !
|
|
119
|
-
|
|
118
|
+
if (githubFirst !== undefined && !GITHUB_PLANS.includes(githubFirst)) throw usage("--github-first takes your GitHub plan: free, pro, team or enterprise");
|
|
119
|
+
if (githubFirst && values["no-github-first"]) throw usage("pass --github-first PLAN or --no-github-first, not both");
|
|
120
|
+
return command === "init" ? { command, scopes, mcp: !values["no-mcp"], billing: values.billing === true,
|
|
121
|
+
githubFirst: values["no-github-first"] ? false : githubFirst ?? true } : { command, scopes };
|
|
120
122
|
}
|
|
121
123
|
if (["doctor", "logout", "whoami", "repos"].includes(command)) { expect(); return { command }; }
|
|
122
124
|
if (command === "version" || command === "--version") { expect(); return { command: "version" }; }
|
|
@@ -289,7 +291,17 @@ export function renderError({ error = "", code, next, ...extra }, now = Date.now
|
|
|
289
291
|
return { error, code, next: next ?? defaults[code] ?? "Tell the human the error.", ...extra };
|
|
290
292
|
}
|
|
291
293
|
|
|
292
|
-
|
|
294
|
+
const GITHUB_PLANS = ["free", "pro", "team", "enterprise"];
|
|
295
|
+
|
|
296
|
+
/** GitHub's plan name (from `gh api user` or `gh api orgs/OWNER`) as a githubFirst plan; undefined when unknown. @param {unknown} name */
|
|
297
|
+
export function githubPlan(name) {
|
|
298
|
+
const value = String(name ?? "").trim().toLowerCase();
|
|
299
|
+
if (GITHUB_PLANS.includes(value)) return value;
|
|
300
|
+
if (/^(enterprise|business)/.test(value)) return "enterprise";
|
|
301
|
+
return undefined;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
export const TAKEOVER_GUARD ="vars.NOTUSCI_TAKEOVER != 'true' || github.event.pull_request.head.repo.fork";
|
|
293
305
|
|
|
294
306
|
/**
|
|
295
307
|
* Spec 070: adds the takeover guard to each job's `if:`, keeping an existing one with `&&`.
|