diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ae1c1f2..bd2986a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,11 +7,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/). ## [Unreleased] ### Added -- **Context-lock now covers your SSH identity per project.** A new `[context.ssh]` block locks which key a repo pushes/deploys with — declare any of `identity` (the IdentityFile path), `fingerprint` (`SHA256:…`, machine-portable), or `host_alias` (the `~/.ssh/config` Host the remote uses). `kit context check` reads the repo's *effective* identity — a per-repo `core.sshCommand` `-i` override wins, otherwise it resolves the remote host through `ssh -G` — derives the key fingerprint via `ssh-keygen -lf`, and verifies it matches. Pushing a repo with the wrong account's key is a mismatch, not a silent pass — the SSH analog of the git-host remote lock. Pure parsers (`core.sshCommand` `-i`, `ssh -G` identityfile, keygen fingerprint, remote host) are unit-tested; the live `ssh -G` read smoke-tests on a real machine. +- **`kit health` adds a Vercel sensor (failed production deploys).** Probes the Vercel REST API (`GET /v6/deployments?target=production`) with `VERCEL_TOKEN`, using the `projectId`/`teamId` from `.vercel/project.json`; flags the most recent *terminal* production deployment as red when its state is `ERROR` (`CANCELED` is not red), and reports `unknown` (never a false `green`) when the project isn't linked, the token is missing, or the API errors. Reuses the `httpGet` probe path the GitLab/Bitbucket sensors introduced. Live API smoke is pending a real token; the parsers are fixture-tested. ## [1.13.0] - 2026-06-22 ### Added +- **Context-lock now covers your SSH identity per project.** A new `[context.ssh]` block locks which key a repo pushes/deploys with — declare any of `identity` (the IdentityFile path), `fingerprint` (`SHA256:…`, machine-portable), or `host_alias` (the `~/.ssh/config` Host the remote uses). `kit context check` reads the repo's *effective* identity — a per-repo `core.sshCommand` `-i` override wins, otherwise it resolves the remote host through `ssh -G` — derives the key fingerprint via `ssh-keygen -lf`, and verifies it matches. Pushing a repo with the wrong account's key is a mismatch, not a silent pass — the SSH analog of the git-host remote lock. Pure parsers (`core.sshCommand` `-i`, `ssh -G` identityfile, keygen fingerprint, remote host) are unit-tested; the live `ssh -G` read smoke-tests on a real machine. - **`kit health` now covers all three git hosts: GitLab CI and Bitbucket Pipelines sensors (GitHub was already there).** Both probe the platform REST API via a new `httpGet` on `HealthDeps` (the HTTP-probe path the sentinel design anticipated): GitLab `GET /api/v4/projects/:path/pipelines` with `GITLAB_TOKEN`, Bitbucket `GET /2.0/repositories/:ws/:repo/pipelines/` with `BITBUCKET_TOKEN` (or `BITBUCKET_USERNAME` + `BITBUCKET_APP_PASSWORD`). Each flags the most recent terminal pipeline as red when it failed, records the `host/path` it checked, and reports `unknown` (never a false `green`) on a missing token, a non-OK response, or no git remote. Sensors are selected by CI-file presence (`.gitlab-ci.yml` / `bitbucket-pipelines.yml`). Also: `analyze`'s CI-file detection now recognizes `bitbucket-pipelines.yml` (it knew `.github/workflows` + `.gitlab-ci.yml` but missed Bitbucket). Live API smoke is pending a real GitLab/Bitbucket project + tokens; the parsers + auth + remote-parsing are fixture-tested against the documented schemas. - **Context-lock now covers GitLab and Bitbucket, not just GitHub.** `[context.gitlab]` (`group`, `remote`) and `[context.bitbucket]` (`workspace`, `remote`) join `[context.github]`: `kit context check` parses the live `origin` remote per host and verifies it matches the declared values, so a repo pushed to the wrong GitLab group or Bitbucket workspace is a mismatch, not a silent pass (same cross-account guard as GitHub). The brownfield `kit init` offer (`suggestContextToml` / `hasLockableContext`) now also surfaces a detected GitLab/Bitbucket binding. One `(tool, field)` row each — the lock table stays data-driven. - **Two services in the registry: Keycloak and Atlassian (acli).** Keycloak is detected from its clients (`keycloak-js`, `keycloak-connect`, `keycloak-admin-client`, `keycloak-angular`, `python-keycloak`, Go `gocloak`) and declares its realm/admin secrets (`KEYCLOAK_URL`, `KEYCLOAK_REALM`, `KEYCLOAK_CLIENT_ID/SECRET`, `KEYCLOAK_ADMIN/_PASSWORD`); it carries no mise tool because it is a self-hosted server (run via Docker), with admin through the server's own `kcadm.sh`. Atlassian is detected from `bitbucket-pipelines.yml` / `.bitbucket`, provisions the Atlassian CLI via mise (`tool: acli` → `aqua:atlassian.com/acli`), and tracks `ATLASSIAN_API_TOKEN` / `ATLASSIAN_SITE_URL`; its auth is left as an informational note rather than a guessed login command. Adding each was a single registry data row (no detector/generator edits), per the unified-registry design. diff --git a/src/cli.ts b/src/cli.ts index ca018c5f..2929bcca 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -4061,15 +4061,26 @@ async function cmdHealth(): Promise { const { syncHealthFindings } = await import("./health-track.js"); const { execFileNoThrow } = await import("./utils/execFileNoThrow.js"); - // git remote + CI-file presence drive sensor selection + // git remote + CI-file presence + Vercel link drive sensor selection const remote = await execFileNoThrow("git", ["remote"], { timeout: 5_000 }); const cwd = process.cwd(); + let vercel: { orgId?: string; projectId?: string } | undefined; + try { + const vj = JSON.parse(readFileSync(resolve(cwd, ".vercel", "project.json"), "utf8")) as { + orgId?: string; + projectId?: string; + }; + if (vj.projectId) vercel = { orgId: vj.orgId, projectId: vj.projectId }; + } catch { + vercel = undefined; + } const ctx = { cwd, config, gitRemote: remote.ok && remote.stdout.trim().length > 0, gitlabCi: existsSync(resolve(cwd, ".gitlab-ci.yml")), bitbucketPipelines: existsSync(resolve(cwd, "bitbucket-pipelines.yml")), + vercel, }; const sensors = selectSensors(ctx); diff --git a/src/health-sensors/vercel.test.ts b/src/health-sensors/vercel.test.ts new file mode 100644 index 00000000..6dd9dd4b --- /dev/null +++ b/src/health-sensors/vercel.test.ts @@ -0,0 +1,73 @@ +import { describe, it, afterEach } from "node:test"; +import assert from "node:assert/strict"; +import { parseVercelDeployments, latestFailedVercel, vercelSensor } from "./vercel.js"; +import type { HealthCtx, HealthDeps, HttpResponse } from "../health.js"; + +const body = JSON.stringify({ + deployments: [ + { uid: "dpl_3", state: "ERROR", target: "production", created: 3000 }, + { uid: "dpl_2", state: "READY", target: "production", created: 2000 }, + { uid: "dpl_4", state: "BUILDING", target: "production", created: 4000 }, + ], +}); + +function deps(over: { http?: HttpResponse } = {}): HealthDeps { + return { + runCli: async () => ({ stdout: "", stderr: "", exitCode: 0, ok: true }), + httpGet: async () => over.http ?? { ok: true, status: 200, body }, + }; +} +const linked: HealthCtx = { cwd: "/tmp/repo", config: {}, vercel: { orgId: "team_1", projectId: "prj_abc" } }; + +describe("parseVercelDeployments / latestFailedVercel", () => { + it("flags the latest TERMINAL deploy when it errored (ignores in-progress BUILDING)", () => { + const f = latestFailedVercel(parseVercelDeployments(body)); + assert.equal(f?.uid, "dpl_3"); + }); + it("returns null when the latest terminal deploy is READY", () => { + const ok = JSON.stringify({ deployments: [{ uid: "x", state: "READY", target: "production", created: 9 }] }); + assert.equal(latestFailedVercel(parseVercelDeployments(ok)), null); + }); + it("treats CANCELED as not-red", () => { + const c = JSON.stringify({ deployments: [{ uid: "x", state: "CANCELED", target: "production", created: 9 }] }); + assert.equal(latestFailedVercel(parseVercelDeployments(c)), null); + }); + it("supports readyState as the state field alias", () => { + const r = JSON.stringify({ deployments: [{ uid: "x", readyState: "ERROR", target: "production", created: 9 }] }); + assert.equal(latestFailedVercel(parseVercelDeployments(r))?.uid, "x"); + }); +}); + +describe("vercelSensor.probe", () => { + afterEach(() => { + delete process.env.VERCEL_TOKEN; + }); + + it("emits red when the latest production deploy errored", async () => { + process.env.VERCEL_TOKEN = "vt"; + const out = await vercelSensor.probe(linked, deps()); + assert.equal(out[0].status, "red"); + assert.equal(out[0].source, "prj_abc"); + assert.equal(out[0].suggestedClass, "code"); + }); + + it("is unknown when the project is not linked (no .vercel/project.json)", async () => { + process.env.VERCEL_TOKEN = "vt"; + const out = await vercelSensor.probe({ cwd: "/r", config: {} }, deps()); + assert.equal(out[0].status, "unknown"); + assert.match(out[0].title, /not linked/); + }); + + it("is unknown (not green) when VERCEL_TOKEN is missing", async () => { + const out = await vercelSensor.probe(linked, deps()); + assert.equal(out[0].status, "unknown"); + assert.match(out[0].title, /VERCEL_TOKEN/); + }); + + it("is unknown on a non-OK API response", async () => { + process.env.VERCEL_TOKEN = "vt"; + const out = await vercelSensor.probe(linked, deps({ http: { ok: false, status: 403, body: "" } })); + assert.equal(out[0].status, "unknown"); + assert.match(out[0].title, /403/); + }); +}); diff --git a/src/health-sensors/vercel.ts b/src/health-sensors/vercel.ts new file mode 100644 index 00000000..6d94af98 --- /dev/null +++ b/src/health-sensors/vercel.ts @@ -0,0 +1,87 @@ +import type { HealthFinding, HealthSensor } from "../health.js"; + +export interface VercelDeployment { + uid?: string; + state?: string; + readyState?: string; + created?: number; + target?: string | null; +} + +// Terminal Vercel deployment states; ERROR is the only "red" one (CANCELED = user-stopped). +const TERMINAL = new Set(["READY", "ERROR", "CANCELED"]); + +function stateOf(d: VercelDeployment): string | undefined { + return d.state ?? d.readyState; +} + +export function parseVercelDeployments(body: string): VercelDeployment[] { + try { + const obj = JSON.parse(body) as { deployments?: VercelDeployment[] }; + return Array.isArray(obj.deployments) ? obj.deployments : []; + } catch { + return []; + } +} + +/** Most recent terminal production deployment; returned only when it errored. */ +export function latestFailedVercel(deployments: VercelDeployment[]): VercelDeployment | null { + const terminal = deployments.filter((d) => { + const s = stateOf(d); + return s !== undefined && TERMINAL.has(s); + }); + if (terminal.length === 0) return null; + const latest = terminal.reduce((a, b) => ((a.created ?? 0) >= (b.created ?? 0) ? a : b)); + return stateOf(latest) === "ERROR" ? latest : null; +} + +export const vercelSensor: HealthSensor = { + id: "vercel", + async probe(ctx, deps): Promise { + const projectId = ctx.vercel?.projectId; + if (!projectId) { + return [{ + sensor: "vercel", + source: "(not linked)", + status: "unknown", + title: "Vercel probe skipped: project not linked", + detail: "no .vercel/project.json — run `vercel link`", + }]; + } + const token = process.env.VERCEL_TOKEN; + if (!token) { + return [{ + sensor: "vercel", + source: projectId, + status: "unknown", + title: "Vercel probe skipped: VERCEL_TOKEN not set", + detail: "set VERCEL_TOKEN to enable the Vercel sensor", + }]; + } + const team = ctx.vercel?.orgId ? `&teamId=${encodeURIComponent(ctx.vercel.orgId)}` : ""; + const url = `https://api.vercel.com/v6/deployments?projectId=${encodeURIComponent(projectId)}&target=production&limit=10${team}`; + const res = await deps.httpGet(url, { Authorization: `Bearer ${token}` }); + if (!res.ok) { + return [{ + sensor: "vercel", + source: projectId, + status: "unknown", + title: `Vercel API returned HTTP ${res.status}`, + detail: "check VERCEL_TOKEN scope / project + team access", + }]; + } + const failed = latestFailedVercel(parseVercelDeployments(res.body)); + if (!failed) { + return [{ sensor: "vercel", source: projectId, status: "green", title: "Vercel: latest production deploy green" }]; + } + return [{ + sensor: "vercel", + source: projectId, + status: "red", + severity: "high", + title: "Vercel production deploy failed", + detail: `deployment ${failed.uid ?? "?"} state=ERROR`, + suggestedClass: "code", + }]; + }, +}; diff --git a/src/health.ts b/src/health.ts index 51c9c3f8..da0b928d 100644 --- a/src/health.ts +++ b/src/health.ts @@ -3,6 +3,7 @@ import { execFileNoThrow, type ExecResult } from "./utils/execFileNoThrow.js"; import { githubActionsSensor } from "./health-sensors/github-actions.js"; import { gitlabSensor } from "./health-sensors/gitlab-ci.js"; import { bitbucketSensor } from "./health-sensors/bitbucket-pipelines.js"; +import { vercelSensor } from "./health-sensors/vercel.js"; export type HealthStatus = "green" | "red" | "unknown"; export type HealthClass = "code" | "human" | "noise"; @@ -27,6 +28,8 @@ export interface HealthCtx { gitlabCi?: boolean; /** True when a bitbucket-pipelines.yml is present (Bitbucket Pipelines in use). */ bitbucketPipelines?: boolean; + /** Vercel link from .vercel/project.json (orgId = teamId, projectId), if present. */ + vercel?: { orgId?: string; projectId?: string }; } export interface HttpResponse { @@ -72,7 +75,7 @@ export async function runHealth( return all.flat(); } -export const HEALTH_SENSORS: HealthSensor[] = [githubActionsSensor, gitlabSensor, bitbucketSensor]; +export const HEALTH_SENSORS: HealthSensor[] = [githubActionsSensor, gitlabSensor, bitbucketSensor, vercelSensor]; /** Returns the sensors whose underlying CI platform the project actually uses. */ export function selectSensors(ctx: HealthCtx): HealthSensor[] { @@ -84,6 +87,8 @@ export function selectSensors(ctx: HealthCtx): HealthSensor[] { return ctx.gitlabCi === true; case "bitbucket-pipelines": return ctx.bitbucketPipelines === true; + case "vercel": + return Boolean(ctx.vercel?.projectId); default: return false; }