Skip to content

Commit

Permalink
Merge pull request #252 from chinthakagodawita/pr-not-found
Browse files Browse the repository at this point in the history
  • Loading branch information
chinthakagodawita committed Dec 12, 2021
2 parents c203734 + 5c560ec commit 60e0488
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
33 changes: 21 additions & 12 deletions src/autoupdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
15 changes: 15 additions & 0 deletions test/autoupdate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, <any>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);
Expand Down

0 comments on commit 60e0488

Please sign in to comment.