Skip to content

Commit

Permalink
feat: add support for milestone, assignees and reviewers
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Schmid <[email protected]>
  • Loading branch information
jschmid1 committed Nov 2, 2023
1 parent cef13d1 commit 0283b4d
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 2 deletions.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ jobs:
runs-on: ubuntu-latest
# Only run when pull request is merged
# or when a comment containing `/backport` is created by someone other than the
# or when a comment containing `/backport` is created by someone other than the
# https://github.com/backport-action bot user (user id: 97796249). Note that if you use your
# own PAT as `github_token`, that you should replace this id with yours.
if: >
Expand Down Expand Up @@ -185,6 +185,26 @@ See [How it works](#how-it-works).
Can be used in addition to backport labels.
By default, only backport labels are used to specify the target branches.


### `copy_milestone`

Default: `''` (disabled)

Controls wheather the "milestone" of the original pull request should be added to the backports.

### `copy_assignees`

Default: `''` (disabled)

Controls wheather the assignees of the original pull request should be added to the backports.

### `copy_reviewers`

Default: `''` (disabled)

Controls wheather the reviewers of the original pull request should be added to the backports.


## Placeholders
In the `pull_description` and `pull_title` inputs, placeholders can be used to define variable values.
These are indicated by a dollar sign and curly braces (`${placeholder}`).
Expand Down
12 changes: 12 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ inputs:
Note that the pull request's headref is excluded automatically.
Can be used in addition to backport labels.
By default, only backport labels are used to specify the target branches.
copy_milestone:
description: >
Whether or not to copy the milestone from the original pull request to the backport pull request.
By default, the milestone is not copied.
copy_assignees:
description: >
Whether or not to copy the assignees from the original pull request to the backport pull request.
By default, the assignees are not copied.
copy_reviewers:
description: >
Whether or not to copy the reviewers from the original pull request to the backport pull request.
By default, the reviewers are not copied.
outputs:
was_successful:
description: >
Expand Down
54 changes: 53 additions & 1 deletion src/backport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export type Config = {
commits: {
merge_commits: "fail" | "skip";
};
copy_milestone: boolean;
copy_assignees: boolean;
copy_reviewers: boolean;
};

enum Output {
Expand Down Expand Up @@ -257,6 +260,54 @@ export class Backport {
}
const new_pr = new_pr_response.data;

if (this.config.copy_milestone == true) {
const milestone = mainpr.milestone?.number;
if (milestone) {
console.info("Setting milestone to " + milestone);
const set_milestone_response = await this.github.setMilestone(
new_pr.number,
milestone,
);
if (set_milestone_response.status != 200) {
console.error(set_milestone_response.status);
console.error(JSON.stringify(set_milestone_response));
}
}
}

if (this.config.copy_assignees == true) {
const assignees = mainpr.assignees.map((label) => label.login);
if (assignees.length > 0) {
console.info("Setting assignees " + assignees);
const set_assignee_response = await this.github.setAssignees(
new_pr.number,
assignees,
);
if (set_assignee_response.status != 201) {
console.error(set_assignee_response.status);
}
}
}

if (this.config.copy_reviewers == true) {
const reviewers = mainpr.requested_reviewers?.map(
(reviewer) => reviewer.login,
);
if (reviewers?.length > 0) {
console.info("Setting reviewers " + reviewers);
const reviewRequest = {
...this.github.getRepo(),
pull_number: new_pr.number,
reviewers: reviewers,
};
const set_reviewers_response =
await this.github.requestReviewers(reviewRequest);
if (set_reviewers_response.status != 201) {
console.error(set_reviewers_response.status);
}
}
}

if (labelsToCopy.length > 0) {
const label_response = await this.github.labelPR(
new_pr.number,
Expand Down Expand Up @@ -306,6 +357,7 @@ export class Backport {
}
}


private findTargetBranches(mainpr: PullRequest, config: Config): string[] {
const labels = mainpr.labels.map((label) => label.name);
return findTargetBranches(config, labels, mainpr.head.ref);
Expand Down Expand Up @@ -377,7 +429,7 @@ export class Backport {
private composeMessageForCreatePRFailed(
response: CreatePullRequestResponse,
): string {
return dedent`Backport branch created but failed to create PR.
return dedent`Backport branch created but failed to create PR.
Request to create PR rejected with status ${response.status}.
(see action log for full response)`;
Expand Down
37 changes: 37 additions & 0 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ 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>;
setMilestone(pr: number, milestone: number): Promise<GenericResponse>;
}

export class Github implements GithubApi {
Expand Down Expand Up @@ -126,6 +128,24 @@ export class Github implements GithubApi {
labels,
});
}

public async setAssignees(pr: number, assignees: string[]) {
console.log(`Set Assignees ${assignees} to #${pr}`);
return this.#octokit.rest.issues.addAssignees({
...this.getRepo(),
issue_number: pr,
assignees,
});
}

public async setMilestone(pr: number, milestone: number) {
console.log(`Set Milestone ${milestone} to #${pr}`);
return this.#octokit.rest.issues.update({
...this.getRepo(),
issue_number: pr,
milestone: milestone,
});
}
}

export type PullRequest = {
Expand All @@ -149,6 +169,18 @@ export type PullRequest = {
login: string;
}[];
commits: number;
milestone: {
number: number;
id: number;
title: string;
};
assignees: {
login: string;
id: number;
}[];
merged_by: {
login: string;
};
};
export type CreatePullRequestResponse = {
status: number;
Expand All @@ -158,6 +190,11 @@ export type CreatePullRequestResponse = {
};
};
export type RequestReviewersResponse = CreatePullRequestResponse;

export type GenericResponse = {
status: number;
};

export type LabelPullRequestResponse = {
status: number;
};
Expand Down
6 changes: 6 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ async function run(): Promise<void> {
const copy_labels_pattern = core.getInput("copy_labels_pattern");
const target_branches = core.getInput("target_branches");
const merge_commits = core.getInput("merge_commits");
const copy_assignees = core.getInput("copy_assignees");
const copy_milestone = core.getInput("copy_milestone");
const copy_reviewers = core.getInput("copy_reviewers");

if (merge_commits != "fail" && merge_commits != "skip") {
const message = `Expected input 'merge_commits' to be either 'fail' or 'skip', but was '${merge_commits}'`;
Expand All @@ -36,6 +39,9 @@ async function run(): Promise<void> {
copy_labels_pattern === "" ? undefined : new RegExp(copy_labels_pattern),
target_branches: target_branches === "" ? undefined : target_branches,
commits: { merge_commits },
copy_assignees: copy_assignees === "true",
copy_milestone: copy_milestone === "true",
copy_reviewers: copy_reviewers === "true",
};
const backport = new Backport(github, config, git);

Expand Down

0 comments on commit 0283b4d

Please sign in to comment.