feat: override backporting pr fields (#38)

* feat: override local git user config

* feat(issue-32): override reviewers and assignees
This commit is contained in:
Andrea Lamparelli 2023-06-22 17:44:14 +02:00 committed by GitHub
parent 22bec0c537
commit a32e8cd34c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 671 additions and 107 deletions

86
dist/gha/index.js vendored
View file

@ -35,21 +35,39 @@ class GHAArgsParser {
* @param key input key
* @returns the value or undefined
*/
_getOrUndefined(key) {
getOrUndefined(key) {
const value = (0, core_1.getInput)(key);
return value !== "" ? value : undefined;
}
getOrDefault(key, defaultValue) {
const value = (0, core_1.getInput)(key);
return value !== "" ? value : defaultValue;
}
getAsCommaSeparatedList(key) {
// trim the value
const value = ((0, core_1.getInput)(key) ?? "").trim();
return value !== "" ? value.replace(/\s/g, "").split(",") : [];
}
getAsBooleanOrDefault(key, defaultValue) {
const value = (0, core_1.getInput)(key).trim();
return value !== "" ? value.toLowerCase() === "true" : defaultValue;
}
parse() {
return {
dryRun: (0, core_1.getInput)("dry-run") === "true",
auth: (0, core_1.getInput)("auth") ? (0, core_1.getInput)("auth") : "",
dryRun: this.getAsBooleanOrDefault("dry-run", false),
auth: (0, core_1.getInput)("auth"),
pullRequest: (0, core_1.getInput)("pull-request"),
targetBranch: (0, core_1.getInput)("target-branch"),
folder: this._getOrUndefined("folder"),
title: this._getOrUndefined("title"),
body: this._getOrUndefined("body"),
bodyPrefix: this._getOrUndefined("body-prefix"),
bpBranchName: this._getOrUndefined("bp-branch-name"),
folder: this.getOrUndefined("folder"),
gitUser: this.getOrDefault("git-user", "GitHub"),
gitEmail: this.getOrDefault("git-email", "noreply@github.com"),
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", false),
};
}
}
@ -117,11 +135,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() {
@ -135,18 +156,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,
@ -176,10 +201,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
@ -189,15 +214,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;
@ -366,6 +391,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],
@ -437,13 +463,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
/**
@ -648,7 +687,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
@ -670,7 +709,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