Skip to content

Commit

Permalink
Merge pull request #327 from korthout/copy-labels
Browse files Browse the repository at this point in the history
Copy labels as specified by `copy_labels_pattern`
  • Loading branch information
korthout committed Feb 23, 2023
2 parents 374bb18 + e8aa81b commit a15b77d
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 2 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ jobs:
# These are indicated by a dollar sign and curly braces (`${placeholder}`).
# Please refer to this action's README for all available placeholders.
# pull_title: "[Backport ${target_branch}] ${pull_title}"

# Optional
# Regex pattern to match github labels which will be copied from the
# original pull request to the backport pull request. By default, no
# labels are copied.
# copy_labels_pattern: ''
```

### Trigger using a comment
Expand Down Expand Up @@ -139,6 +145,12 @@ jobs:
# These are indicated by a dollar sign and curly braces (`${placeholder}`).
# Please refer to this action's README for all available placeholders.
# pull_title: "[Backport ${target_branch}] ${pull_title}"

# Optional
# Regex pattern to match github labels which will be copied from the
# original pull request to the backport pull request. By default, no
# labels are copied.
# copy_labels_pattern: ''
```

</p>
Expand Down
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ inputs:
Please refer to this action's README for all available placeholders.
default: >-
[Backport ${target_branch}] ${pull_title}
copy_labels_pattern:
description: >
Regex pattern to match github labels which will be copied from the
original pull request to the backport pull request. By default, no labels
are copied.
outputs:
was_successful:
description: >
Expand Down
24 changes: 24 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ class Backport {
console.log("Determining first and last commit shas, so we can cherry-pick the commit range");
const commitShas = yield this.github.getCommits(mainpr);
console.log(`Found commits: ${commitShas}`);
let labelsToCopy = [];
if (typeof this.config.copy_labels_pattern !== "undefined") {
let copyLabelsPattern = this.config.copy_labels_pattern;
labelsToCopy = labels
.map((label) => label.name)
.filter((label) => label.match(copyLabelsPattern) &&
!label.match(this.config.labels.pattern));
}
console.log(`Will copy labels matching ${this.config.copy_labels_pattern}. Found matching labels: ${labelsToCopy}`);
const successByTarget = new Map();
for (const label of labels) {
console.log(`Working on label ${label.name}`);
Expand Down Expand Up @@ -200,6 +209,13 @@ class Backport {
continue;
}
const new_pr = new_pr_response.data;
if (labelsToCopy.length > 0) {
const label_response = yield this.github.labelPR(new_pr.number, labelsToCopy);
if (label_response.status != 200) {
console.error(JSON.stringify(label_response));
// The PR was still created so let's still comment on the original.
}
}
const message = this.composeMessageForSuccess(new_pr.number, target);
successByTarget.set(target, true);
yield this.github.createComment({
Expand Down Expand Up @@ -547,6 +563,12 @@ class Github {
return __classPrivateFieldGet(this, _Github_octokit, "f").rest.pulls.requestReviewers(request);
});
}
labelPR(pr, labels) {
return __awaiter(this, void 0, void 0, function* () {
console.log(`Label PR #${pr} with labels: ${labels}`);
return __classPrivateFieldGet(this, _Github_octokit, "f").rest.issues.addLabels(Object.assign(Object.assign({}, this.getRepo()), { issue_number: pr, labels }));
});
}
}
exports.Github = Github;
_Github_octokit = new WeakMap(), _Github_context = new WeakMap();
Expand Down Expand Up @@ -607,11 +629,13 @@ function run() {
const pattern = new RegExp(core.getInput("label_pattern"));
const description = core.getInput("pull_description");
const title = core.getInput("pull_title");
const copy_labels_pattern = core.getInput("copy_labels_pattern");
const github = new github_1.Github(token);
const backport = new backport_1.Backport(github, {
pwd,
labels: { pattern },
pull: { description, title },
copy_labels_pattern: copy_labels_pattern === "" ? undefined : new RegExp(copy_labels_pattern),
});
return backport.run();
});
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

28 changes: 27 additions & 1 deletion src/backport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Config = {
description: string;
title: string;
};
copy_labels_pattern?: RegExp;
};

enum Output {
Expand Down Expand Up @@ -84,9 +85,23 @@ export class Backport {
);

const commitShas = await this.github.getCommits(mainpr);

console.log(`Found commits: ${commitShas}`);

let labelsToCopy: string[] = [];
if (typeof this.config.copy_labels_pattern !== "undefined") {
let copyLabelsPattern: RegExp = this.config.copy_labels_pattern;
labelsToCopy = labels
.map((label) => label.name)
.filter(
(label) =>
label.match(copyLabelsPattern) &&
!label.match(this.config.labels.pattern)
);
}
console.log(
`Will copy labels matching ${this.config.copy_labels_pattern}. Found matching labels: ${labelsToCopy}`
);

const successByTarget = new Map<string, boolean>();
for (const label of labels) {
console.log(`Working on label ${label.name}`);
Expand Down Expand Up @@ -222,6 +237,17 @@ export class Backport {
}
const new_pr = new_pr_response.data;

if (labelsToCopy.length > 0) {
const label_response = await this.github.labelPR(
new_pr.number,
labelsToCopy
);
if (label_response.status != 200) {
console.error(JSON.stringify(label_response));
// The PR was still created so let's still comment on the original.
}
}

const message = this.composeMessageForSuccess(new_pr.number, target);
successByTarget.set(target, true);
await this.github.createComment({
Expand Down
13 changes: 13 additions & 0 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface GithubApi {
isMerged(pull: PullRequest): Promise<boolean>;
getCommits(pull: PullRequest): Promise<string[]>;
createPR(pr: CreatePullRequest): Promise<CreatePullRequestResponse>;
labelPR(pr: number, labels: string[]): Promise<LabelPullRequestResponse>;
requestReviewers(request: ReviewRequest): Promise<RequestReviewersResponse>;
}

Expand Down Expand Up @@ -116,6 +117,15 @@ export class Github implements GithubApi {
console.log(`Request reviewers: ${request.reviewers}`);
return this.#octokit.rest.pulls.requestReviewers(request);
}

public async labelPR(pr: number, labels: string[]) {
console.log(`Label PR #${pr} with labels: ${labels}`);
return this.#octokit.rest.issues.addLabels({
...this.getRepo(),
issue_number: pr,
labels,
});
}
}

export type PullRequest = {
Expand Down Expand Up @@ -147,6 +157,9 @@ export type CreatePullRequestResponse = {
};
};
export type RequestReviewersResponse = CreatePullRequestResponse;
export type LabelPullRequestResponse = {
status: number;
};

export type Comment = {
owner: string;
Expand Down
3 changes: 3 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ async function run(): Promise<void> {
const pattern = new RegExp(core.getInput("label_pattern"));
const description = core.getInput("pull_description");
const title = core.getInput("pull_title");
const copy_labels_pattern = core.getInput("copy_labels_pattern");

const github = new Github(token);
const backport = new Backport(github, {
pwd,
labels: { pattern },
pull: { description, title },
copy_labels_pattern:
copy_labels_pattern === "" ? undefined : new RegExp(copy_labels_pattern),
});

return backport.run();
Expand Down

0 comments on commit a15b77d

Please sign in to comment.