feat(dev-infra): support user-failures when computing branches for target label (#38223)

The merge tool provides a way for configurations to determine the branches
for a label lazily. This is supported because it allows labels to respect
the currently selected base branch through the Github UI. e.g. if `target: label`
is applied on a PR and the PR is based on the patch branch, then the change
could only go into the selected target branch, while if it would be based on
`master`, the change would be cherry-picked to `master` too. This allows for
convenient back-porting of changes if they did not apply cleanly to the primary
development branch (`master`).

We want to expand this function so that it is possible to report failures if an
invalid target label is appplied (e.g. `target: major` not allowed in
some situations), or if the Github base branch is not valid for the given target
label (e.g. if `target: lts` is used, but it's not based on a LTS branch).

PR Close #38223
This commit is contained in:
Paul Gschwendtner
2020-07-24 18:05:51 +02:00
committed by Andrew Kushnir
parent 576e329f33
commit 6f0f0d3ea2
3 changed files with 45 additions and 6 deletions

View File

@ -12,7 +12,7 @@ import {GitClient} from '../../utils/git';
import {PullRequestFailure} from './failures';
import {matchesPattern} from './string-pattern';
import {getBranchesFromTargetLabel, getTargetLabelFromPullRequest} from './target-label';
import {getBranchesFromTargetLabel, getTargetLabelFromPullRequest, InvalidTargetBranchError, InvalidTargetLabelError} from './target-label';
import {PullRequestMergeTask} from './task';
/** Interface that describes a pull request. */
@ -83,6 +83,20 @@ export async function loadAndValidatePullRequest(
labels.some(name => matchesPattern(name, config.commitMessageFixupLabel));
const hasCaretakerNote = !!config.caretakerNoteLabel &&
labels.some(name => matchesPattern(name, config.caretakerNoteLabel!));
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;
}
return {
url: prData.html_url,
@ -92,8 +106,8 @@ export async function loadAndValidatePullRequest(
githubTargetBranch,
needsCommitMessageFixup,
hasCaretakerNote,
targetBranches,
title: prData.title,
targetBranches: getBranchesFromTargetLabel(targetLabel, githubTargetBranch),
commitCount: prData.commits,
};
}