Skip to content

Commit

Permalink
feat(issue-41): set and inherit labels (#48)
Browse files Browse the repository at this point in the history
fix #41
  • Loading branch information
lampajr authored Jul 10, 2023
1 parent f923f7f commit fcc0167
Show file tree
Hide file tree
Showing 28 changed files with 962 additions and 140 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<a href="https://github.com/kiegroup/git-backporting">
<img alt="ci checks status" src="https://github.com/kiegroup/git-backporting/actions/workflows/ci.yml/badge.svg">
</a>
<a href="https://badge.fury.io/js/@kie%git-backporting">
<a href="https://badge.fury.io/js/@kie%2Fgit-backporting">
<img alt="npm version" src="https://badge.fury.io/js/@kie%2Fgit-backporting.svg">
</a>
</p>
Expand Down Expand Up @@ -94,6 +94,8 @@ This tool comes with some inputs that allow users to override the default behavi
| Assignees | --assignes | N | Backporting pull request comma-separated assignees list | [] |
| No Reviewers Inheritance | --no-inherit-reviewers | N | Considered only if reviewers is empty, if true keep reviewers as empty list, otherwise inherit from original pull request | false |
| Backport Branch Name | --bp-branch-name | N | Name of the backporting pull request branch | bp-{target-branch}-{sha} |
| Labels | --labels | N | Provide custom labels to be added to the backporting pull request | [] |
| Inherit labels | --inherit-labels | N | If enabled inherit lables from the original pull request | false |
| Dry Run | -d, --dry-run | N | If enabled the tool does not push nor create anything remotely, use this to skip PR creation | false |

> **NOTE**: `pull request` and `target branch` are *mandatory*, they must be provided as CLI options or as part of the configuration file (if used).
Expand Down Expand Up @@ -280,6 +282,8 @@ Every change must be submitted through a *GitHub* pull request (PR). Backporting

> **Note**: you don't need to take care about typescript compilation and minifycation, there are automated [git hooks](./.husky) taking care of that!

**Hint**: if you are still in a `work in progress` branch and you want to push your changes remotely, consider adding `--no-verify` for both `commit` and `push`, e.g., `git push origin <feat-branch> --no-verify`

## License

Backporting (BPer) open source project is licensed under the [MIT](./LICENSE) license.
7 changes: 7 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ inputs:
description: "Considered only if reviewers is empty, if true keep reviewers as empty list, otherwise inherit from original pull request"
required: false
default: "false"
labels:
description: "Comma separated list of labels to be assigned to the backported pull request"
required: false
inherit-labels:
description: "If true the backported pull request will inherit labels from the original one"
required: false
default: "false"

runs:
using: node16
Expand Down
103 changes: 82 additions & 21 deletions dist/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
};
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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;


/***/ }),
Expand All @@ -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()
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -924,7 +984,6 @@ class GitLabMapper {
}
}
async mapPullRequest(mr) {
// throw new Error("Method not implemented.");
return {
number: mr.iid,
author: mr.author.username,
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit fcc0167

Please sign in to comment.