diff --git a/src/autoupdater.ts b/src/autoupdater.ts index 93875d47..f95d1032 100644 --- a/src/autoupdater.ts +++ b/src/autoupdater.ts @@ -246,18 +246,27 @@ export class AutoUpdater { return false; } - const { data: comparison } = await this.octokit.rest.repos.compareCommits({ - owner: pull.head.repo.owner.login, - repo: pull.head.repo.name, - // This base->head, head->base logic is intentional, we want - // to see what would happen if we merged the base into head not - // vice-versa. - base: pull.head.label, - head: pull.base.label, - }); - - if (comparison.behind_by === 0) { - ghCore.info('Skipping pull request, up-to-date with base branch.'); + try { + const { data: comparison } = + await this.octokit.rest.repos.compareCommitsWithBasehead({ + owner: pull.head.repo.owner.login, + repo: pull.head.repo.name, + // This base->head, head->base logic is intentional, we want + // to see what would happen if we merged the base into head not + // vice-versa. This parameter expects the format {base}...{head}. + basehead: `${pull.head.label}...${pull.base.label}`, + }); + + if (comparison.behind_by === 0) { + ghCore.info('Skipping pull request, up-to-date with base branch.'); + return false; + } + } catch (e: unknown) { + if (e instanceof Error) { + ghCore.error( + `Caught error trying to compare base with head: ${e.message}`, + ); + } return false; } diff --git a/test/autoupdate.test.ts b/test/autoupdate.test.ts index f4e68f12..3af125bc 100644 --- a/test/autoupdate.test.ts +++ b/test/autoupdate.test.ts @@ -1099,6 +1099,21 @@ describe('test `merge`', () => { } }); + test('handles errors from compareCommitsWithBasehead', async () => { + (config.retryCount as jest.Mock).mockReturnValue(0); + const updater = new AutoUpdater(config, emptyEvent); + + const scope = nock('https://api.github.com:443') + .get(`/repos/${owner}/${repo}/compare/${head}...${base}`) + .reply(404, { + message: 'Not Found', + }); + + const needsUpdate = await updater.update(owner, validPull); + expect(needsUpdate).toEqual(false); + expect(scope.isDone()).toEqual(true); + }); + test('handles fork authorisation errors', async () => { (config.retryCount as jest.Mock).mockReturnValue(0); const updater = new AutoUpdater(config, emptyEvent);