mirror of
https://code.forgejo.org/actions/git-backporting.git
synced 2025-05-23 05:59:14 -04:00
feat: override backporting pr fields (#38)
* feat: override local git user config * feat(issue-32): override reviewers and assignees
This commit is contained in:
parent
22bec0c537
commit
a32e8cd34c
21 changed files with 671 additions and 107 deletions
77
dist/cli/index.js
vendored
77
dist/cli/index.js
vendored
|
@ -30,20 +30,30 @@ runner.run();
|
|||
Object.defineProperty(exports, "__esModule", ({ value: true }));
|
||||
const commander_1 = __nccwpck_require__(4379);
|
||||
const package_json_1 = __nccwpck_require__(6625);
|
||||
function commaSeparatedList(value, _prev) {
|
||||
// remove all whitespaces
|
||||
const cleanedValue = value.trim();
|
||||
return cleanedValue !== "" ? cleanedValue.replace(/\s/g, "").split(",") : [];
|
||||
}
|
||||
class CLIArgsParser {
|
||||
getCommand() {
|
||||
return new commander_1.Command(package_json_1.name)
|
||||
.version(package_json_1.version)
|
||||
.description(package_json_1.description)
|
||||
.requiredOption("-tb, --target-branch <branch>", "branch where changes must be backported to.")
|
||||
.requiredOption("-pr, --pull-request <pr url>", "pull request url, e.g., https://github.com/lampajr/backporting/pull/1.")
|
||||
.requiredOption("-pr, --pull-request <pr-url>", "pull request url, e.g., https://github.com/lampajr/backporting/pull/1.")
|
||||
.option("-d, --dry-run", "if enabled the tool does not create any pull request nor push anything remotely", false)
|
||||
.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'.", "GitHub")
|
||||
.option("-ge, --git-email <git-email>", "local git user email, default is 'noreply@github.com'.", "noreply@github.com")
|
||||
.option("-f, --folder <folder>", "local folder where the repo will be checked out, e.g., /tmp/folder.", undefined)
|
||||
.option("--title <folder>", "backport pr title, default original pr title prefixed by target branch.", undefined)
|
||||
.option("--body <folder>", "backport pr title, default original pr body prefixed by bodyPrefix.", undefined)
|
||||
.option("--body-prefix <folder>", "backport pr body prefix, default `backport <original-pr-link>`.", undefined)
|
||||
.option("--bp-branch-name <folder>", "backport pr branch name, default auto-generated by the commit.", undefined);
|
||||
.option("--title <bp-title>", "backport pr title, default original pr title prefixed by target branch.", undefined)
|
||||
.option("--body <bp-body>", "backport pr title, default original pr body prefixed by bodyPrefix.", undefined)
|
||||
.option("--body-prefix <bp-body-prefix>", "backport pr body prefix, default `backport <original-pr-link>`.", undefined)
|
||||
.option("--bp-branch-name <bp-branch-name>", "backport pr branch name, default auto-generated by the commit.", undefined)
|
||||
.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("--no-inherit-reviewers", "if provided and reviewers option is empty then inherit them from original pull request", true);
|
||||
}
|
||||
parse() {
|
||||
const opts = this.getCommand()
|
||||
|
@ -55,10 +65,15 @@ class CLIArgsParser {
|
|||
pullRequest: opts.pullRequest,
|
||||
targetBranch: opts.targetBranch,
|
||||
folder: opts.folder,
|
||||
gitUser: opts.gitUser,
|
||||
gitEmail: opts.gitEmail,
|
||||
title: opts.title,
|
||||
body: opts.body,
|
||||
bodyPrefix: opts.bodyPrefix,
|
||||
bpBranchName: opts.bpBranchName,
|
||||
reviewers: opts.reviewers,
|
||||
assignees: opts.assignees,
|
||||
inheritReviewers: opts.inheritReviewers,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -126,11 +141,14 @@ class PullRequestConfigsParser extends configs_parser_1.default {
|
|||
return {
|
||||
dryRun: args.dryRun,
|
||||
auth: args.auth,
|
||||
author: args.author ?? pr.author,
|
||||
folder: `${folder.startsWith("/") ? "" : process.cwd() + "/"}${args.folder ?? this.getDefaultFolder()}`,
|
||||
targetBranch: args.targetBranch,
|
||||
originalPullRequest: pr,
|
||||
backportPullRequest: this.getDefaultBackportPullRequest(pr, args)
|
||||
backportPullRequest: this.getDefaultBackportPullRequest(pr, args),
|
||||
git: {
|
||||
user: args.gitUser,
|
||||
email: args.gitEmail,
|
||||
}
|
||||
};
|
||||
}
|
||||
getDefaultFolder() {
|
||||
|
@ -144,18 +162,22 @@ class PullRequestConfigsParser extends configs_parser_1.default {
|
|||
* @returns {GitPullRequest}
|
||||
*/
|
||||
getDefaultBackportPullRequest(originalPullRequest, args) {
|
||||
const reviewers = [];
|
||||
reviewers.push(originalPullRequest.author);
|
||||
if (originalPullRequest.mergedBy) {
|
||||
reviewers.push(originalPullRequest.mergedBy);
|
||||
const reviewers = args.reviewers ?? [];
|
||||
if (reviewers.length == 0 && args.inheritReviewers) {
|
||||
// inherit only if args.reviewers is empty and args.inheritReviewers set to true
|
||||
reviewers.push(originalPullRequest.author);
|
||||
if (originalPullRequest.mergedBy) {
|
||||
reviewers.push(originalPullRequest.mergedBy);
|
||||
}
|
||||
}
|
||||
const bodyPrefix = args.bodyPrefix ?? `**Backport:** ${originalPullRequest.htmlUrl}\r\n\r\n`;
|
||||
const body = args.body ?? `${originalPullRequest.body}\r\n\r\nPowered by [BPer](https://github.com/lampajr/backporting).`;
|
||||
return {
|
||||
author: originalPullRequest.author,
|
||||
author: args.gitUser,
|
||||
title: args.title ?? `[${args.targetBranch}] ${originalPullRequest.title}`,
|
||||
body: `${bodyPrefix}${body}`,
|
||||
reviewers: [...new Set(reviewers)],
|
||||
assignees: [...new Set(args.assignees)],
|
||||
targetRepo: originalPullRequest.targetRepo,
|
||||
sourceRepo: originalPullRequest.targetRepo,
|
||||
branchName: args.bpBranchName,
|
||||
|
@ -185,10 +207,10 @@ const fs_1 = __importDefault(__nccwpck_require__(7147));
|
|||
* Command line git commands executor service
|
||||
*/
|
||||
class GitCLIService {
|
||||
constructor(auth, author) {
|
||||
constructor(auth, gitData) {
|
||||
this.logger = logger_service_factory_1.default.getLogger();
|
||||
this.auth = auth;
|
||||
this.author = author;
|
||||
this.gitData = gitData;
|
||||
}
|
||||
/**
|
||||
* Return a pre-configured SimpleGit instance able to execute commands from current
|
||||
|
@ -198,15 +220,15 @@ class GitCLIService {
|
|||
*/
|
||||
git(cwd) {
|
||||
const gitConfig = { ...(cwd ? { baseDir: cwd } : {}) };
|
||||
return (0, simple_git_1.default)(gitConfig).addConfig("user.name", this.author).addConfig("user.email", "noreply@github.com");
|
||||
return (0, simple_git_1.default)(gitConfig).addConfig("user.name", this.gitData.user).addConfig("user.email", this.gitData.email);
|
||||
}
|
||||
/**
|
||||
* Update the provided remote URL by adding the auth token if not empty
|
||||
* @param remoteURL remote link, e.g., https://github.com/lampajr/backporting-example.git
|
||||
*/
|
||||
remoteWithAuth(remoteURL) {
|
||||
if (this.auth && this.author) {
|
||||
return remoteURL.replace("://", `://${this.author}:${this.auth}@`);
|
||||
if (this.auth && this.gitData.user) {
|
||||
return remoteURL.replace("://", `://${this.gitData.user}:${this.auth}@`);
|
||||
}
|
||||
// return remote as it is
|
||||
return remoteURL;
|
||||
|
@ -375,6 +397,7 @@ class GitHubMapper {
|
|||
merged: pr.merged ?? false,
|
||||
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),
|
||||
sourceRepo: {
|
||||
owner: pr.head.repo.full_name.split("/")[0],
|
||||
project: pr.head.repo.full_name.split("/")[1],
|
||||
|
@ -446,13 +469,26 @@ class GitHubService {
|
|||
owner: backport.owner,
|
||||
repo: backport.repo,
|
||||
pull_number: data.number,
|
||||
reviewers: backport.reviewers
|
||||
reviewers: backport.reviewers,
|
||||
});
|
||||
}
|
||||
catch (error) {
|
||||
this.logger.error(`Error requesting reviewers: ${error}`);
|
||||
}
|
||||
}
|
||||
if (backport.assignees.length > 0) {
|
||||
try {
|
||||
await this.octokit.issues.addAssignees({
|
||||
owner: backport.owner,
|
||||
repo: backport.repo,
|
||||
issue_number: data.number,
|
||||
assignees: backport.assignees,
|
||||
});
|
||||
}
|
||||
catch (error) {
|
||||
this.logger.error(`Error setting assignees: ${error}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
// UTILS
|
||||
/**
|
||||
|
@ -657,7 +693,7 @@ class Runner {
|
|||
const originalPR = configs.originalPullRequest;
|
||||
const backportPR = configs.backportPullRequest;
|
||||
// start local git operations
|
||||
const git = new git_cli_1.default(configs.auth, configs.author);
|
||||
const git = new git_cli_1.default(configs.auth, configs.git);
|
||||
// 4. clone the repository
|
||||
await git.clone(configs.originalPullRequest.targetRepo.cloneUrl, configs.folder, configs.targetBranch);
|
||||
// 5. create new branch from target one and checkout
|
||||
|
@ -679,7 +715,8 @@ class Runner {
|
|||
base: configs.targetBranch,
|
||||
title: backportPR.title,
|
||||
body: backportPR.body,
|
||||
reviewers: backportPR.reviewers
|
||||
reviewers: backportPR.reviewers,
|
||||
assignees: backportPR.assignees,
|
||||
};
|
||||
if (!configs.dryRun) {
|
||||
// 8. push the new branch to origin
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue