Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions action/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion chant.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
},
};
6 changes: 3 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions src/drift/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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). */
Expand Down
32 changes: 32 additions & 0 deletions src/version.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
31 changes: 31 additions & 0 deletions src/version.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Loading