diff --git a/src/github-handler/commit-and-push-handler.ts b/src/github-handler/commit-and-push-handler.ts index 47714491..dff4af52 100644 --- a/src/github-handler/commit-and-push-handler.ts +++ b/src/github-handler/commit-and-push-handler.ts @@ -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; } } diff --git a/src/index.ts b/src/index.ts index fe89eb92..f393a353 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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( @@ -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}...`); }, } ); diff --git a/src/logger/index.ts b/src/logger.ts similarity index 100% rename from src/logger/index.ts rename to src/logger.ts diff --git a/src/types/index.ts b/src/types.ts similarity index 98% rename from src/types/index.ts rename to src/types.ts index f7300dd7..4096673d 100644 --- a/src/types/index.ts +++ b/src/types.ts @@ -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; } /** diff --git a/test/main-make-pr.ts b/test/main-make-pr.ts index 3e4055f1..30ceb8c6 100644 --- a/test/main-make-pr.ts +++ b/test/main-make-pr.ts @@ -12,7 +12,8 @@ // 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'; @@ -20,13 +21,14 @@ 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'; @@ -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 @@ -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); }); });