Skip to content
Open
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
31 changes: 30 additions & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: Release v1 packages

on:
workflow_dispatch:
push:
branches: [maintenance/v1]
pull_request:
Expand Down Expand Up @@ -68,7 +69,7 @@ jobs:
run: node scripts/v1-release.mjs check >> "$GITHUB_OUTPUT"

publish:
name: Publish stable v1 packages to npm latest
name: Publish v1 packages to npm legacy
needs: validate
if: github.event_name == 'push' && github.ref == 'refs/heads/maintenance/v1' && needs.validate.outputs.pending == 'true' && vars.V1_PUBLISH_ENABLED == 'true'
runs-on: ubuntu-latest
Expand Down Expand Up @@ -100,3 +101,31 @@ jobs:
with:
publish: yarn publish-v1-packages
commitMode: github-api

legacy-tags:
name: Preserve v1 packages on npm legacy
if: github.event_name == 'workflow_dispatch' && github.ref == 'refs/heads/maintenance/v1' && vars.V1_PUBLISH_ENABLED == 'true'
runs-on: ubuntu-latest
environment: npm-v1-release
permissions:
contents: read
id-token: write
steps:
- uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6
with:
persist-credentials: false
- run: corepack enable
- uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6
with:
node-version-file: .nvmrc
registry-url: https://registry.npmjs.org
scope: "@reflag"
package-manager-cache: false
- run: node --test scripts/v1-release.test.mjs
- name: Require the current protected v1 commit
shell: bash
run: |
head=$(git ls-remote --exit-code origin refs/heads/maintenance/v1 | cut -f1)
test "$GITHUB_REF_PROTECTED" = true && test "$head" = "$GITHUB_SHA"
- name: Add legacy tags without changing latest
run: node scripts/v1-release.mjs tag-legacy

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Provide npm authentication for legacy dist-tag updates

When any legacy tag is missing, this step invokes npm dist-tag add, but the job supplies no npm credential. id-token: write enables trusted publishing for npm publish; it does not authenticate standalone dist-tag mutations, and setup-node only configures the registry/token reference rather than supplying a token. Consequently, the manual seeding job fails on its first required update instead of preserving the v1 tags. Supply an appropriately scoped npm write token through NODE_AUTH_TOKEN for this protected step, or use another supported authenticated mechanism for dist-tag updates.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"version-packages": "changeset version && YARN_ENABLE_IMMUTABLE_INSTALLS=false yarn install",
"version-v1-packages": "yarn version-packages && node scripts/v1-release.mjs plan",
"release-packages": "yarn build && yarn release",
"publish-v1-packages": "node scripts/v1-release.mjs check && yarn release --tag latest",
"publish-v1-packages": "node scripts/v1-release.mjs check && yarn release --tag legacy",
"release": "changeset publish",
"docs": "./docs.sh",
"lint:fix": "yarn oxlint --fix .",
Expand Down
67 changes: 56 additions & 11 deletions scripts/v1-release.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,40 @@ import { fileURLToPath } from "node:url";
const planPath = "releases/v1.json";
const npmRegistry = "https://registry.npmjs.org";
const stableVersion = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/;
const legacyPackages = [
"@reflag/browser-sdk",
"@reflag/cli",
"@reflag/node-sdk",
"@reflag/react-native-sdk",
"@reflag/react-sdk",
"@reflag/vue-sdk",
];

export async function legacyTagPlan(packages, { isPublished, tags }) {
const updates = [];
for (const name of legacyPackages) {
const pkg = packages.find((item) => item.name === name);
if (
!pkg ||
!stableVersion.test(pkg.version) ||
Number(pkg.version.split(".")[0]) > 1 ||
!(await isPublished(name, pkg.version))
) {
throw new Error(
`Expected a published pre-v2 workspace version for ${name}`,
);
}
const current = (await tags(name)).legacy;
if (current === pkg.version) continue;
if (current !== undefined) {
throw new Error(
`Refusing to replace existing npm legacy ${name}@${current} with ${pkg.version}`,
);
}
updates.push(`${name}@${pkg.version}`);
}
return updates;
}

export function extendReleasePlan(plan, packages, previousVersion) {
return Object.fromEntries(
Expand All @@ -19,11 +53,7 @@ export function extendReleasePlan(plan, packages, previousVersion) {
);
}

export async function checkReleasePlan(
plan,
packages,
{ isPublished, latest },
) {
export async function checkReleasePlan(plan, packages, { isPublished, tags }) {
const local = new Map(packages.map(({ name, version }) => [name, version]));
for (const [name, version] of Object.entries(plan)) {
if (
Expand All @@ -43,14 +73,15 @@ export async function checkReleasePlan(
if (!Object.hasOwn(plan, name)) {
throw new Error(`Unapproved unpublished version: ${name}@${version}`);
}
const current = await latest(name);
const currentTags = await tags(name);
const current = currentTags.legacy ?? currentTags.latest;
const stable = typeof current === "string" && stableVersion.exec(current);
const expected = stable
? `${stable[1]}.${stable[2]}.${BigInt(stable[3]) + 1n}`
: null;
if (version !== expected) {
throw new Error(
`Refusing ${name}@${version}: npm latest is ${current}; expected its next stable patch`,
`Refusing ${name}@${version}: npm legacy baseline is ${current}; expected its next stable patch`,
);
}
return `${name}@${version}`;
Expand Down Expand Up @@ -96,15 +127,15 @@ const registry = {
`Could not check ${name}@${version} (HTTP ${response.status})`,
);
},
async latest(name) {
async tags(name) {
const response = await registryGet(
`-/package/${encodeURIComponent(name)}/dist-tags`,
);
if (!response.ok)
throw new Error(
`Could not read tags for ${name} (HTTP ${response.status})`,
);
return (await response.json()).latest;
return response.json();
},
};

Expand Down Expand Up @@ -139,13 +170,27 @@ if (
const pending = await checkReleasePlan(plan, packages, registry);
console.error(
pending.length
? `Approved npm latest candidates:\n${pending.join("\n")}`
? `Approved npm legacy candidates:\n${pending.join("\n")}`
: "No unpublished v1 releases",
);
console.log(`pending=${pending.length > 0}`);
break;
}
case "tag-legacy": {
const updates = await legacyTagPlan(packages, registry);
for (const pkg of updates) {
execFileSync("npm", ["dist-tag", "add", pkg, "legacy"], {
stdio: "inherit",
});
}
if ((await legacyTagPlan(packages, registry)).length) {
throw new Error(
"npm legacy tags did not match the v1 workspace versions",
);
}
break;
}
default:
throw new Error("Expected plan or check");
throw new Error("Expected plan, check, or tag-legacy");
}
}
126 changes: 121 additions & 5 deletions scripts/v1-release.test.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { test } from "node:test";

import { checkReleasePlan, extendReleasePlan } from "./v1-release.mjs";
import {
checkReleasePlan,
extendReleasePlan,
legacyTagPlan,
} from "./v1-release.mjs";

const cli = {
name: "@reflag/cli",
Expand All @@ -16,7 +21,10 @@ const sdk = {
const plan = { [cli.name]: cli.version, [sdk.name]: sdk.version };
const registry = {
isPublished: async () => false,
latest: async (name) => (name === cli.name ? "1.0.4" : "1.8.3"),
tags: async (name) => ({
legacy: name === cli.name ? "1.0.4" : "1.8.3",
latest: "2.0.0",
}),
};

test("keeps the already versioned CLI and adds versions changed by Changesets", () => {
Expand Down Expand Up @@ -62,8 +70,8 @@ test("rejects a plan that differs from the checked-out packages or specifies a p
}
});

test("does not move latest backward or skip a patch", async () => {
for (const latest of [
test("does not move legacy backward or skip a patch", async () => {
for (const legacy of [
undefined,
"0.9.9",
"2.0.0",
Expand All @@ -74,9 +82,117 @@ test("does not move latest backward or skip a patch", async () => {
await assert.rejects(
checkReleasePlan({ [cli.name]: cli.version }, [cli], {
...registry,
latest: async () => latest,
tags: async () => ({ legacy, latest: "2.0.0" }),
}),
/expected its next stable patch/,
);
}
});

test("can start legacy publishing while latest still points at v1", async () => {
assert.deepEqual(
await checkReleasePlan(plan, [cli, sdk], {
...registry,
tags: async (name) => ({
latest: name === cli.name ? "1.0.4" : "1.8.3",
}),
}),
["@reflag/[email protected]", "@reflag/[email protected]"],
);
});

const legacyWorkspaces = [
{ name: "@reflag/browser-sdk", version: "1.7.4" },
cli,
sdk,
{ name: "@reflag/react-native-sdk", version: "0.3.7" },
{ name: "@reflag/react-sdk", version: "1.7.4" },
{ name: "@reflag/vue-sdk", version: "1.5.7" },
];
const tagRegistry = {
isPublished: async () => true,
tags: async () => ({ latest: "2.0.0" }),
};

test("seeds only the six coordinated legacy packages, including pre-1.0 Native", async () => {
assert.deepEqual(
await legacyTagPlan(
[
...legacyWorkspaces,
{ name: "@reflag/management-sdk", version: "0.1.0" },
],
tagRegistry,
),
legacyWorkspaces.map(({ name, version }) => `${name}@${version}`),
);
});

test("legacy tag seeding is idempotent and does not use latest as its target", async () => {
assert.deepEqual(
await legacyTagPlan(legacyWorkspaces, {
...tagRegistry,
tags: async (name) => ({
latest: "2.0.0",
legacy: legacyWorkspaces.find((pkg) => pkg.name === name).version,
}),
}),
[],
);
});

test("refuses missing, unpublished, prerelease, or v2 legacy tag targets", async () => {
for (const version of [undefined, "2.0.0", "1.8.4-next.0", "not-a-version"]) {
const packages = legacyWorkspaces.filter((pkg) => pkg.name !== sdk.name);
if (version !== undefined) packages.push({ ...sdk, version });
await assert.rejects(
legacyTagPlan(packages, tagRegistry),
/Expected a published pre-v2 workspace version/,
);
}
await assert.rejects(
legacyTagPlan(legacyWorkspaces, {
...tagRegistry,
isPublished: async (name) => name !== sdk.name,
}),
/Expected a published pre-v2 workspace version/,
);
});

test("never replaces an existing legacy tag during seeding", async () => {
for (const legacy of ["1.8.3", "1.8.5", "2.0.0"]) {
await assert.rejects(
legacyTagPlan(legacyWorkspaces, {
...tagRegistry,
tags: async (name) => ({
legacy: name === sdk.name ? legacy : undefined,
}),
}),
/Refusing to replace existing npm legacy/,
);
}
});

test("the maintenance publisher never selects latest", () => {
const pkg = JSON.parse(
readFileSync(new URL("../package.json", import.meta.url)),
);
assert.equal(
pkg.scripts["publish-v1-packages"],
"node scripts/v1-release.mjs check && yarn release --tag legacy",
);
const workflow = readFileSync(
new URL("../.github/workflows/publish.yml", import.meta.url),
"utf8",
);
const seedJob = workflow.split(" legacy-tags:\n")[1];
assert.match(seedJob, /github\.event_name == 'workflow_dispatch'/);
assert.match(seedJob, /github\.ref == 'refs\/heads\/maintenance\/v1'/);
assert.match(seedJob, /vars\.V1_PUBLISH_ENABLED == 'true'/);
assert.match(seedJob, /environment: npm-v1-release/);
assert.match(seedJob, /id-token: write/);
assert.match(
seedJob,
/test "\$GITHUB_REF_PROTECTED" = true && test "\$head" = "\$GITHUB_SHA"/,
);
assert.match(seedJob, /run: node scripts\/v1-release\.mjs tag-legacy/);
});
Loading