Skip to content

Commit 5fba9eb

Browse files
committed
codereview: define a git-user slug instead of a true/false config
1 parent f3b199b commit 5fba9eb

7 files changed

+32
-14
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ Please refer to the [release page](https://github.com/actions/checkout/releases/
4040
#
4141
# Default: ${{ github.token }}
4242
token: ''
43+
44+
# Github slug used to configure local user.name and user.email for git.
45+
# This is required to push a commit from a Github Action Workflow
46+
# Set to '' to disable this configuration
47+
# Default: "github-action[bot]
48+
git-config: ''
4349

4450
# SSH key used to fetch the repository. The SSH key is configured with the local
4551
# git config, which enables your scripts to run authenticated git commands. The

action.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ inputs:
2222
2323
[Learn more about creating and using encrypted secrets](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/creating-and-using-encrypted-secrets)
2424
default: ${{ github.token }}
25-
configure-user:
25+
git-user:
2626
description: >
27-
Whether to configure user.name and user.email in the local git config.
27+
Github slug used to configure local user.name and user.email for git.
2828
This is required to push a commit from a Github Action Workflow.
29-
Set to `false` to disable the config.
30-
default: true
29+
Set to '' to disable this configuration.
30+
default: "github-action[bot]"
3131
ssh-key:
3232
description: >
3333
SSH key used to fetch the repository. The SSH key is configured with the local

dist/index.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1814,8 +1814,7 @@ function getInputs() {
18141814
// Auth token
18151815
result.authToken = core.getInput('token', { required: true });
18161816
// Configure user
1817-
result.configureUser =
1818-
(core.getInput('configure-user') || 'true').toUpperCase() === 'TRUE'
1817+
result.gitUser = (core.getInput('git-user') || 'github-action[bot]')
18191818
// SSH
18201819
result.sshKey = core.getInput('ssh-key');
18211820
result.sshKnownHosts = core.getInput('ssh-known-hosts');

src/git-source-provider.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -274,12 +274,14 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
274274
settings.commit,
275275
settings.githubServerUrl
276276
)
277-
if (settings.configureUser) {
277+
if (settings.gitUser) {
278278
if (!await git.configExists('user.name', true)) {
279-
await git.config('user.name', 'github-action[bot]', true)
279+
await git.config('user.name', settings.gitUser, true)
280280
}
281281
if (!await git.configExists('user.email', true)) {
282-
await git.config('user.email', '41898282+github-actions[bot]@users.noreply.github.com', true)
282+
283+
const userId = await githubApiHelper.getUserId(settings.gitUser, settings.authToken, settings.githubServerUrl);
284+
await git.config('user.email', `${userId}+${settings.gitUser}@users.noreply.github.com`, true)
283285
}
284286
}
285287
} finally {

src/git-source-settings.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ export interface IGitSourceSettings {
8080
authToken: string
8181

8282
/**
83-
* Indicates whether to set a default user name and email in the local git config
83+
* A github user slug to set a default user name and email in the local git config
8484
*/
85-
configureUser: boolean
85+
gitUser: string
8686

8787
/**
8888
* The SSH key to configure

src/github-api-helper.ts

+12
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,15 @@ async function downloadArchive(
143143
})
144144
return Buffer.from(response.data as ArrayBuffer) // response.data is ArrayBuffer
145145
}
146+
147+
export async function getUserId(
148+
username: string,
149+
authToken: string,
150+
baseUrl?: string
151+
): Promise<number> {
152+
const octokit = github.getOctokit(authToken, {
153+
baseUrl: getServerApiUrl(baseUrl)
154+
})
155+
const user = await octokit.rest.users.getByUsername({username,});
156+
return user.data.id
157+
}

src/input-helper.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,8 @@ export async function getInputs(): Promise<IGitSourceSettings> {
138138
// Auth token
139139
result.authToken = core.getInput('token', {required: true})
140140

141-
// Configure user
142-
result.configureUser =
143-
(core.getInput('configure-user') || 'true').toUpperCase() === 'TRUE'
141+
// Git user
142+
result.gitUser = core.getInput('git-user') || 'github-action[bot]'
144143

145144
// SSH
146145
result.sshKey = core.getInput('ssh-key')

0 commit comments

Comments
 (0)