From f533962a987f86bd47cd9d63524fc9e2ca52a4d0 Mon Sep 17 00:00:00 2001 From: Rhys Arkins <rhys@arkins.net> Date: Wed, 3 Feb 2021 15:03:44 +0100 Subject: [PATCH] feat(github): change fork default branch (#8516) --- .../github/__snapshots__/index.spec.ts.snap | 28 +++++++++++++++++++ lib/platform/github/index.spec.ts | 2 ++ lib/platform/github/index.ts | 27 +++++++++++++++++- 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/lib/platform/github/__snapshots__/index.spec.ts.snap b/lib/platform/github/__snapshots__/index.spec.ts.snap index dce5916227..04620e028a 100644 --- a/lib/platform/github/__snapshots__/index.spec.ts.snap +++ b/lib/platform/github/__snapshots__/index.spec.ts.snap @@ -4830,6 +4830,34 @@ Array [ "method": "POST", "url": "https://api.github.com/repos/some/repo/forks", }, + Object { + "body": "{\\"ref\\":\\"refs/heads/master\\",\\"sha\\":\\"1234\\"}", + "headers": Object { + "accept": "application/vnd.github.v3+json", + "accept-encoding": "gzip, deflate", + "authorization": "token abc123", + "content-length": "40", + "content-type": "application/json", + "host": "api.github.com", + "user-agent": "https://github.com/renovatebot/renovate", + }, + "method": "POST", + "url": "https://api.github.com/repos/forked/repo/git/refs", + }, + Object { + "body": "{\\"default_branch\\":\\"master\\"}", + "headers": Object { + "accept": "application/vnd.github.v3+json", + "accept-encoding": "gzip, deflate", + "authorization": "token abc123", + "content-length": "27", + "content-type": "application/json", + "host": "api.github.com", + "user-agent": "https://github.com/renovatebot/renovate", + }, + "method": "PATCH", + "url": "https://api.github.com/repos/forked/repo", + }, Object { "body": "{\\"sha\\":\\"1234\\",\\"force\\":true}", "headers": Object { diff --git a/lib/platform/github/index.spec.ts b/lib/platform/github/index.spec.ts index f0ac6a8fd8..e3e4a95005 100644 --- a/lib/platform/github/index.spec.ts +++ b/lib/platform/github/index.spec.ts @@ -258,6 +258,8 @@ describe('platform/github', () => { it('detects fork default branch mismatch', async () => { const scope = httpMock.scope(githubApiHost); forkInitRepoMock(scope, 'some/repo', true, 'not_master'); + scope.post('/repos/forked/repo/git/refs').reply(200); + scope.patch('/repos/forked/repo').reply(200); scope.patch('/repos/forked/repo/git/refs/heads/master').reply(200); const config = await github.initRepo({ repository: 'some/repo', diff --git a/lib/platform/github/index.ts b/lib/platform/github/index.ts index 6fae26284f..e741f2a65c 100644 --- a/lib/platform/github/index.ts +++ b/lib/platform/github/index.ts @@ -309,13 +309,38 @@ export async function initRepo({ config.repository = forkedRepo.body.full_name; const forkDefaultBranch = forkedRepo.body.default_branch; if (forkDefaultBranch !== config.defaultBranch) { + const body = { + ref: `refs/heads/${config.defaultBranch}`, + sha: repo.defaultBranchRef.target.oid, + }; logger.debug( { defaultBranch: config.defaultBranch, forkDefaultBranch, + body, }, - 'Fork has different default branch to parent' + 'Fork has different default branch to parent, attempting to create branch' ); + try { + await githubApi.postJson(`repos/${config.repository}/git/refs`, { + body, + token: forkToken || opts.token, + }); + logger.debug('Created new default branch in fork'); + } catch (err) /* istanbul ignore next */ { + 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 }, + }); + logger.debug('Successfully changed default branch for fork'); + } catch (err) /* istanbul ignore next */ { + logger.warn({ err }, 'Could not set default branch'); + } } } catch (err) /* istanbul ignore next */ { logger.debug({ err }, 'Error forking repository'); -- GitLab