Skip to content

Commit

Permalink
refactor(ng-dev/pr): move merge ready assertion to common valiations
Browse files Browse the repository at this point in the history
Move the merge ready label check validation to the common validations.
  • Loading branch information
josephperrott committed Sep 27, 2021
1 parent 25b5c13 commit cc3a89b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
17 changes: 17 additions & 0 deletions ng-dev/pr/common/validation/validations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,20 @@ export function assertSignedCla(pullRequest: PullRequestFromGithub) {

throw PullRequestFailure.claUnsigned();
}

/**
* Assert the pull request has been marked ready for merge by the author.
* @throws {PullRequestFailure} if the pull request is missing the merge ready label.
*/
export function assertMergeReady(pullRequest: PullRequestFromGithub, config: PullRequestConfig) {
if (pullRequest.labels.nodes.some(({name}) => matchesPattern(name, config.mergeReadyLabel))) {
return true;
}
throw PullRequestFailure.notMergeReady();
}

// TODO: Remove need to export this pattern matching utility.
/** Checks whether the specified value matches the given pattern. */
export function matchesPattern(value: string, pattern: RegExp | string): boolean {
return typeof pattern === 'string' ? value === pattern : pattern.test(value);
}
8 changes: 3 additions & 5 deletions ng-dev/pr/merge/pull-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@

import {parseCommitMessage} from '../../commit-message/parse';
import {PullRequestFailure} from '../common/validation/failures';
import {matchesPattern} from './string-pattern';
import {PullRequestMergeTask} from './task';
import {getTargetBranchesForPullRequest} from '../common/targeting/target-label';
import {
assertCorrectBreakingChangeLabeling,
assertMergeReady,
matchesPattern,
assertPendingState,
assertSignedCla,
} from '../common/validation/validations';
Expand Down Expand Up @@ -63,10 +64,6 @@ export async function loadAndValidatePullRequest(

const labels = prData.labels.nodes.map((l) => l.name);

if (!labels.some((name) => matchesPattern(name, config.pullRequest.mergeReadyLabel))) {
return PullRequestFailure.notMergeReady();
}

/** List of parsed commits for all of the commits in the pull request. */
const commitsInPr = prData.commits.nodes.map((n) => parseCommitMessage(n.commit.message));
const githubTargetBranch = prData.baseRefName;
Expand All @@ -80,6 +77,7 @@ export async function loadAndValidatePullRequest(
);

try {
assertMergeReady(prData, config.pullRequest);
assertSignedCla(prData);
assertPendingState(prData);
assertCorrectBreakingChangeLabeling(commitsInPr, labels);
Expand Down
2 changes: 1 addition & 1 deletion ng-dev/pr/merge/strategies/api-merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import {AuthenticatedGitClient} from '../../../utils/git/authenticated-git-clien
import {GithubApiMergeMethod, GithubApiMergeStrategyConfig} from '../../config';
import {PullRequestFailure} from '../../common/validation/failures';
import {PullRequest} from '../pull-request';
import {matchesPattern} from '../string-pattern';

import {MergeStrategy, TEMP_PR_HEAD_BRANCH} from './strategy';
import {GithubApiRequestError} from '../../../utils/git/github';
import {matchesPattern} from '../../common/validation/validations';

/** Type describing the parameters for the Octokit `merge` API endpoint. */
type OctokitMergeParams = RestEndpointMethodTypes['pulls']['merge']['parameters'];
Expand Down
12 changes: 0 additions & 12 deletions ng-dev/pr/merge/string-pattern.ts

This file was deleted.

0 comments on commit cc3a89b

Please sign in to comment.