Skip to content

Commit

Permalink
feat: retries only on a specific error (and update tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ricardo Souza committed Aug 14, 2020
1 parent 18e4918 commit 7cf6771
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/common/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ const merge = async (
await octokit.graphql(mutation, { commitHeadline, pullRequestId });
};

const shouldRetry = (error: Error): boolean =>
error.message.includes('Base branch was modified.');

export const mergeWithRetry = async (
octokit: ReturnType<typeof getOctokit>,
details: PullRequestDetails & {
Expand All @@ -65,7 +68,7 @@ export const mergeWithRetry = async (
/* eslint-disable-next-line @typescript-eslint/no-base-to-string */
logDebug(`Original error: ${(error as Error).toString()}.`);

if (retryCount <= maximumRetries) {
if (shouldRetry(error) && retryCount <= maximumRetries) {
const nextRetryIn =
retryCount ** EXPONENTIAL_BACKOFF *
(minimumWaitTime === undefined ? MINIMUM_WAIT_TIME : minimumWaitTime);
Expand Down
9 changes: 7 additions & 2 deletions src/eventHandlers/checkSuite/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,10 @@ describe('check Suite event handler', (): void => {
})
.post('/graphql')
.times(3)
.reply(403, 'Error when merging');
.reply(
403,
'##[error]GraphqlError: Base branch was modified. Review and try the merge again.',
);

const mergeWithRetrySpy = jest.spyOn(merge, 'mergeWithRetry');
const logDebugSpy = jest.spyOn(log, 'logDebug');
Expand All @@ -418,7 +421,9 @@ describe('check Suite event handler', (): void => {
});
} catch (error) {
expect(error).toBeInstanceOf(Error);
expect(error.message).toStrictEqual('Error when merging');
expect(error.message).toStrictEqual(
'##[error]GraphqlError: Base branch was modified. Review and try the merge again.',
);
expect(mergeWithRetrySpy).toHaveBeenCalledTimes(3);
expect(logDebugSpy).toHaveBeenCalledTimes(3);
expect(logInfoSpy.mock.calls[1][0]).toStrictEqual(
Expand Down
9 changes: 7 additions & 2 deletions src/eventHandlers/pullRequest/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,10 @@ describe('pull request event handler', (): void => {
})
.post('/graphql')
.times(3)
.reply(403, 'Error when merging');
.reply(
403,
'##[error]GraphqlError: Base branch was modified. Review and try the merge again.',
);

const mergeWithRetrySpy = jest.spyOn(merge, 'mergeWithRetry');
const logDebugSpy = jest.spyOn(log, 'logDebug');
Expand All @@ -195,7 +198,9 @@ describe('pull request event handler', (): void => {
});
} catch (error) {
expect(error).toBeInstanceOf(Error);
expect(error.message).toStrictEqual('Error when merging');
expect(error.message).toStrictEqual(
'##[error]GraphqlError: Base branch was modified. Review and try the merge again.',
);
expect(mergeWithRetrySpy).toHaveBeenCalledTimes(3);
expect(logDebugSpy).toHaveBeenCalledTimes(3);
expect(logInfoSpy.mock.calls[1][0]).toStrictEqual(
Expand Down
9 changes: 7 additions & 2 deletions src/eventHandlers/push/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,10 @@ describe('push event handler', (): void => {
})
.post('/graphql')
.times(3)
.reply(403, 'Error when merging');
.reply(
403,
'##[error]GraphqlError: Base branch was modified. Review and try the merge again.',
);

const mergeWithRetrySpy = jest.spyOn(merge, 'mergeWithRetry');
const logDebugSpy = jest.spyOn(log, 'logDebug');
Expand All @@ -326,7 +329,9 @@ describe('push event handler', (): void => {
});
} catch (error) {
expect(error).toBeInstanceOf(Error);
expect(error.message).toStrictEqual('Error when merging');
expect(error.message).toStrictEqual(
'##[error]GraphqlError: Base branch was modified. Review and try the merge again.',
);
expect(mergeWithRetrySpy).toHaveBeenCalledTimes(3);
expect(logDebugSpy).toHaveBeenCalledTimes(3);
expect(logInfoSpy.mock.calls[1][0]).toStrictEqual(
Expand Down

0 comments on commit 7cf6771

Please sign in to comment.