From f4e9e92e77bd60f37186f2319f94b4dc16bda084 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Sat, 27 Feb 2021 06:42:41 +0100
Subject: [PATCH] fix(github): include name field when setting fork default
 branch

---
 .../github/__snapshots__/index.spec.ts.snap      |  4 ++--
 lib/platform/github/index.ts                     | 16 ++++++++++++++--
 2 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/lib/platform/github/__snapshots__/index.spec.ts.snap b/lib/platform/github/__snapshots__/index.spec.ts.snap
index 41eee3b17c..d51b08c1e0 100644
--- a/lib/platform/github/__snapshots__/index.spec.ts.snap
+++ b/lib/platform/github/__snapshots__/index.spec.ts.snap
@@ -4845,12 +4845,12 @@ Array [
     "url": "https://api.github.com/repos/forked/repo/git/refs",
   },
   Object {
-    "body": "{\\"default_branch\\":\\"master\\"}",
+    "body": "{\\"name\\":\\"repo\\",\\"default_branch\\":\\"master\\"}",
     "headers": Object {
       "accept": "application/vnd.github.v3+json",
       "accept-encoding": "gzip, deflate",
       "authorization": "token abc123",
-      "content-length": "27",
+      "content-length": "41",
       "content-type": "application/json",
       "host": "api.github.com",
       "user-agent": "https://github.com/renovatebot/renovate",
diff --git a/lib/platform/github/index.ts b/lib/platform/github/index.ts
index c4b742b2c8..1533d7ab28 100644
--- a/lib/platform/github/index.ts
+++ b/lib/platform/github/index.ts
@@ -328,14 +328,26 @@ export async function initRepo({
           });
           logger.debug('Created new default branch in fork');
         } catch (err) /* istanbul ignore next */ {
-          logger.warn({ err }, 'Could not create parent defaultBranch in fork');
+          if (err.response?.body?.message === 'Reference already exists') {
+            logger.debug(
+              `Branch ${config.defaultBranch} already exists in the fork`
+            );
+          } else {
+            logger.warn(
+              { err },
+              'Could not create parent defaultBranch in fork'
+            );
+          }
         }
         logger.debug(
           `Setting ${config.defaultBranch} as default branch for ${config.repository}`
         );
         try {
           await githubApi.patchJson(`repos/${config.repository}`, {
-            body: { default_branch: config.defaultBranch },
+            body: {
+              name: config.repository.split('/')[1],
+              default_branch: config.defaultBranch,
+            },
           });
           logger.debug('Successfully changed default branch for fork');
         } catch (err) /* istanbul ignore next */ {
-- 
GitLab