feat(issue-41): set and inherit labels (#48)

fix https://github.com/kiegroup/git-backporting/issues/41
This commit is contained in:
Andrea Lamparelli 2023-07-10 08:49:11 +02:00 committed by GitHub
parent f923f7f4c2
commit fcc01673f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
28 changed files with 962 additions and 140 deletions

103
dist/cli/index.js vendored
View file

@ -58,6 +58,8 @@ class ArgsParser {
reviewers: this.getOrDefault(args.reviewers, []),
assignees: this.getOrDefault(args.assignees, []),
inheritReviewers: this.getOrDefault(args.inheritReviewers, true),
labels: this.getOrDefault(args.labels, []),
inheritLabels: this.getOrDefault(args.inheritLabels, false),
};
}
}
@ -95,7 +97,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
return result;
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.readConfigFile = exports.parseArgs = void 0;
exports.getAsBooleanOrDefault = exports.getAsCommaSeparatedList = exports.getAsCleanedCommaSeparatedList = exports.getOrUndefined = exports.readConfigFile = exports.parseArgs = void 0;
const fs = __importStar(__nccwpck_require__(7147));
/**
* Parse the input configuation string as json object and
@ -117,6 +119,34 @@ function readConfigFile(pathToFile) {
return parseArgs(asString);
}
exports.readConfigFile = readConfigFile;
/**
* Return the input only if it is not a blank or null string, otherwise returns undefined
* @param key input key
* @returns the value or undefined
*/
function getOrUndefined(value) {
return value !== "" ? value : undefined;
}
exports.getOrUndefined = getOrUndefined;
// get rid of inner spaces too
function getAsCleanedCommaSeparatedList(value) {
// trim the value
const trimmed = value.trim();
return trimmed !== "" ? trimmed.replace(/\s/g, "").split(",") : undefined;
}
exports.getAsCleanedCommaSeparatedList = getAsCleanedCommaSeparatedList;
// preserve inner spaces
function getAsCommaSeparatedList(value) {
// trim the value
const trimmed = value.trim();
return trimmed !== "" ? trimmed.split(",").map(v => v.trim()) : undefined;
}
exports.getAsCommaSeparatedList = getAsCommaSeparatedList;
function getAsBooleanOrDefault(value) {
const trimmed = value.trim();
return trimmed !== "" ? trimmed.toLowerCase() === "true" : undefined;
}
exports.getAsBooleanOrDefault = getAsBooleanOrDefault;
/***/ }),
@ -134,31 +164,28 @@ const args_parser_1 = __importDefault(__nccwpck_require__(3025));
const commander_1 = __nccwpck_require__(4379);
const package_json_1 = __nccwpck_require__(6625);
const args_utils_1 = __nccwpck_require__(8048);
function commaSeparatedList(value, _prev) {
// remove all whitespaces
const cleanedValue = value.trim();
return cleanedValue !== "" ? cleanedValue.replace(/\s/g, "").split(",") : [];
}
class CLIArgsParser extends args_parser_1.default {
getCommand() {
return new commander_1.Command(package_json_1.name)
.version(package_json_1.version)
.description(package_json_1.description)
.option("-tb, --target-branch <branch>", "branch where changes must be backported to.")
.option("-pr, --pull-request <pr-url>", "pull request url, e.g., https://github.com/kiegroup/git-backporting/pull/1.")
.option("-tb, --target-branch <branch>", "branch where changes must be backported to")
.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 service authentication string, e.g., github token.")
.option("-gu, --git-user <git-user>", "local git user name, default is 'GitHub'.")
.option("-ge, --git-email <git-email>", "local git user email, default is 'noreply@github.com'.")
.option("-f, --folder <folder>", "local folder where the repo will be checked out, e.g., /tmp/folder.")
.option("--title <bp-title>", "backport pr title, default original pr title prefixed by target branch.")
.option("--body <bp-body>", "backport pr title, default original pr body prefixed by bodyPrefix.")
.option("--body-prefix <bp-body-prefix>", "backport pr body prefix, default `backport <original-pr-link>`.")
.option("--bp-branch-name <bp-branch-name>", "backport pr branch name, default auto-generated by the commit.")
.option("--reviewers <reviewers>", "comma separated list of reviewers for the backporting pull request.", commaSeparatedList)
.option("--assignees <assignees>", "comma separated list of assignees for the backporting pull request.", commaSeparatedList)
.option("-a, --auth <auth>", "git service authentication string, e.g., github token")
.option("-gu, --git-user <git-user>", "local git user name, default is 'GitHub'")
.option("-ge, --git-email <git-email>", "local git user email, default is 'noreply@github.com'")
.option("-f, --folder <folder>", "local folder where the repo will be checked out, e.g., /tmp/folder")
.option("--title <bp-title>", "backport pr title, default original pr title prefixed by target branch")
.option("--body <bp-body>", "backport pr title, default original pr body prefixed by bodyPrefix")
.option("--body-prefix <bp-body-prefix>", "backport pr body prefix, default `backport <original-pr-link>`")
.option("--bp-branch-name <bp-branch-name>", "backport pr branch name, default auto-generated by the commit")
.option("--reviewers <reviewers>", "comma separated list of reviewers for the backporting pull request", args_utils_1.getAsCleanedCommaSeparatedList)
.option("--assignees <assignees>", "comma separated list of assignees for the backporting pull request", args_utils_1.getAsCleanedCommaSeparatedList)
.option("--no-inherit-reviewers", "if provided and reviewers option is empty then inherit them from original pull request")
.option("-cf, --config-file <config-file>", "configuration file containing all valid options, the json must match Args interface.");
.option("--labels <labels>", "comma separated list of labels to be assigned to the backported pull request", args_utils_1.getAsCommaSeparatedList)
.option("--inherit-labels", "if true the backported pull request will inherit labels from the original one")
.option("-cf, --config-file <config-file>", "configuration file containing all valid options, the json must match Args interface");
}
readArgs() {
const opts = this.getCommand()
@ -185,6 +212,8 @@ class CLIArgsParser extends args_parser_1.default {
reviewers: opts.reviewers,
assignees: opts.assignees,
inheritReviewers: opts.inheritReviewers,
labels: opts.labels,
inheritLabels: opts.inheritLabels,
};
}
return args;
@ -292,12 +321,17 @@ class PullRequestConfigsParser extends configs_parser_1.default {
}
const bodyPrefix = args.bodyPrefix ?? `**Backport:** ${originalPullRequest.htmlUrl}\r\n\r\n`;
const body = args.body ?? `${originalPullRequest.body}`;
const labels = args.labels ?? [];
if (args.inheritLabels) {
labels.push(...originalPullRequest.labels);
}
return {
author: args.gitUser ?? this.gitClient.getDefaultGitUser(),
title: args.title ?? `[${args.targetBranch}] ${originalPullRequest.title}`,
body: `${bodyPrefix}${body}`,
reviewers: [...new Set(reviewers)],
assignees: [...new Set(args.assignees)],
labels: [...new Set(labels)],
targetRepo: originalPullRequest.targetRepo,
sourceRepo: originalPullRequest.targetRepo,
branchName: args.bpBranchName,
@ -608,11 +642,24 @@ class GitHubClient {
head: backport.head,
base: backport.base,
title: backport.title,
body: backport.body
body: backport.body,
});
if (!data) {
throw new Error("Pull request creation failed");
}
if (backport.labels.length > 0) {
try {
await this.octokit.issues.addLabels({
owner: backport.owner,
repo: backport.repo,
issue_number: data.number,
labels: backport.labels,
});
}
catch (error) {
this.logger.error(`Error setting labels: ${error}`);
}
}
if (backport.reviewers.length > 0) {
try {
await this.octokit.pulls.requestReviewers({
@ -690,6 +737,7 @@ class GitHubMapper {
mergedBy: pr.merged_by?.login,
reviewers: pr.requested_reviewers.filter(r => "login" in r).map((r => r?.login)),
assignees: pr.assignees.filter(r => "login" in r).map(r => r.login),
labels: pr.labels.map(l => l.name),
sourceRepo: await this.mapSourceRepo(pr),
targetRepo: await this.mapTargetRepo(pr),
nCommits: pr.commits,
@ -810,6 +858,18 @@ class GitLabClient {
assignee_ids: [],
});
const mr = data;
// labels
if (backport.labels.length > 0) {
try {
this.logger.info("Setting labels: " + backport.labels);
await this.client.put(`/projects/${projectId}/merge_requests/${mr.iid}`, {
labels: backport.labels.join(","),
});
}
catch (error) {
this.logger.warn("Failure trying to update labels. " + error);
}
}
// reviewers
const reviewerIds = [];
for (const r of backport.reviewers) {
@ -924,7 +984,6 @@ class GitLabMapper {
}
}
async mapPullRequest(mr) {
// throw new Error("Method not implemented.");
return {
number: mr.iid,
author: mr.author.username,
@ -937,6 +996,7 @@ class GitLabMapper {
mergedBy: mr.merged_by?.username,
reviewers: mr.reviewers?.map((r => r.username)) ?? [],
assignees: mr.assignees?.map((r => r.username)) ?? [],
labels: mr.labels ?? [],
sourceRepo: await this.mapSourceRepo(mr),
targetRepo: await this.mapTargetRepo(mr),
nCommits: 1,
@ -1151,6 +1211,7 @@ class Runner {
body: backportPR.body,
reviewers: backportPR.reviewers,
assignees: backportPR.assignees,
labels: backportPR.labels,
};
if (!configs.dryRun) {
// 8. push the new branch to origin