Skip to content

Commit 5b307d3

Browse files
authored
Validate that projects use Changesets CLI v3 and direct Changesets CLI v2 users to changesets/action@v1 (#699)
* Validate that projects use Changesets CLI v3 and direct Changesets CLI v2 users to `changesets/action@v1` * better spy/mocking * fmt * shuffle
1 parent 84d78c6 commit 5b307d3

9 files changed

Lines changed: 143 additions & 23 deletions

File tree

‎.changeset/bright-lions-check.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@changesets/action": patch
3+
---
4+
5+
Validate that projects use Changesets CLI v3 and direct Changesets CLI v2 users to `changesets/action@v1`.

‎src/index.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@ import {
88
getOptionalInput,
99
getRequiredInput,
1010
throwOnRenamedInputs,
11+
validateChangesetsCliVersion,
1112
} from "./utils.ts";
1213

1314
(async () => {
15+
// If the user needs to change the cwd, set `working-directory` in the step instead
16+
const cwd = process.cwd();
17+
await validateChangesetsCliVersion(cwd);
18+
1419
throwOnRenamedInputs({
1520
publish: "publish-script",
1621
version: "version-script",
@@ -32,9 +37,6 @@ import {
3237
);
3338
}
3439

35-
// If the user needs to change the cwd, set `working-directory` in the step instead
36-
const cwd = process.cwd();
37-
3840
const commitMode = getOptionalInput("commit-mode") ?? "git-cli";
3941
const prDraft = getOptionalInput("pr-draft");
4042
if (commitMode !== "git-cli" && commitMode !== "github-api") {

‎src/pack/index.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
downloadArtifact,
88
execChangesetsCli,
99
getOptionalInput,
10+
validateChangesetsCliVersion,
1011
} from "../utils.ts";
1112

1213
try {
@@ -16,10 +17,12 @@ try {
1617
}
1718

1819
async function main() {
19-
const publishPlanArtifactId = getOptionalInput("publish-plan-artifact-id");
20-
2120
// If the user needs to change the cwd, set `working-directory` in the step instead
2221
const cwd = process.cwd();
22+
await validateChangesetsCliVersion(cwd);
23+
24+
const publishPlanArtifactId = getOptionalInput("publish-plan-artifact-id");
25+
2326
const tmpDir = process.env.RUNNER_TEMP ?? (await fs.realpath(os.tmpdir()));
2427
const outDir = path.join(tmpDir, `changeset-pack-${Date.now()}`);
2528

‎src/publish/index.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
downloadArtifact,
88
getOptionalInput,
99
getRequiredInput,
10+
validateChangesetsCliVersion,
1011
} from "../utils.ts";
1112

1213
try {
@@ -16,6 +17,10 @@ try {
1617
}
1718

1819
async function main() {
20+
// If the user needs to change the cwd, set `working-directory` in the step instead
21+
const cwd = process.cwd();
22+
await validateChangesetsCliVersion(cwd);
23+
1924
const githubToken = getRequiredInput("github-token");
2025
const script = getOptionalInput("script");
2126
const packDirArtifactId = getOptionalInput("pack-dir-artifact-id");
@@ -30,9 +35,6 @@ async function main() {
3035
);
3136
}
3237

33-
// If the user needs to change the cwd, set `working-directory` in the step instead
34-
const cwd = process.cwd();
35-
3638
// NOTE: Always use API mode here as publish does not need a commit-mode.
3739
const github = new GitHub({ cwd, githubToken, commitMode: "github-api" });
3840

‎src/run.test.ts‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ vi.mock("@actions/github", () => ({
2121
graphql: mockedGraphql,
2222
}),
2323
}));
24-
vi.mock("@actions/core", async (importOriginal) => ({
25-
...(await importOriginal<typeof import("@actions/core")>()),
26-
warning: vi.fn(),
27-
}));
24+
vi.mock("@actions/core", { spy: true });
25+
const mockedWarning = vi.mocked(core.warning);
2826
vi.mock("@changesets/ghcommit");
2927

3028
let mockedGithubMethods = {
@@ -126,7 +124,7 @@ describe("publish", () => {
126124
});
127125

128126
expect(result).toEqual({ published: false, exitCode: 0 });
129-
expect(core.warning).toHaveBeenCalledWith(
127+
expect(mockedWarning).toHaveBeenCalledWith(
130128
expect.stringContaining(
131129
"GitHub releases and git tags cannot be created without this output",
132130
),

‎src/select-mode/index.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import path from "node:path";
44
import artifact from "@actions/artifact";
55
import * as core from "@actions/core";
66
import readChangesetState from "../readChangesetState.ts";
7-
import { execChangesetsCli } from "../utils.ts";
7+
import { execChangesetsCli, validateChangesetsCliVersion } from "../utils.ts";
88

99
type ModeResult =
1010
| {
@@ -27,7 +27,10 @@ try {
2727
}
2828

2929
async function main() {
30-
const result = await getMode();
30+
const cwd = process.cwd();
31+
await validateChangesetsCliVersion(cwd);
32+
33+
const result = await getMode(cwd);
3134
core.setOutput("mode", result.mode);
3235
if (result.mode === "publish") {
3336
const publishPlanArtifact = await artifact.uploadArtifact(
@@ -48,8 +51,8 @@ async function main() {
4851
}
4952
}
5053

51-
async function getMode(): Promise<ModeResult> {
52-
const { changesets } = await readChangesetState();
54+
async function getMode(cwd: string): Promise<ModeResult> {
55+
const { changesets } = await readChangesetState(cwd);
5356

5457
if (changesets.length > 0) {
5558
const hasNonEmptyChangesets = changesets.some(
@@ -61,7 +64,6 @@ async function getMode(): Promise<ModeResult> {
6164
return { mode: "none" };
6265
}
6366

64-
const cwd = process.cwd();
6567
const publishPlanPath = path.join(
6668
process.env.RUNNER_TEMP ?? (await fs.realpath(os.tmpdir())),
6769
`changeset-publish-plan-${Date.now()}`,

‎src/utils.test.ts‎

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
import { createFixture } from "fs-fixture";
12
import { expect, test } from "vitest";
2-
import { BumpLevels, getChangelogEntry, sortTheThings } from "./utils.ts";
3+
import {
4+
BumpLevels,
5+
getChangelogEntry,
6+
sortTheThings,
7+
validateChangesetsCliVersion,
8+
} from "./utils.ts";
39

410
let changelog = `# @keystone-alpha/email
511
@@ -100,3 +106,49 @@ test("it sorts the things right", () => {
100106
];
101107
expect(things.sort(sortTheThings)).toMatchSnapshot();
102108
});
109+
110+
test("throws when the project declares Changesets CLI v2", async () => {
111+
await using fixture = await createFixture({
112+
"package.json": JSON.stringify({
113+
name: "project",
114+
devDependencies: { "@changesets/cli": "^2.29.7" },
115+
}),
116+
"package-lock.json": "",
117+
});
118+
119+
await expect(validateChangesetsCliVersion(fixture.path)).rejects.toThrow(
120+
"This version of the Changesets action is designed to work with Changesets CLI v3. Changesets CLI v2 is not supported; use Changesets action v1 instead, which is compatible with CLI v2.",
121+
);
122+
});
123+
124+
test("accepts a Changesets CLI v3 contract without an installed CLI", async () => {
125+
await using fixture = await createFixture({
126+
"package.json": JSON.stringify({
127+
name: "project",
128+
devDependencies: { "@changesets/cli": "^3.0.0" },
129+
}),
130+
"package-lock.json": "",
131+
});
132+
133+
await expect(
134+
validateChangesetsCliVersion(fixture.path),
135+
).resolves.toBeUndefined();
136+
});
137+
138+
test("throws when the project has Changesets CLI v2 installed", async () => {
139+
await using fixture = await createFixture({
140+
"package.json": JSON.stringify({
141+
name: "project",
142+
devDependencies: { "@changesets/cli": "^3.0.0" },
143+
}),
144+
"package-lock.json": "",
145+
"node_modules/@changesets/cli/package.json": JSON.stringify({
146+
name: "@changesets/cli",
147+
version: "2.29.7",
148+
}),
149+
});
150+
151+
await expect(validateChangesetsCliVersion(fixture.path)).rejects.toThrow(
152+
"This version of the Changesets action is designed to work with Changesets CLI v3. Changesets CLI v2 is not supported; use Changesets action v1 instead, which is compatible with CLI v2.",
153+
);
154+
});

‎src/utils.ts‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {
1010
type ExecOutput,
1111
} from "@actions/exec";
1212
import { getPackages, type Package } from "@manypkg/get-packages";
13+
import major from "semver/functions/major.js";
14+
import subset from "semver/ranges/subset.js";
1315

1416
const require = createRequire(import.meta.url);
1517

@@ -154,6 +156,55 @@ export function throwOnRenamedInputs(renames: Record<string, string>) {
154156
}
155157
}
156158

159+
const changesetsCliCompatibilityError =
160+
"This version of the Changesets action is designed to work with Changesets CLI v3. " +
161+
"Changesets CLI v2 is not supported; use Changesets action v1 instead, which is compatible with CLI v2.";
162+
163+
export async function validateChangesetsCliVersion(cwd: string) {
164+
const { rootPackage } = await getPackages(cwd);
165+
const packageJson = rootPackage?.packageJson;
166+
const declaredVersion =
167+
packageJson?.devDependencies?.["@changesets/cli"] ??
168+
packageJson?.dependencies?.["@changesets/cli"];
169+
170+
if (typeof declaredVersion === "string") {
171+
const range = declaredVersion.startsWith("workspace:")
172+
? declaredVersion.slice("workspace:".length)
173+
: declaredVersion;
174+
175+
let isV2 = false;
176+
177+
try {
178+
isV2 = subset(range, ">=2.0.0-0 <3.0.0-0", {
179+
includePrerelease: true,
180+
});
181+
} catch {
182+
// it could be a non-semver protocol
183+
}
184+
185+
if (isV2) {
186+
throw new Error(changesetsCliCompatibilityError);
187+
}
188+
}
189+
190+
let cliPackageJson;
191+
192+
try {
193+
cliPackageJson = require(
194+
require.resolve("@changesets/cli/package.json", { paths: [cwd] }),
195+
);
196+
} catch {
197+
return;
198+
}
199+
200+
if (
201+
typeof cliPackageJson.version === "string" &&
202+
major(cliPackageJson.version) === 2
203+
) {
204+
throw new Error(changesetsCliCompatibilityError);
205+
}
206+
}
207+
157208
function resolveChangesetsCli(cwd: string) {
158209
return require.resolve("@changesets/cli/bin.js", {
159210
paths: [cwd],

‎src/version/index.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import * as core from "@actions/core";
22
import { GitHub } from "../github.ts";
33
import { runVersion } from "../run.ts";
4-
import { getOptionalInput, getRequiredInput } from "../utils.ts";
4+
import {
5+
getOptionalInput,
6+
getRequiredInput,
7+
validateChangesetsCliVersion,
8+
} from "../utils.ts";
59

610
try {
711
await main();
@@ -10,6 +14,10 @@ try {
1014
}
1115

1216
async function main() {
17+
// If the user needs to change the cwd, set `working-directory` in the step instead
18+
const cwd = process.cwd();
19+
await validateChangesetsCliVersion(cwd);
20+
1321
const githubToken = getRequiredInput("github-token");
1422
const script = getOptionalInput("script");
1523
const commitMessage = getRequiredInput("commit-message");
@@ -27,9 +35,6 @@ async function main() {
2735
throw new Error(`Invalid commit-mode input: ${commitMode}`);
2836
}
2937

30-
// If the user needs to change the cwd, set `working-directory` in the step instead
31-
const cwd = process.cwd();
32-
3338
const github = new GitHub({
3439
cwd,
3540
githubToken,

0 commit comments

Comments
 (0)