Skip to content

Commit 039e5e0

Browse files
committed
Validate committed package versions when releasing tags
1 parent 4a26bb7 commit 039e5e0

4 files changed

Lines changed: 40 additions & 23 deletions

File tree

‎.github/workflows/release.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
cache: npm
3232
- name: Install dependencies
3333
run: npm ci
34-
- name: Set package version from tag
34+
- name: Verify package version matches tag
3535
id: version
3636
env:
3737
RELEASE_TAG: ${{ github.ref_name }}

‎README.md‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -288,17 +288,19 @@ copy it to another machine and follow the `.tgz` instructions above.
288288
- **Pull requests:** CI runs ESLint, type checking, unit tests, a build, and a
289289
package installation check on Node 22 and 24. Pushes to `main`/`master` also run CI.
290290
- **Releases:** push a SemVer tag to build and publish a GitHub Release with the
291-
`.tgz` and SHA-256 checksum. The tag controls the package version; there is no
292-
need to edit `package.json` first. Tests and package installation must pass.
291+
`.tgz` and SHA-256 checksum. CI verifies that the tag matches the committed
292+
version in `package.json` and `package-lock.json`. Tests and package installation must pass.
293293

294-
After the workflow files are committed and pushed, tag the commit you want to release:
294+
For a stable release, merge the PR first, then update your local `main` branch.
295+
With a clean working tree, run (replace `0.6.0` with your next unused version):
295296

296297
```bash
297-
git tag v0.5.0
298-
git push origin v0.5.0
298+
npm version 0.6.0
299+
git push --atomic origin HEAD v0.6.0
299300
```
300301

301-
Use your next unused version. Both `v0.5.0` and `0.5.0` are accepted; a suffix
302-
such as `v0.6.0-beta.1` creates a prerelease. Version changes happen only in CI,
303-
without a version commit or npm publication. Releases use the built-in
302+
`npm version` updates both manifests, creates a commit, and tags it automatically.
303+
The push sends the current branch and tag together. For testing, use a version
304+
such as `0.7.0-beta.1` on a feature branch; CI marks it as a prerelease.
305+
CI does not rewrite versions or publish to npm. Releases use the built-in
304306
`GITHUB_TOKEN`; no npm token or extra secret is needed.

‎scripts/release-version.mjs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { appendFile, readFile, writeFile } from "node:fs/promises";
1+
import { appendFile, readFile } from "node:fs/promises";
22
import semver from "semver";
33

