From dd636ed15ca824a4e3ee269e51c79ffec7588b7d Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Sat, 27 Jun 2020 12:21:47 +0200
Subject: [PATCH] refactor: pass git author in initRepo (#6605)

---
 lib/platform/azure/index.ts            |  2 ++
 lib/platform/bitbucket-server/index.ts |  2 ++
 lib/platform/bitbucket/index.ts        |  2 ++
 lib/platform/git/storage.spec.ts       |  6 ++----
 lib/platform/git/storage.ts            | 24 +++++++++++++++---------
 lib/platform/gitea/index.ts            |  2 ++
 lib/platform/github/index.ts           |  2 ++
 lib/platform/gitlab/index.ts           |  2 ++
 8 files changed, 29 insertions(+), 13 deletions(-)

diff --git a/lib/platform/azure/index.ts b/lib/platform/azure/index.ts
index 4dc108915a..f2e24997fb 100644
--- a/lib/platform/azure/index.ts
+++ b/lib/platform/azure/index.ts
@@ -169,6 +169,8 @@ export async function initRepo({
     localDir,
     url,
     extraCloneOpts: azureHelper.getStorageExtraCloneOpts(opts),
+    gitAuthorName: global.gitAuthor?.name,
+    gitAuthorEmail: global.gitAuthor?.email,
   });
   const repoConfig: RepoConfig = {
     baseBranch: config.baseBranch,
diff --git a/lib/platform/bitbucket-server/index.ts b/lib/platform/bitbucket-server/index.ts
index 83d32dceee..3dbf30082c 100644
--- a/lib/platform/bitbucket-server/index.ts
+++ b/lib/platform/bitbucket-server/index.ts
@@ -191,6 +191,8 @@ export async function initRepo({
     ...config,
     localDir,
     url: gitUrl,
+    gitAuthorName: global.gitAuthor?.name,
+    gitAuthorEmail: global.gitAuthor?.email,
   });
 
   try {
diff --git a/lib/platform/bitbucket/index.ts b/lib/platform/bitbucket/index.ts
index 61552d83da..cd5b87a0f3 100644
--- a/lib/platform/bitbucket/index.ts
+++ b/lib/platform/bitbucket/index.ts
@@ -160,6 +160,8 @@ export async function initRepo({
     ...config,
     localDir,
     url,
+    gitAuthorName: global.gitAuthor?.name,
+    gitAuthorEmail: global.gitAuthor?.email,
   });
   const repoConfig: RepoConfig = {
     baseBranch: config.baseBranch,
diff --git a/lib/platform/git/storage.spec.ts b/lib/platform/git/storage.spec.ts
index 1966fe7ba3..7e1efd4a93 100644
--- a/lib/platform/git/storage.spec.ts
+++ b/lib/platform/git/storage.spec.ts
@@ -47,16 +47,14 @@ describe('platform/git/storage', () => {
     const repo = Git(origin.path);
     await repo.clone(base.path, '.', ['--bare']);
     tmpDir = await tmp.dir({ unsafeCleanup: true });
-    global.gitAuthor = {
-      name: 'test',
-      email: 'test@example.com',
-    };
     await git.initRepo({
       localDir: tmpDir.path,
       url: origin.path,
       extraCloneOpts: {
         '--config': 'extra.clone.config=test-extra-config-value',
       },
+      gitAuthorName: 'test',
+      gitAuthorEmail: 'test@example.com',
     });
   });
 
diff --git a/lib/platform/git/storage.ts b/lib/platform/git/storage.ts
index aa9e3ec0c1..0cc66884af 100644
--- a/lib/platform/git/storage.ts
+++ b/lib/platform/git/storage.ts
@@ -28,6 +28,8 @@ interface StorageConfig {
   baseBranch?: string;
   url: string;
   extraCloneOpts?: Git.Options;
+  gitAuthorName?: string;
+  gitAuthorEmail?: string;
 }
 
 interface LocalConfig extends StorageConfig {
@@ -200,16 +202,20 @@ export class Storage {
       }
       logger.warn({ err }, 'Cannot retrieve latest commit date');
     }
-    if (global.gitAuthor) {
-      logger.debug({ gitAuthor: global.gitAuthor }, 'Setting git author');
-      try {
-        await this._git.raw(['config', 'user.name', global.gitAuthor.name]);
-        await this._git.raw(['config', 'user.email', global.gitAuthor.email]);
-      } catch (err) /* istanbul ignore next */ {
-        checkForPlatformFailure(err);
-        logger.debug({ err }, 'Error setting git config');
-        throw new Error(REPOSITORY_TEMPORARY_ERROR);
+    try {
+      const { gitAuthorName, gitAuthorEmail } = args;
+      if (gitAuthorName) {
+        logger.debug({ gitAuthorName }, 'Setting git author name');
+        await this._git.raw(['config', 'user.name', gitAuthorName]);
       }
+      if (gitAuthorEmail) {
+        logger.debug({ gitAuthorEmail }, 'Setting git author email');
+        await this._git.raw(['config', 'user.email', gitAuthorEmail]);
+      }
+    } catch (err) /* istanbul ignore next */ {
+      checkForPlatformFailure(err);
+      logger.debug({ err }, 'Error setting git author config');
+      throw new Error(REPOSITORY_TEMPORARY_ERROR);
     }
 
     await setBaseBranchToDefault(this._git);
diff --git a/lib/platform/gitea/index.ts b/lib/platform/gitea/index.ts
index 2fea422ce9..5a394fbbf3 100644
--- a/lib/platform/gitea/index.ts
+++ b/lib/platform/gitea/index.ts
@@ -338,6 +338,8 @@ const platform: Platform = {
     await config.storage.initRepo({
       ...config,
       url: URL.format(gitEndpoint),
+      gitAuthorName: global.gitAuthor?.name,
+      gitAuthorEmail: global.gitAuthor?.email,
     });
 
     // Reset cached resources
diff --git a/lib/platform/github/index.ts b/lib/platform/github/index.ts
index d23dc10cbb..d45a87d29a 100644
--- a/lib/platform/github/index.ts
+++ b/lib/platform/github/index.ts
@@ -436,6 +436,8 @@ export async function initRepo({
   await config.storage.initRepo({
     ...config,
     url,
+    gitAuthorName: global.gitAuthor?.name,
+    gitAuthorEmail: global.gitAuthor?.email,
   });
   const repoConfig: RepoConfig = {
     baseBranch: config.baseBranch,
diff --git a/lib/platform/gitlab/index.ts b/lib/platform/gitlab/index.ts
index e7e5b37a40..f3b61624f5 100644
--- a/lib/platform/gitlab/index.ts
+++ b/lib/platform/gitlab/index.ts
@@ -249,6 +249,8 @@ export async function initRepo({
     await config.storage.initRepo({
       ...config,
       url,
+      gitAuthorName: global.gitAuthor?.name,
+      gitAuthorEmail: global.gitAuthor?.email,
     });
   } catch (err) /* istanbul ignore next */ {
     logger.debug({ err }, 'Caught initRepo error');
-- 
GitLab