Skip to content

Commit

Permalink
Continue if comparing commits returns an error
Browse files Browse the repository at this point in the history
  • Loading branch information
anarast committed Oct 5, 2021
1 parent 3e650ff commit 4b2adf4
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 @@ -172,6 +172,7 @@ export class AutoUpdater {
}

async update(sourceEventOwner: string, pull: PullRequest): Promise<boolean> {
console.log('Updating...............');
const { ref } = pull.head;
ghCore.info(`Evaluating pull request #${pull.number}...`);

Expand Down Expand Up @@ -227,6 +228,7 @@ export class AutoUpdater {
}

async prNeedsUpdate(pull: PullRequest): Promise<boolean> {
console.log('Checking if PR needs update');
if (pull.merged === true) {
ghCore.warning('Skipping pull request, already merged.');
return false;
Expand All @@ -244,18 +246,25 @@ 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) {
`Caught error trying to evaluate PR: ${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 @@ -1038,6 +1038,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 4b2adf4

Please sign in to comment.