codereview: define a git-user slug instead of a true/false config

This commit is contained in:
Nicolas Schweitzer 2025-01-21 18:50:25 +01:00
parent f3b199b7ed
commit 5fba9eb899
No known key found for this signature in database
7 changed files with 32 additions and 14 deletions

View file

@ -41,6 +41,12 @@ Please refer to the [release page](https://github.com/actions/checkout/releases/
# Default: ${{ github.token }} # Default: ${{ github.token }}
token: '' token: ''
# Github slug used to configure local user.name and user.email for git.
# This is required to push a commit from a Github Action Workflow
# Set to '' to disable this configuration
# Default: "github-action[bot]
git-config: ''
# SSH key used to fetch the repository. The SSH key is configured with the local # SSH key used to fetch the repository. The SSH key is configured with the local
# git config, which enables your scripts to run authenticated git commands. The # git config, which enables your scripts to run authenticated git commands. The
# post-job step removes the SSH key. # post-job step removes the SSH key.

View file

@ -22,12 +22,12 @@ inputs:
[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) [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)
default: ${{ github.token }} default: ${{ github.token }}
configure-user: git-user:
description: > description: >
Whether to configure user.name and user.email in the local git config. Github slug used to configure local user.name and user.email for git.
This is required to push a commit from a Github Action Workflow. This is required to push a commit from a Github Action Workflow.
Set to `false` to disable the config. Set to '' to disable this configuration.
default: true default: "github-action[bot]"
ssh-key: ssh-key:
description: > description: >
SSH key used to fetch the repository. The SSH key is configured with the local SSH key used to fetch the repository. The SSH key is configured with the local

3
dist/index.js vendored
View file

@ -1814,8 +1814,7 @@ function getInputs() {
// Auth token // Auth token
result.authToken = core.getInput('token', { required: true }); result.authToken = core.getInput('token', { required: true });
// Configure user // Configure user
result.configureUser = result.gitUser = (core.getInput('git-user') || 'github-action[bot]')
(core.getInput('configure-user') || 'true').toUpperCase() === 'TRUE'
// SSH // SSH
result.sshKey = core.getInput('ssh-key'); result.sshKey = core.getInput('ssh-key');
result.sshKnownHosts = core.getInput('ssh-known-hosts'); result.sshKnownHosts = core.getInput('ssh-known-hosts');

View file

@ -274,12 +274,14 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
settings.commit, settings.commit,
settings.githubServerUrl settings.githubServerUrl
) )
if (settings.configureUser) { if (settings.gitUser) {
if (!await git.configExists('user.name', true)) { if (!await git.configExists('user.name', true)) {
await git.config('user.name', 'github-action[bot]', true) await git.config('user.name', settings.gitUser, true)
} }
if (!await git.configExists('user.email', true)) { if (!await git.configExists('user.email', true)) {
await git.config('user.email', '41898282+github-actions[bot]@users.noreply.github.com', true)
const userId = await githubApiHelper.getUserId(settings.gitUser, settings.authToken, settings.githubServerUrl);
await git.config('user.email', `${userId}+${settings.gitUser}@users.noreply.github.com`, true)
} }
} }
} finally { } finally {

View file

@ -80,9 +80,9 @@ export interface IGitSourceSettings {
authToken: string authToken: string
/** /**
* Indicates whether to set a default user name and email in the local git config * A github user slug to set a default user name and email in the local git config
*/ */
configureUser: boolean gitUser: string
/** /**
* The SSH key to configure * The SSH key to configure

View file

@ -143,3 +143,15 @@ async function downloadArchive(
}) })
return Buffer.from(response.data as ArrayBuffer) // response.data is ArrayBuffer return Buffer.from(response.data as ArrayBuffer) // response.data is ArrayBuffer
} }
export async function getUserId(
username: string,
authToken: string,
baseUrl?: string
): Promise<number> {
const octokit = github.getOctokit(authToken, {
baseUrl: getServerApiUrl(baseUrl)
})
const user = await octokit.rest.users.getByUsername({username,});
return user.data.id
}

View file

@ -138,9 +138,8 @@ export async function getInputs(): Promise<IGitSourceSettings> {
// Auth token // Auth token
result.authToken = core.getInput('token', {required: true}) result.authToken = core.getInput('token', {required: true})
// Configure user // Git user
result.configureUser = result.gitUser = core.getInput('git-user') || 'github-action[bot]'
(core.getInput('configure-user') || 'true').toUpperCase() === 'TRUE'
// SSH // SSH
result.sshKey = core.getInput('ssh-key') result.sshKey = core.getInput('ssh-key')