Files
Codewhale/scripts/ci/cnb-github-checkrun.mjs
CodeWhale BotandClaude Opus 5.5 59e941a511 docs: point every link at the codewhale-hq organization
Hunter moved the Codewhale repositories to github.com/codewhale-hq on
2026-10-05. Rewrite Hmbown/<moved repo> slugs to codewhale-hq/<repo> in
docs, READMEs (all 19), website, CHANGELOGs, workflows, issue templates,
Cargo/npm/VS Code/nix/winget metadata, install and updater code, the
bundled first-party marketplace catalog and their tests: 1,795 references
in 434 files, plus four escaped regex literals. Docker images publish to
ghcr.io/codewhale-hq/codewhale from the next release.

Old URLs keep working through GitHub's transfer redirects (verified live:
API releases/latest, HTML releases/latest, release asset downloads,
codeload tarballs, git clone), so released clients keep updating.

Left unchanged on purpose: Hmbown/codewhale-cu-plugin (not moved yet; its
shipped updater refuses redirects), Hmbown/deepseek-skills, cwc and other
repos that did not move; package identities (winget Hmbown.CodeWhale, NSIS
publisher); vendor/codewhale-ratatui (frozen reviewed package); and the
extension-host GitHub adapter with tools/github/report.rs and their parity
fixture, which need a bundle rebuild and move together in a follow-up.

README translation stamps updated to the new README.md hash.

Validation: npm test (wrapper 101/0, SDK 19/0, extension host 398 passed /
0 failed / 7 skipped, web 763/0) and npm run check:web exit 0 (one
pre-existing facts-lib.mjs lint warning). cargo check --workspace --tests
--all-features --locked: exit 0. cargo test -p codewhale-cli -p
codewhale-release -p codewhale-protocol and the touched codewhale-tui
modules (client, cloud_dispatch, commands, dispatch_runner, llm_client::mock,
oauth, operate, plugins::marketplace, remote_control, remote_setup,
tools::mcp_registry, tools::web::fetch, working_set, tui::ui::tests): all
ok, 0 failed. check-readme-translations (18 in sync), readme locales, TUI
locale parity, product vocabulary, web locales, contributor credit, plugin
claims, release-workflows.test.js and check-versions all pass.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: CodeWhale Bot <[email protected]>
2026-10-04 18:09:44 -07:00

148 lines
5.7 KiB
JavaScript

