diff --git a/action/index.mjs b/action/index.mjs index e39d376..6bde9dd 100644 --- a/action/index.mjs +++ b/action/index.mjs @@ -259132,6 +259132,15 @@ var package_default = { } }; +// src/version.ts +var VERSION_TAG = /^v?(\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?)$/; +function versionFromRef(ref) { + return ref ? VERSION_TAG.exec(ref.trim())?.[1] : void 0; +} +function wardenVersion(ref = process.env["GITHUB_ACTION_REF"], isAction = true) { + return (isAction ? versionFromRef(ref) : void 0) ?? package_default.version; +} + // src/config/load.ts var GovernanceConfigError = class extends Error { field; @@ -259963,7 +259972,7 @@ function driftStart(input) { var DRIFT_ENGINE = "warden"; var DRIFT_READ = "live-read"; function driftEvidence() { - return { tool: "github-warden", version: package_default.version, command: "reconcile --mode dry-run" }; + return { tool: "github-warden", version: wardenVersion(), command: "reconcile --mode dry-run" }; } var parentOf = (key) => key.split("/")[0]; function driftAddress(org, entry) { @@ -262859,7 +262868,7 @@ async function main(argv = process.argv.slice(2)) { process.exit(0); } if (subcommand === "--version" || subcommand === "-v") { - process.stdout.write(`${package_default.version} + process.stdout.write(`${wardenVersion()} `); process.exit(0); } diff --git a/chant.config.ts b/chant.config.ts index bde8682..681eb56 100644 --- a/chant.config.ts +++ b/chant.config.ts @@ -5,7 +5,7 @@ export default { legacyTags: ["v"], green: "forge-checks", version: { packages: ["."] }, - publish: [{ type: "npm", packages: ["."] }], + publish: [{ type: "npm", packages: ["."], stage: false }], workflow: { trigger: "dispatch", file: ".github/workflows/publish.yml" }, }, }; diff --git a/src/cli.ts b/src/cli.ts index 5fef29a..3d62dcc 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -57,7 +57,7 @@ import { existsSync, readFileSync, writeFileSync } from "node:fs"; import { pathToFileURL } from "node:url"; -import pkg from "../package.json" with { type: "json" }; +import { wardenVersion } from "./version.js"; import { GovernanceConfigError } from "./config/load.js"; import { parseConfigFile } from "./config/parse.js"; import { CONFIG_MODES, loadGovernancePolicy, type ConfigMode } from "./config/policy.js"; @@ -768,8 +768,8 @@ async function main(argv: string[] = process.argv.slice(2)) { } if (subcommand === "--version" || subcommand === "-v") { - // Inlined from package.json at build time — always matches the published version. - process.stdout.write(`${pkg.version}\n`); + // The package.json version, or the action ref when run as the action (src/version.ts). + process.stdout.write(`${wardenVersion()}\n`); process.exit(0); } diff --git a/src/drift/reader.ts b/src/drift/reader.ts index b5df57f..e974028 100644 --- a/src/drift/reader.ts +++ b/src/drift/reader.ts @@ -35,7 +35,7 @@ import { import type { CycleError, DeferredWork } from "@intentius/chant/reconcile"; import type { GovernanceConfig } from "../config/types.js"; import type { ChangeSet, ChangeSetEntry } from "../reconcile/diff.js"; -import pkg from "../../package.json" with { type: "json" }; +import { wardenVersion } from "../version.js"; /** The engine a warden's records name. */ export const DRIFT_ENGINE = "warden" as const; @@ -44,7 +44,7 @@ export const DRIFT_READ = "live-read" as const; /** What every record and the start line say produced them. */ export function driftEvidence(): DriftEvidence { - return { tool: "github-warden", version: pkg.version, command: "reconcile --mode dry-run" }; + return { tool: "github-warden", version: wardenVersion(), command: "reconcile --mode dry-run" }; } /** The first segment of a child entry's key, which names its parent (`core/alice` is team `core`'s, `api/main` repo `api`'s). */ diff --git a/src/version.test.ts b/src/version.test.ts new file mode 100644 index 0000000..12d08ec --- /dev/null +++ b/src/version.test.ts @@ -0,0 +1,32 @@ +import { describe, expect, it } from "vitest"; +import pkg from "../package.json" with { type: "json" }; +import { versionFromRef, wardenVersion } from "./version.js"; + +describe("versionFromRef", () => { + it("reads a version tag, with or without the v", () => { + expect(versionFromRef("v0.8.1")).toBe("0.8.1"); + expect(versionFromRef("0.8.1")).toBe("0.8.1"); + expect(versionFromRef("v1.0.0-rc.1")).toBe("1.0.0-rc.1"); + }); + + it("ignores refs that are not a full version", () => { + for (const ref of [undefined, "", "main", "v0", "v0.8", "feat/x", "7cd5abf0e0b8c4d7a1f3b2c9d8e7f6a5b4c3d2e1"]) { + expect(versionFromRef(ref)).toBeUndefined(); + } + }); +}); + +describe("wardenVersion", () => { + it("uses the action ref when run as the action", () => { + expect(wardenVersion("v9.9.9", true)).toBe("9.9.9"); + }); + + it("falls back to package.json when the ref is not a version tag", () => { + expect(wardenVersion("main", true)).toBe(pkg.version); + expect(wardenVersion(undefined, true)).toBe(pkg.version); + }); + + it("ignores the ref outside the action", () => { + expect(wardenVersion("v9.9.9", false)).toBe(pkg.version); + }); +}); diff --git a/src/version.ts b/src/version.ts new file mode 100644 index 0000000..5e14e0f --- /dev/null +++ b/src/version.ts @@ -0,0 +1,31 @@ +/** + * The version this build reports. + * + * The npm package carries the version chant release stamps into package.json + * before it builds, so there package.json is right. The committed action + * bundle (action/index.mjs) is built before any release commit exists, so its + * inlined package.json version is the previous release. When running as the + * action, the ref the workflow used (`uses: INTENTIUS/github-warden@v0.8.1`) + * names the release, so that wins when it is a version tag. Anything else (a + * branch, a commit SHA, a floating major tag) falls back to package.json. + */ +import pkg from "../package.json" with { type: "json" }; + +const VERSION_TAG = /^v?(\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?)$/; + +/** The version a ref names, or undefined when the ref is not a version tag. */ +export function versionFromRef(ref: string | undefined): string | undefined { + return ref ? VERSION_TAG.exec(ref.trim())?.[1] : undefined; +} + +/** + * The version to report: the action's ref when it is a version tag, else package.json's. + * GITHUB_WARDEN_IS_ACTION is replaced at action build time (build:action), so it must be + * read as a literal `process.env` access and not through a passed-in env. + */ +export function wardenVersion( + ref: string | undefined = process.env["GITHUB_ACTION_REF"], + isAction: boolean = !!process.env["GITHUB_WARDEN_IS_ACTION"], +): string { + return (isAction ? versionFromRef(ref) : undefined) ?? pkg.version; +}