From dc3f9aa6d62f401ccc2999a1f959169ebe47d4c4 Mon Sep 17 00:00:00 2001
From: Milad Irannejad <moorara@users.noreply.github.com>
Date: Wed, 1 Mar 2023 17:36:47 -0500
Subject: [PATCH] Change git config scope from global to system

---
 __test__/git-auth-helper.test.ts | 13 +++++++------
 src/git-auth-helper.ts           | 17 +++++++++--------
 src/git-command-manager.ts       | 24 +++++++++++++++---------
 src/git-source-provider.ts       |  5 +++--
 4 files changed, 34 insertions(+), 25 deletions(-)

diff --git a/__test__/git-auth-helper.test.ts b/__test__/git-auth-helper.test.ts
index 2acec38..6ea5744 100644
--- a/__test__/git-auth-helper.test.ts
+++ b/__test__/git-auth-helper.test.ts
@@ -5,6 +5,7 @@ import * as io from '@actions/io'
 import * as os from 'os'
 import * as path from 'path'
 import * as stateHelper from '../lib/state-helper'
+import {ConfigScope} from '../lib/git-command-manager'
 import {IGitCommandManager} from '../lib/git-command-manager'
 import {IGitSourceSettings} from '../lib/git-source-settings'
 
@@ -730,16 +731,16 @@ async function setup(testName: string): Promise<void> {
     checkout: jest.fn(),
     checkoutDetach: jest.fn(),
     config: jest.fn(
-      async (key: string, value: string, globalConfig?: boolean) => {
-        const configPath = globalConfig
+      async (key: string, value: string, configScope?: ConfigScope) => {
+        const configPath = configScope
           ? path.join(git.env['HOME'] || tempHomedir, '.gitconfig')
           : localGitConfigPath
         await fs.promises.appendFile(configPath, `\n${key} ${value}`)
       }
     ),
     configExists: jest.fn(
-      async (key: string, globalConfig?: boolean): Promise<boolean> => {
-        const configPath = globalConfig
+      async (key: string, configScope?: ConfigScope): Promise<boolean> => {
+        const configPath = configScope
           ? path.join(git.env['HOME'] || tempHomedir, '.gitconfig')
           : localGitConfigPath
         const content = await fs.promises.readFile(configPath)
@@ -774,8 +775,8 @@ async function setup(testName: string): Promise<void> {
     tagExists: jest.fn(),
     tryClean: jest.fn(),
     tryConfigUnset: jest.fn(
-      async (key: string, globalConfig?: boolean): Promise<boolean> => {
-        const configPath = globalConfig
+      async (key: string, configScope?: ConfigScope): Promise<boolean> => {
+        const configPath = configScope
           ? path.join(git.env['HOME'] || tempHomedir, '.gitconfig')
           : localGitConfigPath
         let content = await fs.promises.readFile(configPath)
diff --git a/src/git-auth-helper.ts b/src/git-auth-helper.ts
index 6e3ad28..6dbec52 100644
--- a/src/git-auth-helper.ts
+++ b/src/git-auth-helper.ts
@@ -9,6 +9,7 @@ import * as regexpHelper from './regexp-helper'
 import * as stateHelper from './state-helper'
 import * as urlHelper from './url-helper'
 import {default as uuid} from 'uuid/v4'
+import {ConfigScope} from './git-command-manager'
 import {IGitCommandManager} from './git-command-manager'
 import {IGitSourceSettings} from './git-source-settings'
 
@@ -129,13 +130,13 @@ class GitAuthHelper {
     const newGitConfigPath = await this.configureTempGlobalConfig()
     try {
       // Configure the token
-      await this.configureToken(newGitConfigPath, true)
+      await this.configureToken(newGitConfigPath, ConfigScope.System)
 
       // Configure HTTPS instead of SSH
-      await this.git.tryConfigUnset(this.insteadOfKey, true)
+      await this.git.tryConfigUnset(this.insteadOfKey, ConfigScope.System)
       if (!this.settings.sshKey) {
         for (const insteadOfValue of this.insteadOfValues) {
-          await this.git.config(this.insteadOfKey, insteadOfValue, true, true)
+          await this.git.config(this.insteadOfKey, insteadOfValue, ConfigScope.System, true)
         }
       }
     } catch (err) {
@@ -143,7 +144,7 @@ class GitAuthHelper {
       core.info(
         'Encountered an error when attempting to configure token. Attempting unconfigure.'
       )
-      await this.git.tryConfigUnset(this.tokenConfigKey, true)
+      await this.git.tryConfigUnset(this.tokenConfigKey, ConfigScope.System)
       throw err
     }
   }
@@ -274,16 +275,16 @@ class GitAuthHelper {
 
   private async configureToken(
     configPath?: string,
-    globalConfig?: boolean
+    configScope?: ConfigScope
   ): Promise<void> {
     // Validate args
     assert.ok(
-      (configPath && globalConfig) || (!configPath && !globalConfig),
+      (configPath && configScope) || (!configPath && !configScope),
       'Unexpected configureToken parameter combinations'
     )
 
     // Default config path
-    if (!configPath && !globalConfig) {
+    if (!configPath && !configScope) {
       configPath = path.join(this.git.getWorkingDirectory(), '.git', 'config')
     }
 
@@ -293,7 +294,7 @@ class GitAuthHelper {
     await this.git.config(
       this.tokenConfigKey,
       this.tokenPlaceholderConfigValue,
-      globalConfig
+      configScope,
     )
 
     // Replace the placeholder
diff --git a/src/git-command-manager.ts b/src/git-command-manager.ts
index 01aedfe..eb80485 100644
--- a/src/git-command-manager.ts
+++ b/src/git-command-manager.ts
@@ -12,6 +12,12 @@ import {GitVersion} from './git-version'
 // Wire protocol v2 not supported before 2.18
 export const MinimumGitVersion = new GitVersion('2.18')
 
+export enum ConfigScope {
+  Local = "local",
+  Global = "global",
+  System = "system",
+}
+
 export interface IGitCommandManager {
   branchDelete(remote: boolean, branch: string): Promise<void>
   branchExists(remote: boolean, pattern: string): Promise<boolean>
@@ -21,10 +27,10 @@ export interface IGitCommandManager {
   config(
     configKey: string,
     configValue: string,
-    globalConfig?: boolean,
+    configScope?: ConfigScope,
     add?: boolean
   ): Promise<void>
-  configExists(configKey: string, globalConfig?: boolean): Promise<boolean>
+  configExists(configKey: string, configScope?: ConfigScope): Promise<boolean>
   fetch(refSpec: string[], fetchDepth?: number): Promise<void>
   getDefaultBranch(repositoryUrl: string): Promise<string>
   getWorkingDirectory(): string
@@ -43,7 +49,7 @@ export interface IGitCommandManager {
   submoduleUpdate(fetchDepth: number, recursive: boolean): Promise<void>
   tagExists(pattern: string): Promise<boolean>
   tryClean(): Promise<boolean>
-  tryConfigUnset(configKey: string, globalConfig?: boolean): Promise<boolean>
+  tryConfigUnset(configKey: string, configScope?: ConfigScope): Promise<boolean>
   tryDisableAutomaticGarbageCollection(): Promise<boolean>
   tryGetFetchUrl(): Promise<string>
   tryReset(): Promise<boolean>
@@ -172,10 +178,10 @@ class GitCommandManager {
   async config(
     configKey: string,
     configValue: string,
-    globalConfig?: boolean,
+    configScope?: ConfigScope,
     add?: boolean
   ): Promise<void> {
-    const args: string[] = ['config', globalConfig ? '--global' : '--local']
+    const args: string[] = ['config', configScope ? `--${configScope}` : '--local']
     if (add) {
       args.push('--add')
     }
@@ -185,13 +191,13 @@ class GitCommandManager {
 
   async configExists(
     configKey: string,
-    globalConfig?: boolean
+    configScope?: ConfigScope
   ): Promise<boolean> {
     const pattern = regexpHelper.escape(configKey)
     const output = await this.execGit(
       [
         'config',
-        globalConfig ? '--global' : '--local',
+        configScope ? `--${configScope}` : '--local',
         '--name-only',
         '--get-regexp',
         pattern
@@ -369,12 +375,12 @@ class GitCommandManager {
 
   async tryConfigUnset(
     configKey: string,
-    globalConfig?: boolean
+    configScope?: ConfigScope
   ): Promise<boolean> {
     const output = await this.execGit(
       [
         'config',
-        globalConfig ? '--global' : '--local',
+        configScope ? `--${configScope}` : '--local',
         '--unset-all',
         configKey
       ],
diff --git a/src/git-source-provider.ts b/src/git-source-provider.ts
index 48f20da..cbec039 100644
--- a/src/git-source-provider.ts
+++ b/src/git-source-provider.ts
@@ -9,6 +9,7 @@ import * as path from 'path'
 import * as refHelper from './ref-helper'
 import * as stateHelper from './state-helper'
 import * as urlHelper from './url-helper'
+import {ConfigScope} from './git-command-manager'
 import {IGitCommandManager} from './git-command-manager'
 import {IGitSourceSettings} from './git-source-settings'
 
@@ -49,7 +50,7 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
         )
 
         await git
-          .config('safe.directory', settings.repositoryPath, true, true)
+          .config('safe.directory', settings.repositoryPath, ConfigScope.System, true)
           .catch(error => {
             core.info(
               `Failed to initialize safe directory with error: ${error}`
@@ -278,7 +279,7 @@ export async function cleanup(repositoryPath: string): Promise<void> {
       )
 
       await git
-        .config('safe.directory', repositoryPath, true, true)
+        .config('safe.directory', repositoryPath, ConfigScope.System, true)
         .catch(error => {
           core.info(`Failed to initialize safe directory with error: ${error}`)
         })