feat(gh75): extract target branched from pr labels (#112)

This commit is contained in:
Andrea Lamparelli 2024-03-30 19:19:17 +01:00 committed by GitHub
parent b2e2e271b9
commit 53cc505f17
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
20 changed files with 523 additions and 83 deletions

View file

@ -16,14 +16,18 @@ export default abstract class ArgsParser {
public parse(): Args {
const args = this.readArgs();
if (!args.pullRequest) {
throw new Error("Missing option: pull request must be provided");
}
// validate and fill with defaults
if (!args.pullRequest || !args.targetBranch || args.targetBranch.trim().length == 0) {
throw new Error("Missing option: pull request and target branches must be provided");
if ((!args.targetBranch || args.targetBranch.trim().length == 0) && !args.targetBranchPattern) {
throw new Error("Missing option: target branch(es) or target regular expression must be provided");
}
return {
pullRequest: args.pullRequest,
targetBranch: args.targetBranch,
targetBranchPattern: args.targetBranchPattern,
dryRun: this.getOrDefault(args.dryRun, false),
auth: this.getOrDefault(args.auth),
folder: this.getOrDefault(args.folder),

View file

@ -3,7 +3,8 @@
*/
export interface Args {
// NOTE: keep targetBranch as singular and of type string for backward compatibilities
targetBranch: string, // comma separated list of branches on the target repo where the change should be backported to
targetBranch?: string, // comma separated list of branches on the target repo where the change should be backported to
targetBranchPattern?: string, // regular expression to extract target branch(es) from pull request labels
pullRequest: string, // url of the pull request to backport
dryRun?: boolean, // if enabled do not push anything remotely
auth?: string, // git service auth, like github token

View file

@ -11,6 +11,7 @@ export default class CLIArgsParser extends ArgsParser {
.version(version)
.description(description)
.option("-tb, --target-branch <branches>", "comma separated list of branches where changes must be backported to")
.option("-tbp, --target-branch-pattern <pattern>", "regular expression pattern to extract target branch(es) from pr labels, the branches will be extracted from the pattern's required `target` named capturing group")
.option("-pr, --pull-request <pr-url>", "pull request url, e.g., https://github.com/kiegroup/git-backporting/pull/1")
.option("-d, --dry-run", "if enabled the tool does not create any pull request nor push anything remotely")
.option("-a, --auth <auth>", "git authentication string, if not provided fallback by looking for existing env variables like GITHUB_TOKEN")
@ -49,6 +50,7 @@ export default class CLIArgsParser extends ArgsParser {
auth: opts.auth,
pullRequest: opts.pullRequest,
targetBranch: opts.targetBranch,
targetBranchPattern: opts.targetBranchPattern,
folder: opts.folder,
gitClient: opts.gitClient,
gitUser: opts.gitUser,

View file

@ -16,7 +16,8 @@ export default class GHAArgsParser extends ArgsParser {
dryRun: getAsBooleanOrDefault(getInput("dry-run")),
auth: getOrUndefined(getInput("auth")),
pullRequest: getInput("pull-request"),
targetBranch: getInput("target-branch"),
targetBranch: getOrUndefined(getInput("target-branch")),
targetBranchPattern: getOrUndefined(getInput("target-reg-exp")),
folder: getOrUndefined(getInput("folder")),
gitClient: getOrUndefined(getInput("git-client")),
gitUser: getOrUndefined(getInput("git-user")),

View file

@ -26,7 +26,17 @@ export default class PullRequestConfigsParser extends ConfigsParser {
const folder: string = args.folder ?? this.getDefaultFolder();
const targetBranches: string[] = [...new Set(getAsCommaSeparatedList(args.targetBranch)!)];
let targetBranches: string[] = [];
if (args.targetBranchPattern) {
// parse labels to extract target branch(es)
targetBranches = this.getTargetBranchesFromLabels(args.targetBranchPattern, pr.labels);
if (targetBranches.length === 0) {
throw new Error(`Unable to extract target branches with regular expression "${args.targetBranchPattern}"`);
}
} else {
// target branch must be provided if targetRegExp is missing
targetBranches = [...new Set(getAsCommaSeparatedList(args.targetBranch!)!)];
}
const bpBranchNames: string[] = [...new Set(args.bpBranchName ? (getAsCleanedCommaSeparatedList(args.bpBranchName) ?? []) : [])];
if (bpBranchNames.length > 1 && bpBranchNames.length != targetBranches.length) {
@ -52,6 +62,33 @@ export default class PullRequestConfigsParser extends ConfigsParser {
return "bp";
}
/**
* Parse the provided labels and return a list of target branches
* obtained by applying the provided pattern as regular expression extractor
* @param pattern reg exp pattern to extract target branch from label name
* @param labels list of labels to check
* @returns list of target branches
*/
private getTargetBranchesFromLabels(pattern: string, labels: string[]): string[] {
this.logger.debug(`Extracting branches from [${labels}] using ${pattern}`);
const regExp = new RegExp(pattern);
const branches: string[] = [];
for (const l of labels) {
const result = regExp.exec(l);
if (result?.groups) {
const { target } = result.groups;
if (target){
branches.push(target);
}
}
}
return [...new Set(branches)];
}
/**
* Create a backport pull request starting from the target branch and
* the original pr to be backported