2020-05-15 11:19:13 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google LLC All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2021-04-06 10:43:21 -04:00
|
|
|
import {params, types as graphQLTypes} from 'typed-graphqlify';
|
2021-04-06 10:49:49 -04:00
|
|
|
import {parseCommitMessage} from '../../commit-message/parse';
|
|
|
|
import {red, warn} from '../../utils/console';
|
2020-05-15 11:19:13 -04:00
|
|
|
|
2020-10-01 19:06:56 -04:00
|
|
|
import {GitClient} from '../../utils/git/index';
|
2021-04-06 10:43:21 -04:00
|
|
|
import {getPr} from '../../utils/github';
|
2020-12-16 13:02:48 -05:00
|
|
|
import {TargetLabel} from './config';
|
2020-05-27 18:39:31 -04:00
|
|
|
|
2020-05-15 11:19:13 -04:00
|
|
|
import {PullRequestFailure} from './failures';
|
|
|
|
import {matchesPattern} from './string-pattern';
|
2020-07-24 12:05:51 -04:00
|
|
|
import {getBranchesFromTargetLabel, getTargetLabelFromPullRequest, InvalidTargetBranchError, InvalidTargetLabelError} from './target-label';
|
2020-05-15 11:19:13 -04:00
|
|
|
import {PullRequestMergeTask} from './task';
|
|
|
|
|
|
|
|
/** Interface that describes a pull request. */
|
|
|
|
export interface PullRequest {
|
2020-06-15 18:20:36 -04:00
|
|
|
/** URL to the pull request. */
|
|
|
|
url: string;
|
2020-05-15 11:19:13 -04:00
|
|
|
/** Number of the pull request. */
|
|
|
|
prNumber: number;
|
|
|
|
/** Title of the pull request. */
|
|
|
|
title: string;
|
|
|
|
/** Labels applied to the pull request. */
|
|
|
|
labels: string[];
|
|
|
|
/** List of branches this PR should be merged into. */
|
|
|
|
targetBranches: string[];
|
|
|
|
/** Branch that the PR targets in the Github UI. */
|
|
|
|
githubTargetBranch: string;
|
|
|
|
/** Count of commits in this pull request. */
|
|
|
|
commitCount: number;
|
|
|
|
/** Optional SHA that this pull request needs to be based on. */
|
|
|
|
requiredBaseSha?: string;
|
|
|
|
/** Whether the pull request commit message fixup. */
|
|
|
|
needsCommitMessageFixup: boolean;
|
2020-06-15 18:20:36 -04:00
|
|
|
/** Whether the pull request has a caretaker note. */
|
|
|
|
hasCaretakerNote: boolean;
|
2020-05-15 11:19:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads and validates the specified pull request against the given configuration.
|
|
|
|
* If the pull requests fails, a pull request failure is returned.
|
|
|
|
*/
|
|
|
|
export async function loadAndValidatePullRequest(
|
|
|
|
{git, config}: PullRequestMergeTask, prNumber: number,
|
|
|
|
ignoreNonFatalFailures = false): Promise<PullRequest|PullRequestFailure> {
|
|
|
|
const prData = await fetchPullRequestFromGithub(git, prNumber);
|
|
|
|
|
|
|
|
if (prData === null) {
|
|
|
|
return PullRequestFailure.notFound();
|
|
|
|
}
|
|
|
|
|
2021-04-06 10:43:21 -04:00
|
|
|
const labels = prData.labels.nodes.map(l => l.name);
|
2020-05-15 11:19:13 -04:00
|
|
|
|
|
|
|
if (!labels.some(name => matchesPattern(name, config.mergeReadyLabel))) {
|
|
|
|
return PullRequestFailure.notMergeReady();
|
|
|
|
}
|
|
|
|
if (!labels.some(name => matchesPattern(name, config.claSignedLabel))) {
|
|
|
|
return PullRequestFailure.claUnsigned();
|
|
|
|
}
|
|
|
|
|
2020-12-16 13:02:48 -05:00
|
|
|
let targetLabel: TargetLabel;
|
|
|
|
try {
|
|
|
|
targetLabel = getTargetLabelFromPullRequest(config, labels);
|
|
|
|
} catch (error) {
|
|
|
|
if (error instanceof InvalidTargetLabelError) {
|
|
|
|
return new PullRequestFailure(error.failureMessage);
|
|
|
|
}
|
|
|
|
throw error;
|
2020-05-15 11:19:13 -04:00
|
|
|
}
|
|
|
|
|
2021-04-06 10:49:49 -04:00
|
|
|
try {
|
|
|
|
assertCorrectTargetForChanges(prData.commits.nodes.map(n => n.commit.message), targetLabel);
|
|
|
|
} catch (error) {
|
|
|
|
return error;
|
|
|
|
}
|
2020-05-15 11:19:13 -04:00
|
|
|
|
2021-04-06 10:43:21 -04:00
|
|
|
const state = prData.commits.nodes.slice(-1)[0].commit.status.state;
|
|
|
|
if (state === 'FAILURE' && !ignoreNonFatalFailures) {
|
2020-05-15 11:19:13 -04:00
|
|
|
return PullRequestFailure.failingCiJobs();
|
|
|
|
}
|
2021-04-06 10:43:21 -04:00
|
|
|
if (state === 'PENDING' && !ignoreNonFatalFailures) {
|
2020-05-15 11:19:13 -04:00
|
|
|
return PullRequestFailure.pendingCiJobs();
|
|
|
|
}
|
|
|
|
|
2021-04-06 10:43:21 -04:00
|
|
|
const githubTargetBranch = prData.baseRefOid;
|
2020-05-15 11:19:13 -04:00
|
|
|
const requiredBaseSha =
|
|
|
|
config.requiredBaseCommits && config.requiredBaseCommits[githubTargetBranch];
|
|
|
|
const needsCommitMessageFixup = !!config.commitMessageFixupLabel &&
|
|
|
|
labels.some(name => matchesPattern(name, config.commitMessageFixupLabel));
|
2020-06-15 18:20:36 -04:00
|
|
|
const hasCaretakerNote = !!config.caretakerNoteLabel &&
|
|
|
|
labels.some(name => matchesPattern(name, config.caretakerNoteLabel!));
|
2020-07-24 12:05:51 -04:00
|
|
|
let targetBranches: string[];
|
|
|
|
|
|
|
|
// If branches are determined for a given target label, capture errors that are
|
|
|
|
// thrown as part of branch computation. This is expected because a merge configuration
|
|
|
|
// can lazily compute branches for a target label and throw. e.g. if an invalid target
|
|
|
|
// label is applied, we want to exit the script gracefully with an error message.
|
|
|
|
try {
|
|
|
|
targetBranches = await getBranchesFromTargetLabel(targetLabel, githubTargetBranch);
|
|
|
|
} catch (error) {
|
|
|
|
if (error instanceof InvalidTargetBranchError || error instanceof InvalidTargetLabelError) {
|
|
|
|
return new PullRequestFailure(error.failureMessage);
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
2020-05-15 11:19:13 -04:00
|
|
|
|
|
|
|
return {
|
2021-04-06 10:43:21 -04:00
|
|
|
url: prData.url,
|
2020-05-15 11:19:13 -04:00
|
|
|
prNumber,
|
|
|
|
labels,
|
|
|
|
requiredBaseSha,
|
|
|
|
githubTargetBranch,
|
|
|
|
needsCommitMessageFixup,
|
2020-06-15 18:20:36 -04:00
|
|
|
hasCaretakerNote,
|
2020-07-24 12:05:51 -04:00
|
|
|
targetBranches,
|
2020-05-15 11:19:13 -04:00
|
|
|
title: prData.title,
|
2021-04-06 10:43:21 -04:00
|
|
|
commitCount: prData.commits.nodes.length,
|
2020-05-15 11:19:13 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-04-06 10:43:21 -04:00
|
|
|
/* GraphQL schema for the response body the requested PR. */
|
|
|
|
const PR_SCHEMA = {
|
|
|
|
url: graphQLTypes.string,
|
|
|
|
number: graphQLTypes.number,
|
|
|
|
commits: params({first: 100}, {
|
|
|
|
nodes: [{
|
|
|
|
commit: {
|
|
|
|
status: {
|
2021-04-06 10:49:49 -04:00
|
|
|
state: graphQLTypes.oneOf(['FAILURE', 'PENDING', 'SUCCESS'] as const),
|
2021-04-06 10:43:21 -04:00
|
|
|
},
|
|
|
|
message: graphQLTypes.string,
|
|
|
|
},
|
|
|
|
}],
|
|
|
|
}),
|
|
|
|
baseRefOid: graphQLTypes.string,
|
|
|
|
title: graphQLTypes.string,
|
|
|
|
labels: params({first: 100}, {
|
|
|
|
nodes: [{
|
|
|
|
name: graphQLTypes.string,
|
|
|
|
}]
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-05-15 11:19:13 -04:00
|
|
|
/** Fetches a pull request from Github. Returns null if an error occurred. */
|
|
|
|
async function fetchPullRequestFromGithub(
|
2021-04-06 10:43:21 -04:00
|
|
|
git: GitClient, prNumber: number): Promise<typeof PR_SCHEMA|null> {
|
2020-05-15 11:19:13 -04:00
|
|
|
try {
|
2021-04-06 10:43:21 -04:00
|
|
|
return await getPr(PR_SCHEMA, prNumber, git);
|
2020-05-15 11:19:13 -04:00
|
|
|
} catch (e) {
|
|
|
|
// If the pull request could not be found, we want to return `null` so
|
|
|
|
// that the error can be handled gracefully.
|
|
|
|
if (e.status === 404) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Whether the specified value resolves to a pull request. */
|
|
|
|
export function isPullRequest(v: PullRequestFailure|PullRequest): v is PullRequest {
|
|
|
|
return (v as PullRequest).targetBranches !== undefined;
|
|
|
|
}
|
2021-04-06 10:49:49 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Assert the commits provided are allowed to merge to the provided target label, throwing a
|
|
|
|
* PullRequestFailure otherwise.
|
|
|
|
*/
|
|
|
|
function assertCorrectTargetForChanges(rawCommits: string[], label: TargetLabel) {
|
|
|
|
/** List of ParsedCommits for all of the commits in the pull request. */
|
|
|
|
const commits = rawCommits.map(parseCommitMessage);
|
|
|
|
switch (label.pattern) {
|
|
|
|
case 'target: major':
|
|
|
|
break;
|
|
|
|
case 'target: minor':
|
|
|
|
// Check if any commits in the PR contains a breaking change.
|
|
|
|
if (commits.some(commit => commit.breakingChanges.length !== 0)) {
|
|
|
|
throw PullRequestFailure.hasBreakingChanges(label);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'target: patch':
|
|
|
|
case 'target: lts':
|
|
|
|
// Check if any commits in the PR contains a breaking change.
|
|
|
|
if (commits.some(commit => commit.breakingChanges.length !== 0)) {
|
|
|
|
throw PullRequestFailure.hasBreakingChanges(label);
|
|
|
|
}
|
|
|
|
// Check if any commits in the PR contains a commit type of "feat".
|
|
|
|
if (commits.some(commit => commit.type === 'feat')) {
|
|
|
|
throw PullRequestFailure.hasFeatureCommits(label);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
warn(red('WARNING: Unable to confirm all commits in the PR are eligible to be merged'));
|
|
|
|
warn(red(`into the target branch: ${label.pattern}`));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|