Skip to content

Commit ca85897

Browse files
authored
Always prepare branch for version (#729)
* Always prepare branch for version * Update test
1 parent 36f529f commit ca85897

3 files changed

Lines changed: 28 additions & 13 deletions

File tree

.changeset/warm-teeth-listen.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+
Always switch and reset branch when generating version commits, similar to if `push-with-git-cli` is enabled

src/github.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,6 @@ export class GitHub {
235235
}
236236

237237
async prepareBranch(branch: string) {
238-
if (!this.pushWithGitCli) {
239-
// Preparing a new local branch is not necessary when using the API
240-
return;
241-
}
242238
await switchToMaybeExistingBranch(branch, { cwd: this.cwd });
243239
await reset(context.sha, { cwd: this.cwd });
244240
}

src/run.test.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import path from "node:path";
22
import * as core from "@actions/core";
3+
import * as github from "@actions/github";
34
import type { Changeset } from "@changesets/types";
45
import { writeChangeset } from "@changesets/write";
5-
import { createFixture } from "fs-fixture";
66
import { exec } from "tinyexec";
77
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
88
import { GitHub } from "./github.ts";
99
import { runPublish, runVersion } from "./run.ts";
10+
import { gitdir } from "./test-utils.ts";
1011

1112
vi.mock("@actions/github", () => ({
1213
context: {
@@ -44,7 +45,7 @@ let mockedGraphql = vi.fn();
4445
const nodeModulesDir = path.join(import.meta.dirname, "..", "node_modules");
4546

4647
function createSimpleProjectFixture() {
47-
return createFixture({
48+
return gitdir({
4849
node_modules: (api) => api.symlink(nodeModulesDir),
4950
".changeset/config.json": JSON.stringify({}),
5051
"packages/pkg-a/package.json": JSON.stringify({
@@ -69,7 +70,7 @@ function createSimpleProjectFixture() {
6970
}
7071

7172
function createIgnoredPackageFixture() {
72-
return createFixture({
73+
return gitdir({
7374
node_modules: (api) => api.symlink(nodeModulesDir),
7475
".changeset/config.json": JSON.stringify({
7576
ignore: ["changesets-dev-ignored-package-pkg-a"],
@@ -106,15 +107,20 @@ const createGithub = (cwd: string) =>
106107
pushWithGitCli: false,
107108
});
108109

109-
async function initializeGitRepository(cwd: string) {
110-
await exec("git", ["init"], {
110+
async function updateGithubContext(cwd: string) {
111+
const head = await exec("git", ["rev-parse", "HEAD"], {
111112
nodeOptions: { cwd },
112-
throwOnError: true,
113113
});
114+
github.context.sha = head.stdout.trim();
115+
}
116+
117+
function resetGithubContext() {
118+
github.context.sha = "xeac7";
114119
}
115120

116121
beforeEach(() => {
117122
vi.clearAllMocks();
123+
resetGithubContext();
118124
});
119125

120126
afterEach(() => {
@@ -125,7 +131,7 @@ describe("publish", () => {
125131
it("warns when a custom publish script does not create the output file", async () => {
126132
await using fixture = await createSimpleProjectFixture();
127133
const cwd = fixture.path;
128-
await initializeGitRepository(cwd);
134+
await updateGithubContext(cwd);
129135
vi.stubEnv("RUNNER_TEMP", cwd);
130136

131137
const result = await runPublish({
@@ -145,7 +151,7 @@ describe("publish", () => {
145151
});
146152

147153
it("throws when the built-in publish command does not create the output file", async () => {
148-
await using fixture = await createFixture({
154+
await using fixture = await gitdir({
149155
"node_modules/@changesets/cli/package.json": JSON.stringify({
150156
name: "@changesets/cli",
151157
type: "module",
@@ -158,7 +164,7 @@ describe("publish", () => {
158164
"package-lock.json": "",
159165
});
160166
const cwd = fixture.path;
161-
await initializeGitRepository(cwd);
167+
await updateGithubContext(cwd);
162168
vi.stubEnv("RUNNER_TEMP", cwd);
163169

164170
await expect(
@@ -176,6 +182,7 @@ describe("version", () => {
176182
it("creates simple PR", async () => {
177183
await using fixture = await createSimpleProjectFixture();
178184
const cwd = fixture.path;
185+
await updateGithubContext(cwd);
179186

180187
mockedGithubMethods.pulls.list.mockImplementationOnce(() => ({ data: [] }));
181188

@@ -213,6 +220,7 @@ describe("version", () => {
213220
it('creates a draft PR when prDraft is "create"', async () => {
214221
await using fixture = await createSimpleProjectFixture();
215222
const cwd = fixture.path;
223+
await updateGithubContext(cwd);
216224

217225
mockedGithubMethods.pulls.list.mockImplementationOnce(() => ({ data: [] }));
218226

@@ -247,6 +255,7 @@ describe("version", () => {
247255
it("only includes bumped packages in the PR body", async () => {
248256
await using fixture = await createSimpleProjectFixture();
249257
const cwd = fixture.path;
258+
await updateGithubContext(cwd);
250259

251260
mockedGithubMethods.pulls.list.mockImplementationOnce(() => ({ data: [] }));
252261

@@ -280,6 +289,7 @@ describe("version", () => {
280289
it("doesn't include ignored package that got a dependency update in the PR body", async () => {
281290
await using fixture = await createIgnoredPackageFixture();
282291
const cwd = fixture.path;
292+
await updateGithubContext(cwd);
283293

284294
mockedGithubMethods.pulls.list.mockImplementationOnce(() => ({ data: [] }));
285295

@@ -313,6 +323,7 @@ describe("version", () => {
313323
it("does not include changelog entries if full message exceeds size limit", async () => {
314324
await using fixture = await createSimpleProjectFixture();
315325
const cwd = fixture.path;
326+
await updateGithubContext(cwd);
316327

317328
mockedGithubMethods.pulls.list.mockImplementationOnce(() => ({ data: [] }));
318329

@@ -370,6 +381,7 @@ fluminis divesque vulnere aquis parce lapsis rabie si visa fulmineis.
370381
it("does not include any release information if a message with simplified release info exceeds size limit", async () => {
371382
await using fixture = await createSimpleProjectFixture();
372383
const cwd = fixture.path;
384+
await updateGithubContext(cwd);
373385

374386
mockedGithubMethods.pulls.list.mockImplementationOnce(() => ({ data: [] }));
375387

@@ -427,6 +439,7 @@ fluminis divesque vulnere aquis parce lapsis rabie si visa fulmineis.
427439
it('updates an existing PR via GraphQL without converting it to draft when prDraft is "create"', async () => {
428440
await using fixture = await createSimpleProjectFixture();
429441
const cwd = fixture.path;
442+
await updateGithubContext(cwd);
430443

431444
mockedGithubMethods.pulls.list.mockImplementationOnce(() => ({
432445
data: [{ number: 123, node_id: "PR_kwDOA" }],
@@ -459,6 +472,7 @@ fluminis divesque vulnere aquis parce lapsis rabie si visa fulmineis.
459472
it('updates an existing PR via GraphQL and converts it to draft when prDraft is "always"', async () => {
460473
await using fixture = await createSimpleProjectFixture();
461474
const cwd = fixture.path;
475+
await updateGithubContext(cwd);
462476

463477
mockedGithubMethods.pulls.list.mockImplementationOnce(() => ({
464478
data: [{ number: 123, node_id: "PR_kwDOA" }],

0 commit comments

Comments
 (0)