From 022dd69046d0dad83d2eeeb3173386c4f9744669 Mon Sep 17 00:00:00 2001 From: Nicolas Schweitzer Date: Thu, 19 Dec 2024 15:30:48 +0100 Subject: [PATCH] feat(git config): Set default user.name and user.email in git config --- README.md | 30 +++++++++++++----------------- action.yml | 6 ++++++ src/git-source-provider.ts | 8 ++++++++ src/git-source-settings.ts | 5 +++++ src/input-helper.ts | 4 ++++ 5 files changed, 36 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index b0f6224f1..f118680de 100644 --- a/README.md +++ b/README.md @@ -131,19 +131,19 @@ Please refer to the [release page](https://github.com/actions/checkout/releases/ # Scenarios -- [Fetch only the root files](#Fetch-only-the-root-files) -- [Fetch only the root files and `.github` and `src` folder](#Fetch-only-the-root-files-and-github-and-src-folder) -- [Fetch only a single file](#Fetch-only-a-single-file) -- [Fetch all history for all tags and branches](#Fetch-all-history-for-all-tags-and-branches) -- [Checkout a different branch](#Checkout-a-different-branch) -- [Checkout HEAD^](#Checkout-HEAD) -- [Checkout multiple repos (side by side)](#Checkout-multiple-repos-side-by-side) -- [Checkout multiple repos (nested)](#Checkout-multiple-repos-nested) -- [Checkout multiple repos (private)](#Checkout-multiple-repos-private) -- [Checkout pull request HEAD commit instead of merge commit](#Checkout-pull-request-HEAD-commit-instead-of-merge-commit) -- [Checkout pull request on closed event](#Checkout-pull-request-on-closed-event) -- [Push a commit using the built-in token](#Push-a-commit-using-the-built-in-token) -- [Push a commit to a PR using the built-in token](#Push-a-commit-to-a-PR-using-the-built-in-token) +- [Fetch only the root files](#fetch-only-the-root-files) +- [Fetch only the root files and `.github` and `src` folder](#fetch-only-the-root-files-and-github-and-src-folder) +- [Fetch only a single file](#fetch-only-a-single-file) +- [Fetch all history for all tags and branches](#fetch-all-history-for-all-tags-and-branches) +- [Checkout a different branch](#checkout-a-different-branch) +- [Checkout HEAD^](#checkout-head) +- [Checkout multiple repos (side by side)](#checkout-multiple-repos-side-by-side) +- [Checkout multiple repos (nested)](#checkout-multiple-repos-nested) +- [Checkout multiple repos (private)](#checkout-multiple-repos-private) +- [Checkout pull request HEAD commit instead of merge commit](#checkout-pull-request-head-commit-instead-of-merge-commit) +- [Checkout pull request on closed event](#checkout-pull-request-on-closed-event) +- [Push a commit using the built-in token](#push-a-commit-using-the-built-in-token) +- [Push a commit to a PR using the built-in token](#push-a-commit-to-a-pr-using-the-built-in-token) ## Fetch only the root files @@ -281,8 +281,6 @@ jobs: - run: | date > generated.txt # Note: the following account information will not work on GHES - git config user.name "github-actions[bot]" - git config user.email "41898282+github-actions[bot]@users.noreply.github.com" git add . git commit -m "generated" git push @@ -305,8 +303,6 @@ jobs: - run: | date > generated.txt # Note: the following account information will not work on GHES - git config user.name "github-actions[bot]" - git config user.email "41898282+github-actions[bot]@users.noreply.github.com" git add . git commit -m "generated" git push diff --git a/action.yml b/action.yml index 6842eb843..2e379126f 100644 --- a/action.yml +++ b/action.yml @@ -22,6 +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) default: ${{ github.token }} + configure-user: + description: > + Whether to configure user.name and user.email in the local git config. + This is required to push a commit from a Github Action Workflow. + Set to `false` to disable the config. + default: true ssh-key: description: > SSH key used to fetch the repository. The SSH key is configured with the local diff --git a/src/git-source-provider.ts b/src/git-source-provider.ts index 2d3513897..dcdb9b278 100644 --- a/src/git-source-provider.ts +++ b/src/git-source-provider.ts @@ -274,6 +274,14 @@ export async function getSource(settings: IGitSourceSettings): Promise { settings.commit, settings.githubServerUrl ) + if (settings.configureUser) { + if (!await git.configExists('user.name', true)) { + await git.config('user.name', 'github-action[bot]', true) + } + if (!await git.configExists('user.email', true)) { + await git.config('user.email', '41898282+github-actions[bot]@users.noreply.github.com', true) + } + } } finally { // Remove auth if (authHelper) { diff --git a/src/git-source-settings.ts b/src/git-source-settings.ts index 4e41ac302..e7ddb41cd 100644 --- a/src/git-source-settings.ts +++ b/src/git-source-settings.ts @@ -79,6 +79,11 @@ export interface IGitSourceSettings { */ authToken: string + /** + * Indicates whether to set a default user name and email in the local git config + */ + configureUser: boolean + /** * The SSH key to configure */ diff --git a/src/input-helper.ts b/src/input-helper.ts index 059232f5c..b1a2b7a4b 100644 --- a/src/input-helper.ts +++ b/src/input-helper.ts @@ -138,6 +138,10 @@ export async function getInputs(): Promise { // Auth token result.authToken = core.getInput('token', {required: true}) + // Configure user + result.configureUser = + (core.getInput('configure-user') || 'true').toUpperCase() === 'TRUE' + // SSH result.sshKey = core.getInput('ssh-key') result.sshKnownHosts = core.getInput('ssh-known-hosts')