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
|
|
|
|
*/
|
|
|
|
|
|
|
|
import {MergeConfig, TargetLabel} from './config';
|
|
|
|
import {matchesPattern} from './string-pattern';
|
|
|
|
|
2020-07-24 12:05:51 -04:00
|
|
|
/**
|
|
|
|
* 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) {}
|
|
|
|
}
|
|
|
|
|
2020-05-15 11:19:13 -04:00
|
|
|
/** Gets the target label from the specified pull request labels. */
|
2020-07-24 11:47:30 -04:00
|
|
|
export function getTargetLabelFromPullRequest(
|
|
|
|
config: Pick<MergeConfig, 'labels'>, labels: string[]): TargetLabel|null {
|
2020-05-15 11:19:13 -04:00
|
|
|
for (const label of labels) {
|
|
|
|
const match = config.labels.find(({pattern}) => matchesPattern(label, pattern));
|
|
|
|
if (match !== undefined) {
|
|
|
|
return match;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-07-24 12:05:51 -04:00
|
|
|
/**
|
|
|
|
* 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;
|
2020-05-15 11:19:13 -04:00
|
|
|
}
|