44
try {
@@ -12,9 +12,9 @@ try {
1212
if (lock.name !== pkg.name || lock.packages?.[""]?.name !== pkg.name) {
1313
throw new Error("package.json and package-lock.json must describe the same root package.");
1414
}
15-
pkg.version = lock.version = lock.packages[""].version = version;
16-
await writeFile("package.json", JSON.stringify(pkg, null, 2) + "\n");
17-
await writeFile("package-lock.json", JSON.stringify(lock, null, 2) + "\n");
15+
if (pkg.version !== version || lock.version !== version || lock.packages[""].version !== version) {
16+
throw new Error(`Release tag ${tag} must match the version in package.json and both root versions in package-lock.json. Use npm version to commit the version and create its tag before pushing.`);
17+
}
1818
const prerelease = semver.prerelease(version) !== null;
1919
if (process.env.GITHUB_OUTPUT) {
2020
await appendFile(process.env.GITHUB_OUTPUT, `version=${version}\nprerelease=${prerelease}\n`);

‎test/release.test.ts‎

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import { fileURLToPath } from "node:url";
99

1010
const exec = promisify(execFile);
1111
const script = fileURLToPath(new URL("../scripts/release-version.mjs", import.meta.url));
12-
async function fixture() {
12+
async function fixture(version = "0.0.0") {
1313
const directory = await mkdtemp(join(tmpdir(), "oc2-release-"));
14-
const pkg = { name: "opencode2-automation", version: "0.0.0", private: true, scripts: { postinstall: "node scripts/postinstall.mjs" } };
14+
const pkg = { name: "opencode2-automation", version, private: true, scripts: { postinstall: "node scripts/postinstall.mjs" } };
1515
const lock = { name: pkg.name, version: pkg.version, lockfileVersion: 3, packages: {
1616
"": { name: pkg.name, version: pkg.version }, "node_modules/example": { version: "8.0.0", integrity: "unchanged" },
1717
} };
@@ -31,14 +31,12 @@ for (const [tag, version, prerelease] of [
3131
["v1.2.3+build.4", "1.2.3+build.4", false],
3232
["1.2.3-rc.2+build.4", "1.2.3-rc.2+build.4", true],
3333
] as const) {
34-
test(`release tag ${tag} versions both manifests and reports prerelease status`, async () => {
35-
const f = await fixture();
34+
test(`release tag ${tag} validates committed versions without changing manifests and reports prerelease status`, async () => {
35+
const f = await fixture(version);
3636
try {
3737
await f.run(tag);
38-
const pkg = JSON.parse(await readFile(join(f.directory, "package.json"), "utf8"));
39-
const lock = JSON.parse(await readFile(join(f.directory, "package-lock.json"), "utf8"));
40-
assert.deepEqual(pkg, { ...f.pkg, version });
41-
assert.deepEqual(lock, { ...f.lock, version, packages: { ...f.lock.packages, "": { ...f.lock.packages[""], version } } });
38+
assert.equal(await readFile(join(f.directory, "package.json"), "utf8"), JSON.stringify(f.pkg));
39+
assert.equal(await readFile(join(f.directory, "package-lock.json"), "utf8"), JSON.stringify(f.lock));
4240
assert.equal(await readFile(f.output, "utf8"), `version=${version}\nprerelease=${prerelease}\n`);
4341
} finally { await f.cleanup(); }
4442
});
@@ -56,11 +54,28 @@ test("invalid or missing release tags cannot change manifests or inject workflow
5654
} finally { await f.cleanup(); }
5755
});
5856

59-
test("a mismatched lockfile fails before either manifest is versioned", async () => {
60-
const f = await fixture();
57+
test("a mismatched lockfile fails without changing either manifest", async () => {
58+
const f = await fixture("1.2.3");
6159
try {
6260
await writeFile(join(f.directory, "package-lock.json"), JSON.stringify({ ...f.lock, name: "another-package" }));
6361
await assert.rejects(f.run("v1.2.3"), /same root package/);
6462
assert.equal(await readFile(join(f.directory, "package.json"), "utf8"), JSON.stringify(f.pkg));
6563
} finally { await f.cleanup(); }
6664
});
65+
66+
for (const field of ["package", "lock", "lock root"] as const) {
67+
test(`a mismatched ${field} version blocks the release without changing manifests or emitting outputs`, async () => {
68+
const f = await fixture("1.2.3");
69+
try {
70+
if (field === "package") f.pkg.version = "1.2.2";
71+
if (field === "lock") f.lock.version = "1.2.2";
72+
if (field === "lock root") f.lock.packages[""].version = "1.2.2";
73+
await writeFile(join(f.directory, "package.json"), JSON.stringify(f.pkg));
74+
await writeFile(join(f.directory, "package-lock.json"), JSON.stringify(f.lock));
75+
await assert.rejects(f.run("v1.2.3"), /Release tag v1\.2\.3 must match/);
76+
assert.equal(await readFile(join(f.directory, "package.json"), "utf8"), JSON.stringify(f.pkg));
77+
assert.equal(await readFile(join(f.directory, "package-lock.json"), "utf8"), JSON.stringify(f.lock));
78+
await assert.rejects(readFile(f.output), { code: "ENOENT" });
79+
} finally { await f.cleanup(); }
80+
});
81+
}

0 commit comments

Comments
 (0)