Skip to content

Commit 219ea82

Browse files
authored
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
1 parent 469993c commit 219ea82

10 files changed

Lines changed: 52 additions & 26 deletions

File tree

.changeset/tidy-bots-smile.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+
Remove the `setup-git-user` input. Complete custom Git identities are now preserved automatically, while `github-actions[bot]` is configured as a fallback before creating local release commits or tags.

.github/workflows/publish.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ jobs:
7575
uses: ./version
7676
with:
7777
github-token: ${{ steps.bot-auth.outputs.token }}
78-
setup-git-user: false
7978
script: pnpm bump
8079

8180
publish:

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,18 @@ There are also sub-actions hosted in this repository. Check out their respective
1818
- version-script - The command to update version, edit CHANGELOG, read and delete changesets. Default to `changeset version` if not provided
1919
- commit-message - The commit message to use. Default to `Version Packages`
2020
- pr-title - The pull request title. Default to `Version Packages`
21-
- setup-git-user - Sets up the git user for commits as `"github-actions[bot]"`. Default to `true`
2221
- create-github-releases - A boolean value to indicate whether to create Github releases after `publish` or not. Default to `true`
2322
- push-git-tags - A boolean value to indicate whether to create git tags after `publish` or not. Default to `true`
2423
- commit-mode - Specifies the commit mode. Use `"git-cli"` to push changes using the Git CLI, or `"github-api"` to push changes via the GitHub API. When using `"github-api"`, all commits and tags are GPG-signed and attributed to the user or app who owns the `GITHUB_TOKEN`. Default to `git-cli`
2524
- cwd - Changes node's `process.cwd()` if the project is not located on the root. Default to `process.cwd()`
2625
- pr-draft - Controls draft PR behavior. Use `create` to create new version PRs as draft, or `always` to also convert existing version PRs back to draft when updating them. By default, version PRs are not forced into draft mode
2726
- github-token - Passes a custom GitHub token
2827

28+
Before creating local commits or annotated tags, the action preserves complete
29+
Git author and committer identities configured through the environment or Git
30+
configuration. If either identity is unavailable, it configures
31+
`github-actions[bot]` as a fallback.
32+
2933
### Outputs
3034

3135
- published - A boolean value to indicate whether a publishing has happened or not

action.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ inputs:
4646
or app who owns the GITHUB_TOKEN.
4747
required: false
4848
default: "git-cli"
49-
setup-git-user:
50-
description: Sets up the git user for commits as `"github-actions[bot]"`. Default to `true`
51-
required: false
52-
default: true
5349
outputs:
5450
published:
5551
description: A boolean value to indicate whether a publishing is happened or not

src/github.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,35 @@ export class GitHub {
9393
};
9494
}
9595

