From 5fba9eb89980216c4b361e5fe35a04a2efc0cf97 Mon Sep 17 00:00:00 2001
From: Nicolas Schweitzer <nicolas.schweitzer@datadoghq.com>
Date: Tue, 21 Jan 2025 18:50:25 +0100
Subject: [PATCH] codereview: define a git-user slug instead of a true/false
 config

---
 README.md                  |  6 ++++++
 action.yml                 |  8 ++++----
 dist/index.js              |  3 +--
 src/git-source-provider.ts |  8 +++++---
 src/git-source-settings.ts |  4 ++--
 src/github-api-helper.ts   | 12 ++++++++++++
 src/input-helper.ts        |  5 ++---
 7 files changed, 32 insertions(+), 14 deletions(-)

diff --git a/README.md b/README.md
index 9b666c8..9683d93 100644
--- a/README.md
+++ b/README.md
@@ -40,6 +40,12 @@ Please refer to the [release page](https://github.com/actions/checkout/releases/
     #
     # Default: ${{ github.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
     # git config, which enables your scripts to run authenticated git commands. The
diff --git a/action.yml b/action.yml
index 2e37912..d1a2902 100644
--- a/action.yml
+++ b/action.yml
@@ -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)
     default: ${{ github.token }}
-  configure-user:
+  git-user:
     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.
-      Set to `false` to disable the config.
-    default: true
+      Set to '' to disable this configuration.
+    default: "github-action[bot]"
   ssh-key:
     description: >
       SSH key used to fetch the repository. The SSH key is configured with the local
diff --git a/dist/index.js b/dist/index.js
index 230e725..b5e352a 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -1814,8 +1814,7 @@ function getInputs() {
         // Auth token
         result.authToken = core.getInput('token', { required: true });
         // Configure user
-        result.configureUser = 
-            (core.getInput('configure-user') || 'true').toUpperCase() === 'TRUE'
+        result.gitUser = (core.getInput('git-user') || 'github-action[bot]')
         // SSH
         result.sshKey = core.getInput('ssh-key');
         result.sshKnownHosts = core.getInput('ssh-known-hosts');
diff --git a/src/git-source-provider.ts b/src/git-source-provider.ts
index dcdb9b2..bb4c5a5 100644
--- a/src/git-source-provider.ts
+++ b/src/git-source-provider.ts
@@ -274,12 +274,14 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
       settings.commit,
       settings.githubServerUrl
     )
-    if (settings.configureUser) {
+    if (settings.gitUser) {
       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)) {
-        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 {
diff --git a/src/git-source-settings.ts b/src/git-source-settings.ts
index e7ddb41..9cc2919 100644
--- a/src/git-source-settings.ts
+++ b/src/git-source-settings.ts
@@ -80,9 +80,9 @@ export interface IGitSourceSettings {
   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
diff --git a/src/github-api-helper.ts b/src/github-api-helper.ts
index 1ff27c2..b90b990 100644
--- a/src/github-api-helper.ts
+++ b/src/github-api-helper.ts
@@ -143,3 +143,15 @@ async function downloadArchive(
   })
   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
+}
diff --git a/src/input-helper.ts b/src/input-helper.ts
index b1a2b7a..343f4a5 100644
--- a/src/input-helper.ts
+++ b/src/input-helper.ts
@@ -138,9 +138,8 @@ export async function getInputs(): Promise<IGitSourceSettings> {
   // Auth token
   result.authToken = core.getInput('token', {required: true})
 
-  // Configure user
-  result.configureUser = 
-    (core.getInput('configure-user') || 'true').toUpperCase() === 'TRUE'
+  // Git user
+  result.gitUser = core.getInput('git-user') || 'github-action[bot]'
 
   // SSH
   result.sshKey = core.getInput('ssh-key')