Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project Management Automation: Check error object for parsed errors #20014

Merged
merged 2 commits into from
Feb 3, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion packages/project-management-automation/lib/add-milestone.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@ const REFERENCE_DATE = '2019-08-12';
// Releases are every 14 days.
const DAYS_PER_RELEASE = 14;

/**
* Returns true if the given error object represents a duplicate entry error, or
* false otherwise. The error is expected to be (though not verified explicitly
* as) an instance of Octokit's RequestError (`@octokit/request-error`).
*
* @see https://github.com/octokit/rest.js/issues/684
* @see https://developer.github.com/v3/#client-errors
* @see https://github.com/octokit/request.js/blob/2a1d768/src/fetch-wrapper.ts#L79-L80
*
* @param {Error} error Error to test.
*
* @return {boolean} Whether error is a duplicate validation request error.
*/
const isDuplicateValidationError = ( error ) =>
Array.isArray( error.errors ) &&
error.errors.some( ( { code } ) => code === 'already_exists' );

/**
* Assigns the correct milestone to PRs once merged.
*
Expand Down Expand Up @@ -95,7 +112,7 @@ async function addMilestone( payload, octokit ) {

debug( 'add-milestone: Milestone created' );
} catch ( error ) {
if ( error.code !== 'already_exists' ) {
if ( ! isDuplicateValidationError( error ) ) {
throw error;
}

Expand Down