#!/usr/bin/env node
// Post a GitHub Check Run for the exact commit a CNB pipeline is building,
// authenticating as the codewhale-cnb-bridge GitHub App.
//
// Secret custody: the App credentials live in the CNB KeyStore repo
// codewhale.net/codewhale-ci-secrets (github-bridge.yml) and are injected as
// environment variables via `imports:` in .cnb.yml
// (https://docs.cnb.cool/en/repo/secret.html). This script reads them from the
// environment and never logs them. Accepted variable names (first match wins):
// app id: GITHUB_APP_ID, GH_APP_ID, APP_ID
// installation id: GITHUB_APP_INSTALLATION_ID, GH_APP_INSTALLATION_ID, INSTALLATION_ID
// private key: GITHUB_APP_PRIVATE_KEY, GH_APP_PRIVATE_KEY, PRIVATE_KEY
// The private key may be a raw PEM, a PEM with escaped \n, or base64-encoded.
//
// Usage:
// node scripts/ci/cnb-github-checkrun.mjs \
// --name "linux rust gates -cnb" --sha <40-hex> \
// --status completed --conclusion success \
// --details-url <url> --summary <text>
import crypto from "node:crypto";
const GITHUB_API = process.env.GITHUB_API_BASE || "https://api.github.com";
const REPO = process.env.GITHUB_REPOSITORY || "codewhale-hq/CodeWhale";
const USER_AGENT = "codewhale-cnb-bridge";
function fail(message) {
console.error(`cnb-github-checkrun: ${message}`);
process.exit(1);
}
function envAny(...names) {
for (const name of names) {
const value = process.env[name];
if (value && value.trim()) return value.trim();
}
return "";
}
function parseArgs(argv) {
const args = {};
for (let i = 0; i < argv.length; i += 1) {
const token = argv[i];
if (!token.startsWith("--")) fail(`unexpected argument ${JSON.stringify(token)}`);
const key = token.slice(2);
const value = argv[i + 1];
if (value === undefined || value.startsWith("--")) fail(`--${key} requires a value`);
args[key] = value;
i += 1;
}
return args;
}
function normalizePrivateKey(raw) {
const withNewlines = raw.replace(/\\n/g, "\n");
if (withNewlines.includes("BEGIN")) return withNewlines;
const decoded = Buffer.from(raw, "base64").toString("utf8");
if (decoded.includes("BEGIN")) return decoded;
fail("private key does not look like a PEM (raw, escaped, or base64)");
}
function mintAppJwt(appId, privateKey) {
const now = Math.floor(Date.now() / 1000);
const encode = (obj) => Buffer.from(JSON.stringify(obj)).toString("base64url");
const header = encode({ alg: "RS256", typ: "JWT" });
const payload = encode({ iat: now - 60, exp: now + 540, iss: appId });
const unsigned = `${header}.${payload}`;
const signature = crypto.sign("sha256", Buffer.from(unsigned), privateKey).toString("base64url");
return `${unsigned}.${signature}`;
}
async function githubApi(path, token, method, body) {
const response = await fetch(`${GITHUB_API}${path}`, {
method,
headers: {
Authorization: `Bearer ${token}`,
Accept: "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
"User-Agent": USER_AGENT,
},
body: body ? JSON.stringify(body) : undefined,
});
if (!response.ok) {
// GitHub error bodies never contain our credentials; cap the log line anyway.
const text = (await response.text()).slice(0, 500);
fail(`${method} ${path} -> HTTP ${response.status}: ${text}`);
}
return response.json();
}
async function main() {
const args = parseArgs(process.argv.slice(2));
const name = args.name || fail("--name is required");
const sha = args.sha || fail("--sha is required");
if (!/^[0-9a-f]{40}$/.test(sha)) {
fail(`--sha must be a 40-character lowercase hex commit, got ${JSON.stringify(sha)}`);
}
const status = args.status || "completed";
if (!["queued", "in_progress", "completed"].includes(status)) {
fail(`--status must be queued|in_progress|completed, got ${JSON.stringify(status)}`);
}
const conclusions = ["success", "failure", "cancelled", "neutral", "skipped", "timed_out"];
const conclusion = args.conclusion || "";
if (status === "completed" && !conclusions.includes(conclusion)) {
fail(`--conclusion must be one of ${conclusions.join("|")} when --status completed`);
}
const appId = envAny("GITHUB_APP_ID", "GH_APP_ID", "APP_ID") ||
fail("GitHub App id env var missing (expected GITHUB_APP_ID)");
const installationId = envAny("GITHUB_APP_INSTALLATION_ID", "GH_APP_INSTALLATION_ID", "INSTALLATION_ID") ||
fail("GitHub App installation id env var missing (expected GITHUB_APP_INSTALLATION_ID)");
const privateKey = normalizePrivateKey(
envAny("GITHUB_APP_PRIVATE_KEY", "GH_APP_PRIVATE_KEY", "PRIVATE_KEY") ||
fail("GitHub App private key env var missing (expected GITHUB_APP_PRIVATE_KEY)"),
);
const jwt = mintAppJwt(appId, privateKey);
const installation = await githubApi(
`/app/installations/${encodeURIComponent(installationId)}/access_tokens`,
jwt,
"POST",
);
if (!installation.token) fail("installation token response had no token field");
const now = new Date().toISOString();
const checkRun = {
name,
head_sha: sha,
status,
output: {
title: args.title || `${name}: ${status === "completed" ? conclusion : status}`,
summary: args.summary || "",
},
};
if (args["details-url"]) checkRun.details_url = args["details-url"];
if (status === "completed") {
checkRun.conclusion = conclusion;
checkRun.completed_at = now;
} else {
checkRun.started_at = now;
}
const created = await githubApi(`/repos/${REPO}/check-runs`, installation.token, "POST", checkRun);
// Receipt line: id, conclusion, and URL only — never any credential material.
console.log(`check run ${created.id} ${status}${conclusion ? `/${conclusion}` : ""} ${created.html_url}`);
}
main().catch((error) => fail(error && error.message ? error.message : String(error)));