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
5 changes: 5 additions & 0 deletions .changeset/warm-teeth-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@changesets/action": patch
---

Always switch and reset branch when generating version commits, similar to if `push-with-git-cli` is enabled
4 changes: 0 additions & 4 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,6 @@ export class GitHub {
}

async prepareBranch(branch: string) {
if (!this.pushWithGitCli) {
// Preparing a new local branch is not necessary when using the API
return;
}
await switchToMaybeExistingBranch(branch, { cwd: this.cwd });
await reset(context.sha, { cwd: this.cwd });
}
Expand Down
32 changes: 23 additions & 9 deletions src/run.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import path from "node:path";
import * as core from "@actions/core";
import * as github from "@actions/github";
import type { Changeset } from "@changesets/types";
import { writeChangeset } from "@changesets/write";
import { createFixture } from "fs-fixture";
import { exec } from "tinyexec";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { GitHub } from "./github.ts";
import { runPublish, runVersion } from "./run.ts";
import { gitdir } from "./test-utils.ts";

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

function createSimpleProjectFixture() {
return createFixture({
return gitdir({
node_modules: (api) => api.symlink(nodeModulesDir),
".changeset/config.json": JSON.stringify({}),
"packages/pkg-a/package.json": JSON.stringify({
Expand All @@ -69,7 +70,7 @@ function createSimpleProjectFixture() {
}

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

async function initializeGitRepository(cwd: string) {
await exec("git", ["init"], {
async function updateGithubContext(cwd: string) {
const head = await exec("git", ["rev-parse", "HEAD"], {
nodeOptions: { cwd },
throwOnError: true,
});
github.context.sha = head.stdout.trim();
}

function resetGithubContext() {
github.context.sha = "xeac7";
}

beforeEach(() => {
vi.clearAllMocks();
resetGithubContext();
});

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

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

it("throws when the built-in publish command does not create the output file", async () => {
await using fixture = await createFixture({
await using fixture = await gitdir({
"node_modules/@changesets/cli/package.json": JSON.stringify({
name: "@changesets/cli",
type: "module",
Expand All @@ -158,7 +164,7 @@ describe("publish", () => {
"package-lock.json": "",
});
const cwd = fixture.path;
await initializeGitRepository(cwd);
await updateGithubContext(cwd);
vi.stubEnv("RUNNER_TEMP", cwd);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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