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 84661eac64
commit db5e1de07a
3 changed files with 45 additions and 6 deletions

View File

@ -9,6 +9,22 @@
import {MergeConfig, TargetLabel} from './config';
import {matchesPattern} from './string-pattern';
/**
* Unique error that can be thrown in the merge configuration if an
* invalid branch is targeted.
*/
export class InvalidTargetBranchError {
constructor(public failureMessage: string) {}
}
/**
* Unique error that can be thrown in the merge configuration if an
* invalid label has been applied to a pull request.
*/
export class InvalidTargetLabelError {
constructor(public failureMessage: string) {}
}
/** Gets the target label from the specified pull request labels. */
export function getTargetLabelFromPullRequest(config: MergeConfig, labels: string[]): TargetLabel|
null {
@ -21,8 +37,14 @@ export function getTargetLabelFromPullRequest(config: MergeConfig, labels: strin
return null;
}
/** Gets the branches from the specified target label. */
export function getBranchesFromTargetLabel(
label: TargetLabel, githubTargetBranch: string): string[] {
return typeof label.branches === 'function' ? label.branches(githubTargetBranch) : label.branches;
/**
* Gets the branches from the specified target label.
*
* @throws {InvalidTargetLabelError} Invalid label has been applied to pull request.
* @throws {InvalidTargetBranchError} Invalid Github target branch has been selected.
*/
export async function getBranchesFromTargetLabel(
label: TargetLabel, githubTargetBranch: string): Promise<string[]> {
return typeof label.branches === 'function' ? await label.branches(githubTargetBranch) :
await label.branches;
}