96-
async setupUser() {
97-
if (this.commitMode === "github-api") {
96+
async ensureGitUser() {
97+
// Check the exact identities that Git would use for commits without
98+
// allowing Git to fall back to auto-detected values like user@hostname.
99+
// This covers explicit GIT_AUTHOR_* / GIT_COMMITTER_* env vars, local
100+
// config, and global config. A partial identity, with only a name or only
101+
// an email, does not pass this check. If either identity is missing,
102+
// configure our default bot user as a fallback.
103+
const authorIdentity = await getExecOutput(
104+
"git",
105+
["-c", "user.useConfigOnly=true", "var", "GIT_AUTHOR_IDENT"],
106+
{
107+
cwd: this.cwd,
108+
ignoreReturnCode: true,
109+
silent: true,
110+
},
111+
);
112+
const committerIdentity = await getExecOutput(
113+
"git",
114+
["-c", "user.useConfigOnly=true", "var", "GIT_COMMITTER_IDENT"],
115+
{
116+
cwd: this.cwd,
117+
ignoreReturnCode: true,
118+
silent: true,
119+
},
120+
);
121+
if (authorIdentity.exitCode === 0 && committerIdentity.exitCode === 0) {
98122
return;
99123
}
124+
core.info("Setting Git user to github-actions[bot]");
100125
await exec("git", ["config", "user.name", `"github-actions[bot]"`], {
101126
cwd: this.cwd,
102127
});
@@ -159,6 +184,7 @@ export class GitHub {
159184
return;
160185
}
161186
if (!(await checkIfClean({ cwd: this.cwd }))) {
187+
await this.ensureGitUser();
162188
await commitAll(message, { cwd: this.cwd });
163189
}
164190
await push(branch, {

src/index.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import {
2323
prDraft: "pr-draft",
2424
createGithubReleases: "create-github-releases",
2525
commitMode: "commit-mode",
26-
setupGitUser: "setup-git-user",
2726
});
2827

2928
const githubToken = getRequiredInput("github-token");
@@ -51,13 +50,6 @@ import {
5150
commitMode,
5251
});
5352

54-
let setupGitUser = core.getBooleanInput("setup-git-user");
55-
56-
if (setupGitUser) {
57-
core.info("setting git user");
58-
await github.setupUser();
59-
}
60-
6153
let { changesets } = await readChangesetState(cwd);
6254

6355
let publishScript = core.getInput("publish-script");

src/run.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as core from "@actions/core";
33
import type { Changeset } from "@changesets/types";
44
import { writeChangeset } from "@changesets/write";
55
import { createFixture } from "fs-fixture";
6+
import { exec } from "tinyexec";
67
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
78
import { GitHub } from "./github.ts";
89
import { runPublish, runVersion } from "./run.ts";
@@ -101,6 +102,13 @@ const createGithub = (cwd: string) =>
101102
commitMode: "github-api",
102103
});
103104

105+
async function initializeGitRepository(cwd: string) {
106+
await exec("git", ["init"], {
107+
nodeOptions: { cwd },
108+
throwOnError: true,
109+
});
110+
}
111+
104112
beforeEach(() => {
105113
vi.clearAllMocks();
106114
});
@@ -113,6 +121,7 @@ describe("publish", () => {
113121
it("warns when a custom publish script does not create the output file", async () => {
114122
await using fixture = await createSimpleProjectFixture();
115123
const cwd = fixture.path;
124+
await initializeGitRepository(cwd);
116125
vi.stubEnv("RUNNER_TEMP", cwd);
117126

118127
const result = await runPublish({
@@ -145,6 +154,7 @@ describe("publish", () => {
145154
"package-lock.json": "",
146155
});
147156
const cwd = fixture.path;
157+
await initializeGitRepository(cwd);
148158
vi.stubEnv("RUNNER_TEMP", cwd);
149159

150160
await expect(

src/run.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ export async function runPublish({
164164
cwd,
165165
}: PublishOptions): Promise<PublishResult> {
166166
const { octokit } = github;
167+
// Changesets creates annotated tags locally, including when the action pushes those tags through the GitHub API.
168+
// It might also be important for custom publish scripts to have a valid git user configured.
169+
await github.ensureGitUser();
170+
167171
let changesetPublishOutput: ExecOutput;
168172
const outputFile = path.join(
169173
process.env.RUNNER_TEMP ?? (await fs.realpath(os.tmpdir())),

src/version/index.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ async function main() {
2525
const prDraft = getOptionalInput("pr-draft");
2626
const prBaseBranch = getOptionalInput("pr-base-branch");
2727
const commitMode = getOptionalInput("commit-mode") ?? "git-cli";
28-
const setupGitUser = core.getBooleanInput("setup-git-user");
2928

3029
// Validations
3130
if (prDraft !== undefined && prDraft !== "always" && prDraft !== "create") {
@@ -41,11 +40,6 @@ async function main() {
4140
commitMode,
4241
});
4342

44-
if (setupGitUser) {
45-
core.info("setting git user");
46-
await github.setupUser();
47-
}
48-
4943
const { pullRequestNumber } = await runVersion({
5044
script,
5145
github,

version/action.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ inputs:
3333
or app who owns the GITHUB_TOKEN.
3434
required: false
3535
default: "git-cli"
36-
setup-git-user:
37-
description: Sets up the git user for commits as `"github-actions[bot]"`. Default to `true`
38-
required: false
39-
default: true
4036
outputs:
4137
pr-number:
4238
description: The pull request number that was created or updated

0 commit comments

Comments
 (0)