diff --git a/lib/modules/platform/github/index.spec.ts b/lib/modules/platform/github/index.spec.ts index 12967fcd9f07295f38f0503e6f1d523c7b9fb919..31bec33b22c41e7fb1178dd88c0c011296c37496 100644 --- a/lib/modules/platform/github/index.spec.ts +++ b/lib/modules/platform/github/index.spec.ts @@ -865,13 +865,19 @@ describe('modules/platform/github/index', () => { }, { number: 91, - head: { ref: 'somebranch', repo: { full_name: 'some/repo' } }, + head: { + ref: 'somebranch', + sha: '123', + repo: { full_name: 'some/repo' }, + }, title: 'old title - autoclosed', state: 'closed', closed_at: DateTime.now().minus({ days: 6 }).toISO(), updated_at: '01-09-2022', }, ]) + .head('/repos/some/repo/git/commits/123') + .reply(200) .head('/repos/some/repo/git/refs/heads/somebranch') .reply(404) .post('/repos/some/repo/git/refs') @@ -957,12 +963,18 @@ describe('modules/platform/github/index', () => { .reply(200, [ { number: 91, - head: { ref: 'somebranch', repo: { full_name: 'some/repo' } }, + head: { + ref: 'somebranch', + repo: { full_name: 'some/repo' }, + sha: '123', + }, title: 'old title - autoclosed', state: 'closed', closed_at: DateTime.now().minus({ minutes: 10 }).toISO(), }, ]) + .head('/repos/some/repo/git/commits/123') + .reply(200) .head('/repos/some/repo/git/refs/heads/somebranch') .reply(404) .post('/repos/some/repo/git/refs') @@ -985,12 +997,18 @@ describe('modules/platform/github/index', () => { .reply(200, [ { number: 91, - head: { ref: 'somebranch', repo: { full_name: 'some/repo' } }, + head: { + ref: 'somebranch', + sha: '123', + repo: { full_name: 'some/repo' }, + }, title: 'old title - autoclosed', state: 'closed', closed_at: DateTime.now().minus({ minutes: 10 }).toISO(), }, ]) + .head('/repos/some/repo/git/commits/123') + .reply(200) .head('/repos/some/repo/git/refs/heads/somebranch') .reply(422); @@ -3290,6 +3308,8 @@ describe('modules/platform/github/index', () => { .reply(200, { sha: '111' }) .post('/repos/some/repo/git/commits') .reply(200, { sha: '222' }) + .head('/repos/some/repo/git/commits/222') + .reply(200) .head('/repos/some/repo/git/refs/heads/foo/bar') .reply(404) .post('/repos/some/repo/git/refs') @@ -3318,6 +3338,8 @@ describe('modules/platform/github/index', () => { .reply(200, { sha: '111' }) .post('/repos/some/repo/git/commits') .reply(200, { sha: '222' }) + .head('/repos/some/repo/git/commits/222') + .reply(200) .head('/repos/some/repo/git/refs/heads/foo/bar') .reply(200) .patch('/repos/some/repo/git/refs/heads/foo/bar') @@ -3346,6 +3368,8 @@ describe('modules/platform/github/index', () => { .reply(200, { sha: '111' }) .post('/repos/some/repo/git/commits') .reply(200, { sha: '222' }) + .head('/repos/some/repo/git/commits/222') + .reply(200) .head('/repos/some/repo/git/refs/heads/foo/bar') .reply(200) .patch('/repos/some/repo/git/refs/heads/foo/bar') @@ -3376,6 +3400,8 @@ describe('modules/platform/github/index', () => { .reply(200, { sha: '111' }) .post('/repos/some/repo/git/commits') .reply(200, { sha: '222' }) + .head('/repos/some/repo/git/commits/222') + .reply(200) .head('/repos/some/repo/git/refs/heads/foo/bar') .reply(200) .patch('/repos/some/repo/git/refs/heads/foo/bar') @@ -3389,5 +3415,31 @@ describe('modules/platform/github/index', () => { expect(res).toBeNull(); }); + + it("aborts if commit SHA doesn't exist", 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/commits/222') + .reply(404); + + const res = await github.commitFiles({ + branchName: 'foo/bar', + files: [{ type: 'addition', path: 'foo.bar', contents: 'foobar' }], + message: 'Foobar', + }); + + expect(res).toBeNull(); + }); }); }); diff --git a/lib/modules/platform/github/index.ts b/lib/modules/platform/github/index.ts index 212666485210ee1a1464692f4e7975f6752a1aa8..6bd3ab403f54210f46c18fc90b04dc54fa6d259c 100644 --- a/lib/modules/platform/github/index.ts +++ b/lib/modules/platform/github/index.ts @@ -739,8 +739,15 @@ export async function findPr({ const REOPEN_THRESHOLD_MILLIS = 1000 * 60 * 60 * 24 * 7; async function ensureBranchSha(branchName: string, sha: string): Promise<void> { - const refUrl = `/repos/${config.repository}/git/refs/heads/${branchName}`; + try { + const commitUrl = `/repos/${config.repository}/git/commits/${sha}`; + await githubApi.head(commitUrl, { memCache: false }); + } catch (err) { + logger.error({ err, sha, branchName }, 'Commit not found'); + throw err; + } + const refUrl = `/repos/${config.repository}/git/refs/heads/${branchName}`; let branchExists = false; let branchResult: undefined | HttpResponse<string>; try { @@ -811,7 +818,10 @@ export async function getBranchPr(branchName: string): Promise<GhPr | null> { await ensureBranchSha(branchName, sha!); logger.debug(`Recreated autoclosed branch ${branchName} with sha ${sha}`); } catch (err) { - logger.debug('Could not recreate autoclosed branch - skipping reopen'); + logger.debug( + { err, branchName, sha, autoclosedPr }, + 'Could not recreate autoclosed branch - skipping reopen' + ); return null; } try {