mirror of
https://github.com/kiegroup/git-backporting.git
synced 2025-06-28 05:33:47 +00:00
feat(issue-41): set and inherit labels (#48)
fix https://github.com/kiegroup/git-backporting/issues/41
This commit is contained in:
parent
f923f7f4c2
commit
fcc01673f4
28 changed files with 962 additions and 140 deletions
|
@ -36,6 +36,8 @@ export default abstract 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),
|
||||
};
|
||||
}
|
||||
}
|
|
@ -19,4 +19,32 @@ export function parseArgs(configFileContent: string): Args {
|
|||
export function readConfigFile(pathToFile: string): Args {
|
||||
const asString: string = fs.readFileSync(pathToFile, "utf-8");
|
||||
return parseArgs(asString);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
export function getOrUndefined(value: string): string | undefined {
|
||||
return value !== "" ? value : undefined;
|
||||
}
|
||||
|
||||
// get rid of inner spaces too
|
||||
export function getAsCleanedCommaSeparatedList(value: string): string[] | undefined {
|
||||
// trim the value
|
||||
const trimmed: string = value.trim();
|
||||
return trimmed !== "" ? trimmed.replace(/\s/g, "").split(",") : undefined;
|
||||
}
|
||||
|
||||
// preserve inner spaces
|
||||
export function getAsCommaSeparatedList(value: string): string[] | undefined {
|
||||
// trim the value
|
||||
const trimmed: string = value.trim();
|
||||
return trimmed !== "" ? trimmed.split(",").map(v => v.trim()) : undefined;
|
||||
}
|
||||
|
||||
export function getAsBooleanOrDefault(value: string): boolean | undefined {
|
||||
const trimmed = value.trim();
|
||||
return trimmed !== "" ? trimmed.toLowerCase() === "true" : undefined;
|
||||
}
|
|
@ -16,4 +16,6 @@ export interface Args {
|
|||
reviewers?: string[], // backport pr reviewers
|
||||
assignees?: string[], // backport pr assignees
|
||||
inheritReviewers?: boolean, // if true and reviewers == [] then inherit reviewers from original pr
|
||||
labels?: string[], // backport pr labels
|
||||
inheritLabels?: boolean, // if true inherit labels from original pr
|
||||
}
|
|
@ -2,13 +2,7 @@ import ArgsParser from "@bp/service/args/args-parser";
|
|||
import { Args } from "@bp/service/args/args.types";
|
||||
import { Command } from "commander";
|
||||
import { name, version, description } from "@bp/../package.json";
|
||||
import { readConfigFile } from "@bp/service/args/args-utils";
|
||||
|
||||
function commaSeparatedList(value: string, _prev: unknown): string[] {
|
||||
// remove all whitespaces
|
||||
const cleanedValue: string = value.trim();
|
||||
return cleanedValue !== "" ? cleanedValue.replace(/\s/g, "").split(",") : [];
|
||||
}
|
||||
import { getAsCleanedCommaSeparatedList, getAsCommaSeparatedList, readConfigFile } from "@bp/service/args/args-utils";
|
||||
|
||||
export default class CLIArgsParser extends ArgsParser {
|
||||
|
||||
|
@ -16,21 +10,23 @@ export default class CLIArgsParser extends ArgsParser {
|
|||
return new Command(name)
|
||||
.version(version)
|
||||
.description(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", getAsCleanedCommaSeparatedList)
|
||||
.option("--assignees <assignees>", "comma separated list of assignees for the backporting pull request", 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", 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(): Args {
|
||||
|
@ -58,6 +54,8 @@ export default class CLIArgsParser extends ArgsParser {
|
|||
reviewers: opts.reviewers,
|
||||
assignees: opts.assignees,
|
||||
inheritReviewers: opts.inheritReviewers,
|
||||
labels: opts.labels,
|
||||
inheritLabels: opts.inheritLabels,
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -1,53 +1,34 @@
|
|||
import ArgsParser from "@bp/service/args/args-parser";
|
||||
import { Args } from "@bp/service/args/args.types";
|
||||
import { getInput } from "@actions/core";
|
||||
import { readConfigFile } from "@bp/service/args/args-utils";
|
||||
import { getAsBooleanOrDefault, getAsCleanedCommaSeparatedList, getAsCommaSeparatedList, getOrUndefined, readConfigFile } from "@bp/service/args/args-utils";
|
||||
|
||||
export default class GHAArgsParser extends ArgsParser {
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public getOrUndefined(key: string): string | undefined {
|
||||
const value = getInput(key);
|
||||
return value !== "" ? value : undefined;
|
||||
}
|
||||
|
||||
public getAsCommaSeparatedList(key: string): string[] | undefined {
|
||||
// trim the value
|
||||
const value: string = (getInput(key) ?? "").trim();
|
||||
return value !== "" ? value.replace(/\s/g, "").split(",") : undefined;
|
||||
}
|
||||
|
||||
private getAsBooleanOrDefault(key: string): boolean | undefined {
|
||||
const value = getInput(key).trim();
|
||||
return value !== "" ? value.toLowerCase() === "true" : undefined;
|
||||
}
|
||||
|
||||
readArgs(): Args {
|
||||
const configFile = this.getOrUndefined("config-file");
|
||||
const configFile = getOrUndefined(getInput("config-file"));
|
||||
|
||||
let args: Args;
|
||||
if (configFile) {
|
||||
args = readConfigFile(configFile);
|
||||
} else {
|
||||
args = {
|
||||
dryRun: this.getAsBooleanOrDefault("dry-run"),
|
||||
auth: this.getOrUndefined("auth"),
|
||||
dryRun: getAsBooleanOrDefault(getInput("dry-run")),
|
||||
auth: getOrUndefined(getInput("auth")),
|
||||
pullRequest: getInput("pull-request"),
|
||||
targetBranch: getInput("target-branch"),
|
||||
folder: this.getOrUndefined("folder"),
|
||||
gitUser: this.getOrUndefined("git-user"),
|
||||
gitEmail: this.getOrUndefined("git-email"),
|
||||
title: this.getOrUndefined("title"),
|
||||
body: this.getOrUndefined("body"),
|
||||
bodyPrefix: this.getOrUndefined("body-prefix"),
|
||||
bpBranchName: this.getOrUndefined("bp-branch-name"),
|
||||
reviewers: this.getAsCommaSeparatedList("reviewers"),
|
||||
assignees: this.getAsCommaSeparatedList("assignees"),
|
||||
inheritReviewers: !this.getAsBooleanOrDefault("no-inherit-reviewers"),
|
||||
folder: getOrUndefined(getInput("folder")),
|
||||
gitUser: getOrUndefined(getInput("git-user")),
|
||||
gitEmail: getOrUndefined(getInput("git-email")),
|
||||
title: getOrUndefined(getInput("title")),
|
||||
body: getOrUndefined(getInput("body")),
|
||||
bodyPrefix: getOrUndefined(getInput("body-prefix")),
|
||||
bpBranchName: getOrUndefined(getInput("bp-branch-name")),
|
||||
reviewers: getAsCleanedCommaSeparatedList(getInput("reviewers")),
|
||||
assignees: getAsCleanedCommaSeparatedList(getInput("assignees")),
|
||||
inheritReviewers: !getAsBooleanOrDefault(getInput("no-inherit-reviewers")),
|
||||
labels: getAsCommaSeparatedList(getInput("labels")),
|
||||
inheritLabels: getAsBooleanOrDefault(getInput("inherit-labels")),
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -63,12 +63,18 @@ export default class PullRequestConfigsParser extends ConfigsParser {
|
|||
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,
|
||||
|
|
|
@ -10,6 +10,7 @@ export interface GitPullRequest {
|
|||
body: string,
|
||||
reviewers: string[],
|
||||
assignees: string[],
|
||||
labels: string[],
|
||||
targetRepo: GitRepository,
|
||||
sourceRepo: GitRepository,
|
||||
nCommits?: number, // number of commits in the pr
|
||||
|
@ -32,6 +33,7 @@ export interface BackportPullRequest {
|
|||
body: string, // pr body
|
||||
reviewers: string[], // pr list of reviewers
|
||||
assignees: string[], // pr list of assignees
|
||||
labels: string[], // pr list of assigned labels
|
||||
branchName?: string,
|
||||
}
|
||||
|
||||
|
|
|
@ -59,13 +59,26 @@ export default class GitHubClient implements GitClient {
|
|||
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 as PullRequest).number,
|
||||
labels: backport.labels,
|
||||
});
|
||||
} catch (error) {
|
||||
this.logger.error(`Error setting labels: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
if (backport.reviewers.length > 0) {
|
||||
try {
|
||||
await this.octokit.pulls.requestReviewers({
|
||||
|
|
|
@ -26,6 +26,7 @@ export default class GitHubMapper implements GitResponseMapper<PullRequest, "ope
|
|||
mergedBy: pr.merged_by?.login,
|
||||
reviewers: pr.requested_reviewers.filter(r => "login" in r).map((r => (r as User)?.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,
|
||||
|
|
|
@ -72,6 +72,18 @@ export default class GitLabClient implements GitClient {
|
|||
|
||||
const mr = data as MergeRequestSchema;
|
||||
|
||||
// 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: number[] = [];
|
||||
for(const r of backport.reviewers) {
|
||||
|
|
|
@ -25,7 +25,6 @@ export default class GitLabMapper implements GitResponseMapper<MergeRequestSchem
|
|||
}
|
||||
|
||||
async mapPullRequest(mr: MergeRequestSchema): Promise<GitPullRequest> {
|
||||
// throw new Error("Method not implemented.");
|
||||
return {
|
||||
number: mr.iid,
|
||||
author: mr.author.username,
|
||||
|
@ -38,6 +37,7 @@ export default class GitLabMapper implements GitResponseMapper<MergeRequestSchem
|
|||
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, // info not present on mr
|
||||
|
|
|
@ -100,6 +100,7 @@ export default class Runner {
|
|||
body: backportPR.body,
|
||||
reviewers: backportPR.reviewers,
|
||||
assignees: backportPR.assignees,
|
||||
labels: backportPR.labels,
|
||||
};
|
||||
|
||||
if (!configs.dryRun) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue