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

48
dist/cli/index.js vendored
View file

@ -39,13 +39,17 @@ class ArgsParser {
}
parse() {
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),
@ -181,6 +185,7 @@ class CLIArgsParser extends args_parser_1.default {
.version(package_json_1.version)
.description(package_json_1.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")
@ -218,6 +223,7 @@ class CLIArgsParser extends args_parser_1.default {
auth: opts.auth,
pullRequest: opts.pullRequest,
targetBranch: opts.targetBranch,
targetBranchPattern: opts.targetBranchPattern,
folder: opts.folder,
gitClient: opts.gitClient,
gitUser: opts.gitUser,
@ -331,7 +337,18 @@ class PullRequestConfigsParser extends configs_parser_1.default {
throw error;
}
const folder = args.folder ?? this.getDefaultFolder();
const targetBranches = [...new Set((0, args_utils_1.getAsCommaSeparatedList)(args.targetBranch))];
let targetBranches = [];
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((0, args_utils_1.getAsCommaSeparatedList)(args.targetBranch))];
}
const bpBranchNames = [...new Set(args.bpBranchName ? ((0, args_utils_1.getAsCleanedCommaSeparatedList)(args.bpBranchName) ?? []) : [])];
if (bpBranchNames.length > 1 && bpBranchNames.length != targetBranches.length) {
throw new Error(`The number of backport branch names, if provided, must match the number of target branches or just one, provided ${bpBranchNames.length} branch names instead`);
@ -353,6 +370,28 @@ class PullRequestConfigsParser extends configs_parser_1.default {
getDefaultFolder() {
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
*/
getTargetBranchesFromLabels(pattern, labels) {
this.logger.debug(`Extracting branches from [${labels}] using ${pattern}`);
const regExp = new RegExp(pattern);
const branches = [];
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
@ -5891,7 +5930,6 @@ var preservedUrlFields = [
"protocol",
"query",
"search",
"hash",
];
// Create handlers that pass events from native requests
@ -6325,7 +6363,7 @@ RedirectableRequest.prototype._processResponse = function (response) {
redirectUrl.protocol !== "https:" ||
redirectUrl.host !== currentHost &&
!isSubdomain(redirectUrl.host, currentHost)) {
removeMatchingHeaders(/^(?:(?:proxy-)?authorization|cookie)$/i, this._options.headers);
removeMatchingHeaders(/^(?:authorization|cookie)$/i, this._options.headers);
}
// Evaluate the beforeRedirect callback