Skip to content

Commit

Permalink
feat(github): change fork default branch (#8516)
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins committed Feb 3, 2021
1 parent 1914a2e commit f533962
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
28 changes: 28 additions & 0 deletions lib/platform/github/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions lib/platform/github/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
27 changes: 26 additions & 1 deletion lib/platform/github/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit f533962

Please sign in to comment.