Navigation Menu

Skip to content

Commit

Permalink
fix(github): try push branch if rebase fails (#22376)
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins committed May 25, 2023
1 parent a8c8c00 commit 3cc637f
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
58 changes: 58 additions & 0 deletions lib/modules/platform/github/index.spec.ts
Expand Up @@ -3332,5 +3332,63 @@ describe('modules/platform/github/index', () => {

expect(res).toBe('0abcdef');
});

it('continues if rebase fails due to 422', async () => {
git.pushCommitToRenovateRef.mockResolvedValueOnce();
git.listCommitTree.mockResolvedValueOnce([]);

const scope = httpMock.scope(githubApiHost);

initRepoMock(scope, 'some/repo');
await github.initRepo({ repository: 'some/repo' });

scope
.post('/repos/some/repo/git/trees')
.reply(200, { sha: '111' })
.post('/repos/some/repo/git/commits')
.reply(200, { sha: '222' })
.head('/repos/some/repo/git/refs/heads/foo/bar')
.reply(200)
.patch('/repos/some/repo/git/refs/heads/foo/bar')
.reply(422)
.post('/repos/some/repo/git/refs')
.reply(200);

const res = await github.commitFiles({
branchName: 'foo/bar',
files: [{ type: 'addition', path: 'foo.bar', contents: 'foobar' }],
message: 'Foobar',
});

expect(res).toBe('0abcdef');
});

it('aborts if rebase fails due to non-422', async () => {
git.pushCommitToRenovateRef.mockResolvedValueOnce();
git.listCommitTree.mockResolvedValueOnce([]);

const scope = httpMock.scope(githubApiHost);

initRepoMock(scope, 'some/repo');
await github.initRepo({ repository: 'some/repo' });

scope
.post('/repos/some/repo/git/trees')
.reply(200, { sha: '111' })
.post('/repos/some/repo/git/commits')
.reply(200, { sha: '222' })
.head('/repos/some/repo/git/refs/heads/foo/bar')
.reply(200)
.patch('/repos/some/repo/git/refs/heads/foo/bar')
.reply(404);

const res = await github.commitFiles({
branchName: 'foo/bar',
files: [{ type: 'addition', path: 'foo.bar', contents: 'foobar' }],
message: 'Foobar',
});

expect(res).toBeNull();
});
});
});
19 changes: 16 additions & 3 deletions lib/modules/platform/github/index.ts
Expand Up @@ -742,8 +742,9 @@ async function ensureBranchSha(branchName: string, sha: string): Promise<void> {
const refUrl = `/repos/${config.repository}/git/refs/heads/${branchName}`;

let branchExists = false;
let branchResult: undefined | HttpResponse<string>;
try {
await githubApi.head(refUrl, { memCache: false });
branchResult = await githubApi.head(refUrl, { memCache: false });
branchExists = true;
} catch (err) {
if (err.statusCode !== 404) {
Expand All @@ -752,8 +753,20 @@ async function ensureBranchSha(branchName: string, sha: string): Promise<void> {
}

if (branchExists) {
await githubApi.patchJson(refUrl, { body: { sha, force: true } });
return;
try {
await githubApi.patchJson(refUrl, { body: { sha, force: true } });
return;
} catch (err) {
if (err.err?.response?.statusCode === 422) {
logger.debug(
{ branchResult, err },
'Branch update failed due to reference not existing - will try to create'
);
} else {
logger.warn({ refUrl, err, branchResult }, 'Error updating branch');
throw err;
}
}
}

await githubApi.postJson(`/repos/${config.repository}/git/refs`, {
Expand Down

0 comments on commit 3cc637f

Please sign in to comment.