Skip to content

Commit

Permalink
feat: allow backport to remote repository
Browse files Browse the repository at this point in the history
  • Loading branch information
tasso94 committed Dec 20, 2023
1 parent 671709e commit 651a8d9
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 20 deletions.
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ inputs:
Please refer to this action's README for all available placeholders.
default: >-
[Backport ${target_branch}] ${pull_title}
pull_branchname:
description: ""
default: >-
backport-${pull_number}-to-${target}
target_branches:
description: >
The action will backport the pull request to each specified target branch (space-delimited).
Expand Down
29 changes: 19 additions & 10 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class Backport {
this.git = git;
}
run() {
var _a, _b, _c, _d, _e, _f, _g, _h;
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
return __awaiter(this, void 0, void 0, function* () {
try {
const payload = this.github.getPayload();
Expand Down Expand Up @@ -195,7 +195,7 @@ class Backport {
}
}
try {
const branchname = `backport-${pull_number}-to-${target}`;
const { title, body, branchname } = this.composePRContent(target, mainpr);
console.log(`Start backport to ${branchname}`);
try {
yield this.git.checkout(branchname, `origin/${target}`, pwd);
Expand Down Expand Up @@ -242,7 +242,6 @@ class Backport {
continue;
}
console.info(`Create PR for ${branchname}`);
const { title, body } = this.composePRContent(target, mainpr);
const new_pr_response = yield this.github.createPR({
owner: (_e = this.config.target_owner) !== null && _e !== void 0 ? _e : owner,
repo: (_f = this.config.target_repo) !== null && _f !== void 0 ? _f : repo,
Expand Down Expand Up @@ -279,17 +278,25 @@ class Backport {
const assignees = mainpr.assignees.map((label) => label.login);
if (assignees.length > 0) {
console.info("Setting assignees " + assignees);
const set_assignee_response = yield this.github.setAssignees(new_pr.number, assignees);
const set_assignee_response = yield this.github.setAssignees(new_pr.number, assignees, {
owner: (_h = this.config.target_owner) !== null && _h !== void 0 ? _h : owner,
repo: (_j = this.config.target_repo) !== null && _j !== void 0 ? _j : repo,
});
if (set_assignee_response.status != 201) {
console.error(JSON.stringify(set_assignee_response));
}
}
}
if (this.config.copy_requested_reviewers == true) {
const reviewers = (_h = mainpr.requested_reviewers) === null || _h === void 0 ? void 0 : _h.map((reviewer) => reviewer.login);
const reviewers = (_k = mainpr.requested_reviewers) === null || _k === void 0 ? void 0 : _k.map((reviewer) => reviewer.login);
if ((reviewers === null || reviewers === void 0 ? void 0 : reviewers.length) > 0) {
console.info("Setting reviewers " + reviewers);
const reviewRequest = Object.assign(Object.assign({}, this.github.getRepo()), { pull_number: new_pr.number, reviewers: reviewers });
const reviewRequest = {
owner: (_l = this.config.target_owner) !== null && _l !== void 0 ? _l : owner,
repo: (_m = this.config.target_repo) !== null && _m !== void 0 ? _m : repo,
pull_number: new_pr.number,
reviewers: reviewers,
};
const set_reviewers_response = yield this.github.requestReviewers(reviewRequest);
if (set_reviewers_response.status != 201) {
console.error(JSON.stringify(set_reviewers_response));
Expand Down Expand Up @@ -350,7 +357,8 @@ class Backport {
composePRContent(target, main) {
const title = utils.replacePlaceholders(this.config.pull.title, main, target);
const body = utils.replacePlaceholders(this.config.pull.description, main, target);
return { title, body };
const branchname = utils.replacePlaceholders(this.config.pull.branchname, main, target);
return { title, body, branchname };
}
composeMessageForFetchTargetFailure(target) {
return (0, dedent_1.default) `Backport failed for \`${target}\`: couldn't find remote ref \`${target}\`.
Expand Down Expand Up @@ -734,10 +742,10 @@ class Github {
return __classPrivateFieldGet(this, _Github_octokit, "f").rest.issues.addLabels(Object.assign(Object.assign({}, this.getRepo()), { issue_number: pr, labels }));
});
}
setAssignees(pr, assignees) {
setAssignees(pr, assignees, repo) {
return __awaiter(this, void 0, void 0, function* () {
console.log(`Set Assignees ${assignees} to #${pr}`);
return __classPrivateFieldGet(this, _Github_octokit, "f").rest.issues.addAssignees(Object.assign(Object.assign({}, this.getRepo()), { issue_number: pr, assignees }));
return __classPrivateFieldGet(this, _Github_octokit, "f").rest.issues.addAssignees(Object.assign(Object.assign({}, repo), { issue_number: pr, assignees }));
});
}
setMilestone(pr, milestone) {
Expand Down Expand Up @@ -959,6 +967,7 @@ function run() {
const pattern = core.getInput("label_pattern");
const description = core.getInput("pull_description");
const title = core.getInput("pull_title");
const branchname = core.getInput("pull_branchname");
const copy_labels_pattern = core.getInput("copy_labels_pattern");
const target_branches = core.getInput("target_branches");
const merge_commits = core.getInput("merge_commits");
Expand Down Expand Up @@ -986,7 +995,7 @@ function run() {
const config = {
pwd,
labels: { pattern: pattern === "" ? undefined : new RegExp(pattern) },
pull: { description, title },
pull: { description, title, branchname },
copy_labels_pattern: copy_labels_pattern === "" ? undefined : new RegExp(copy_labels_pattern),
target_branches: target_branches === "" ? undefined : target_branches,
commits: { merge_commits },
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

19 changes: 15 additions & 4 deletions src/backport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import * as utils from "./utils";
type PRContent = {
title: string;
body: string;
branchname: string;
};

export type Config = {
Expand All @@ -23,6 +24,7 @@ export type Config = {
pull: {
description: string;
title: string;
branchname: string;
};
copy_labels_pattern?: RegExp;
target_branches?: string;
Expand Down Expand Up @@ -247,7 +249,7 @@ export class Backport {
}

try {
const branchname = `backport-${pull_number}-to-${target}`;
const { title, body, branchname} = this.composePRContent(target, mainpr);

console.log(`Start backport to ${branchname}`);
try {
Expand Down Expand Up @@ -311,7 +313,6 @@ export class Backport {
}

console.info(`Create PR for ${branchname}`);
const { title, body } = this.composePRContent(target, mainpr);
const new_pr_response = await this.github.createPR({
owner: this.config.target_owner ?? owner,
repo: this.config.target_repo ?? repo,
Expand Down Expand Up @@ -358,6 +359,10 @@ export class Backport {
const set_assignee_response = await this.github.setAssignees(
new_pr.number,
assignees,
{
owner: this.config.target_owner ?? owner,
repo: this.config.target_repo ?? repo,
}
);
if (set_assignee_response.status != 201) {
console.error(JSON.stringify(set_assignee_response));
Expand All @@ -372,7 +377,8 @@ export class Backport {
if (reviewers?.length > 0) {
console.info("Setting reviewers " + reviewers);
const reviewRequest = {
...this.github.getRepo(),
owner: this.config.target_owner ?? owner,
repo: this.config.target_repo ?? repo,
pull_number: new_pr.number,
reviewers: reviewers,
};
Expand Down Expand Up @@ -450,7 +456,12 @@ export class Backport {
main,
target,
);
return { title, body };
const branchname = utils.replacePlaceholders(
this.config.pull.branchname,
main,
target,
);
return { title, body, branchname };
}

private composeMessageForFetchTargetFailure(target: string) {
Expand Down
13 changes: 9 additions & 4 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import * as github from "@actions/github";

export interface GithubApi {
getRepo(): { owner: string; repo: string };
getRepo(): Repo;
getPayload(): Payload;
getPullNumber(): number;
createComment(comment: Comment): Promise<{}>;
Expand All @@ -20,7 +20,7 @@ export interface GithubApi {
createPR(pr: CreatePullRequest): Promise<CreatePullRequestResponse>;
labelPR(pr: number, labels: string[]): Promise<LabelPullRequestResponse>;
requestReviewers(request: ReviewRequest): Promise<RequestReviewersResponse>;
setAssignees(pr: number, assignees: string[]): Promise<GenericResponse>;
setAssignees(pr: number, assignees: string[], repo: Repo): Promise<GenericResponse>;
setMilestone(pr: number, milestone: number): Promise<GenericResponse>;
mergeStrategy(
pull: PullRequest,
Expand Down Expand Up @@ -134,10 +134,10 @@ export class Github implements GithubApi {
});
}

public async setAssignees(pr: number, assignees: string[]) {
public async setAssignees(pr: number, assignees: string[], repo: Repo) {
console.log(`Set Assignees ${assignees} to #${pr}`);
return this.#octokit.rest.issues.addAssignees({
...this.getRepo(),
...repo,
issue_number: pr,
assignees,
});
Expand Down Expand Up @@ -330,6 +330,11 @@ export enum MergeStrategy {
UNKNOWN = "unknown",
}

export type Repo = {
owner: string,
repo: string;
}

export type PullRequest = {
number: number;
title: string;
Expand Down
3 changes: 2 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ async function run(): Promise<void> {
const pattern = core.getInput("label_pattern");
const description = core.getInput("pull_description");
const title = core.getInput("pull_title");
const branchname = core.getInput("pull_branchname");
const copy_labels_pattern = core.getInput("copy_labels_pattern");
const target_branches = core.getInput("target_branches");
const merge_commits = core.getInput("merge_commits");
Expand Down Expand Up @@ -46,7 +47,7 @@ async function run(): Promise<void> {
const config: Config = {
pwd,
labels: { pattern: pattern === "" ? undefined : new RegExp(pattern) },
pull: { description, title },
pull: { description, title, branchname },
copy_labels_pattern:
copy_labels_pattern === "" ? undefined : new RegExp(copy_labels_pattern),
target_branches: target_branches === "" ? undefined : target_branches,
Expand Down

0 comments on commit 651a8d9

Please sign in to comment.