Remove setup-git-user in favor of setting fallback only when identity is missing (#688)

* Experiment with skipping setup-git-user more often

* drop setup-git-user

* fix tests
This commit is contained in:
Mateusz Burzyński
2026-07-27 05:02:39 +00:00
committed by GitHub
parent 469993ce57
commit 219ea82e52
10 changed files with 52 additions and 26 deletions
+28 -2
View File
@@ -93,10 +93,35 @@ export class GitHub {
};
}
async setupUser() {
if (this.commitMode === "github-api") {
async ensureGitUser() {
// Check the exact identities that Git would use for commits without
// allowing Git to fall back to auto-detected values like user@hostname.
// This covers explicit GIT_AUTHOR_* / GIT_COMMITTER_* env vars, local
// config, and global config. A partial identity, with only a name or only
// an email, does not pass this check. If either identity is missing,
// configure our default bot user as a fallback.
const authorIdentity = await getExecOutput(
"git",
["-c", "user.useConfigOnly=true", "var", "GIT_AUTHOR_IDENT"],
{
cwd: this.cwd,
ignoreReturnCode: true,
silent: true,
},
);
const committerIdentity = await getExecOutput(
"git",
["-c", "user.useConfigOnly=true", "var", "GIT_COMMITTER_IDENT"],
{
cwd: this.cwd,
ignoreReturnCode: true,
silent: true,
},
);
if (authorIdentity.exitCode === 0 && committerIdentity.exitCode === 0) {
return;
}
core.info("Setting Git user to github-actions[bot]");
await exec("git", ["config", "user.name", `"github-actions[bot]"`], {
cwd: this.cwd,
});
@@ -159,6 +184,7 @@ export class GitHub {
return;
}
if (!(await checkIfClean({ cwd: this.cwd }))) {
await this.ensureGitUser();
await commitAll(message, { cwd: this.cwd });
}
await push(branch, {
-8
View File
@@ -23,7 +23,6 @@ import {
prDraft: "pr-draft",
createGithubReleases: "create-github-releases",
commitMode: "commit-mode",
setupGitUser: "setup-git-user",
});
const githubToken = getRequiredInput("github-token");
@@ -51,13 +50,6 @@ import {
commitMode,
});
let setupGitUser = core.getBooleanInput("setup-git-user");
if (setupGitUser) {
core.info("setting git user");
await github.setupUser();
}
let { changesets } = await readChangesetState(cwd);
let publishScript = core.getInput("publish-script");
+10
View File
@@ -3,6 +3,7 @@ import * as core from "@actions/core";
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";
@@ -101,6 +102,13 @@ const createGithub = (cwd: string) =>
commitMode: "github-api",
});
async function initializeGitRepository(cwd: string) {
await exec("git", ["init"], {
nodeOptions: { cwd },
throwOnError: true,
});
}
beforeEach(() => {
vi.clearAllMocks();
});
@@ -113,6 +121,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);
vi.stubEnv("RUNNER_TEMP", cwd);
const result = await runPublish({
@@ -145,6 +154,7 @@ describe("publish", () => {
"package-lock.json": "",
});
const cwd = fixture.path;
await initializeGitRepository(cwd);
vi.stubEnv("RUNNER_TEMP", cwd);
await expect(
+4
View File
@@ -164,6 +164,10 @@ export async function runPublish({
cwd,
}: PublishOptions): Promise<PublishResult> {
const { octokit } = github;
// Changesets creates annotated tags locally, including when the action pushes those tags through the GitHub API.
// It might also be important for custom publish scripts to have a valid git user configured.
await github.ensureGitUser();
let changesetPublishOutput: ExecOutput;
const outputFile = path.join(
process.env.RUNNER_TEMP ?? (await fs.realpath(os.tmpdir())),
-6
View File
@@ -25,7 +25,6 @@ async function main() {
const prDraft = getOptionalInput("pr-draft");
const prBaseBranch = getOptionalInput("pr-base-branch");
const commitMode = getOptionalInput("commit-mode") ?? "git-cli";
const setupGitUser = core.getBooleanInput("setup-git-user");
// Validations
if (prDraft !== undefined && prDraft !== "always" && prDraft !== "create") {
@@ -41,11 +40,6 @@ async function main() {
commitMode,
});
if (setupGitUser) {
core.info("setting git user");
await github.setupUser();
}
const { pullRequestNumber } = await runVersion({
script,
github,