Skip to content

Commit

Permalink
feat: add a flag to prevent retries (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith committed Mar 30, 2021
1 parent a5304a1 commit aede629
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/github-handler/commit-and-push-handler.ts
Expand Up @@ -179,7 +179,8 @@ export async function commitAndPush(
);
await updateRef(octokit, originBranch, commitSha, force);
} catch (err) {
logger.error('Error while creating a tree and updating the ref');
err.message = `Error while creating a tree and updating the ref: ${err.message}`;
logger.error(err);
throw err;
}
}
12 changes: 9 additions & 3 deletions src/index.ts
Expand Up @@ -134,6 +134,10 @@ async function createPullRequest(
...origin,
branch: gitHubConfigs.branch,
};

// The `retry` flag defaults to `5` to maintain compatibility
options.retry = options.retry === undefined ? 5 : options.retry;

const refHeadSha: string = await retry(
async () =>
await handler.branch(
Expand All @@ -144,12 +148,14 @@ async function createPullRequest(
gitHubConfigs.primary
),
{
retries: 5,
retries: options.retry,
factor: 2.8411, // https://www.wolframalpha.com/input/?i=Sum%5B3000*x%5Ek%2C+%7Bk%2C+0%2C+4%7D%5D+%3D+5+*+60+*+1000
minTimeout: 3000,
randomize: false,
onRetry: () => {
logger.info('Retrying at a later time...');
onRetry: (e, attempt) => {
e.message = `Error creating Pull Request: ${e.message}`;
logger.error(e);
logger.info(`Retry attempt #${attempt}...`);
},
}
);
Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions src/types/index.ts → src/types.ts
Expand Up @@ -95,6 +95,8 @@ export interface CreatePullRequestUserOptions {
maintainersCanModify?: boolean;
// The list of labels to apply to the newly created PR. Default is empty. (optional)
labels?: string[];
// Number of times to retry if the request fails. Defaults to 5.
retry?: number;
}

/**
Expand Down
28 changes: 24 additions & 4 deletions test/main-make-pr.ts
Expand Up @@ -12,21 +12,23 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import {assert, expect} from 'chai';
import {expect} from 'chai';
import * as assert from 'assert';
import {describe, it, before, afterEach} from 'mocha';
import {octokit, setup} from './util';
import * as sinon from 'sinon';
import {Changes, FileData, CreatePullRequestUserOptions} from '../src/types';
import {Octokit} from '@octokit/rest';
import * as proxyquire from 'proxyquire';
import * as retry from 'async-retry';
import * as idx from '../src/index';
import * as handler from '../src/github-handler/branch-handler';

before(() => {
setup();
});

/* eslint-disable @typescript-eslint/no-unused-vars */
// tslint:disable:no-unused-expression
// .true triggers ts-lint failure, but is valid chai
describe('Make PR main function', () => {
const upstreamOwner = 'owner';
const upstreamRepo = 'Hello-World';
Expand Down Expand Up @@ -271,6 +273,25 @@ describe('Make PR main function', () => {
expect(err.message).equals('Create branch helper failed');
}
});

it('should respect the retry flag', async () => {
const stub = sinon.stub(handler, 'branch').throws('boop');
// eslint-disable-next-line node/no-unsupported-features/node-builtins
await assert.rejects(
idx.createPullRequest(octokit, changes, {
title: 'hello',
message: 'hello',
description: 'hello',
fork: false,
upstreamOwner: 'googleapis',
upstreamRepo: 'nodejs-storage',
retry: 0,
}),
/boop/
);
assert.ok(stub.calledOnce);
});

it('Passes up the error message with a throw when helper commit and push helper function fails', async () => {
// setup

Expand Down Expand Up @@ -401,6 +422,5 @@ describe('Make PR main function', () => {
await stubMakePr.createPullRequest(octokit, null, options);
await stubMakePr.createPullRequest(octokit, undefined, options);
await stubMakePr.createPullRequest(octokit, new Map(), options);
assert.isOk(true);
});
});

0 comments on commit aede629

Please sign in to comment.