Skip to content

Commit

Permalink
Add test to confirm that autoupdate continues on error
Browse files Browse the repository at this point in the history
Adds a test that throws a 403 half-way through a list of PRs that need
updating to confirm that autoupdate continues to the next PR in line.
  • Loading branch information
chinthakagodawita committed Jan 25, 2021
1 parent 03360cd commit a83d451
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 23 deletions.
5 changes: 4 additions & 1 deletion src/autoupdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ export class AutoUpdater {
try {
await this.merge(mergeOpts);
} catch (e) {
ghCore.info('Autoupdate will attempt to update any remaining PRs.');
ghCore.error(
`Caught error running merge, skipping and continuing with remaining PRs`,
);
ghCore.setFailed(e);
return false;
}

Expand Down
100 changes: 78 additions & 22 deletions test/autoupdate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ const owner = 'chinthakagodawita';
const repo = 'not-a-real-repo';
const base = 'master';
const head = 'develop';
const branch = 'not-a-real-branch';
const dummyEvent = {
ref: `refs/heads/${branch}`,
repository: {
owner: {
name: owner,
},
name: repo,
},
};
const validPull = {
number: 1,
merged: false,
Expand Down Expand Up @@ -316,17 +326,6 @@ describe('test `prNeedsUpdate`', () => {
});

describe('test `handlePush`', () => {
const branch = 'not-a-real-branch';
const dummyEvent = {
ref: `refs/heads/${branch}`,
repository: {
owner: {
name: owner,
},
name: repo,
},
};

const cloneEvent = () => JSON.parse(JSON.stringify(dummyEvent));

test('push event on a non-branch', async () => {
Expand Down Expand Up @@ -606,19 +605,76 @@ describe('test `merge`', () => {
expect(scope.isDone()).toEqual(true);
});

test('autoupdate continues with valid PRs if merging throws an error', async () => {
const mergeMsg = 'dummy-merge-msg';
(config.mergeMsg as jest.Mock).mockReturnValue(mergeMsg);
(config.dryRun as jest.Mock).mockReturnValue(false);
test('continue if merging throws an error', async () => {
(config.mergeMsg as jest.Mock).mockReturnValue(null);
const updater = new AutoUpdater(config, dummyEvent);

const updater = new AutoUpdater(config, {});
const pullsMock = [];
const expectedPulls = 5;
for (let i = 0; i < expectedPulls; i++) {
pullsMock.push({
id: i,
number: i,
base: {
ref: base,
label: base,
},
head: {
label: head,
ref: head,
repo: {
name: repo,
owner: {
login: owner,
},
},
},
});
}

jest.spyOn(updater, 'prNeedsUpdate').mockResolvedValue(true);
jest.spyOn(updater, 'merge').mockImplementation(() => {
throw new Error('Resource not accessible by integration');
});
const needsUpdateSpy = jest
.spyOn(updater, 'prNeedsUpdate')
.mockResolvedValue(true);

const updated = await updater.update(<any>validPull);
expect(updated).toEqual(false);
const pullsScope = nock('https://api.github.com:443')
.get(
`/repos/${owner}/${repo}/pulls?base=${branch}&state=open&sort=updated&direction=desc`,
)
.reply(200, pullsMock);

const mergeScopes = [];
for (let i = 0; i < expectedPulls; i++) {
let httpStatus = 200;
let response: Record<string, unknown> = {
data: {
sha: 'dummy-sha',
},
};

// Throw an error halfway through the PR list to confirm that autoupdate
// continues to the next PR.
if (i === 3) {
httpStatus = 403;
response = {
message: 'Resource not accessible by integration',
};
}

mergeScopes.push(
nock('https://api.github.com:443')
.post(`/repos/${owner}/${repo}/merges`)
.reply(httpStatus, response),
);
}

const updated = await updater.handlePush();

// Only 5 PRs should have been updated.
expect(updated).toBe(expectedPulls - 1);
expect(needsUpdateSpy).toHaveBeenCalledTimes(expectedPulls);
expect(pullsScope.isDone()).toBe(true);
for (const scope of mergeScopes) {
expect(scope.isDone()).toBe(true);
}
});
});

0 comments on commit a83d451

Please sign in to comment.