Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(github): try push branch if rebase fails #22376

Merged
merged 2 commits into from May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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}`;
rarkins marked this conversation as resolved.
Show resolved Hide resolved

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'
);
rarkins marked this conversation as resolved.
Show resolved Hide resolved
} else {
logger.warn({ refUrl, err, branchResult }, 'Error updating branch');
throw err;
}
}
}

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