Skip to content

Commit

Permalink
feat: disable label_pattern with empty string
Browse files Browse the repository at this point in the history
In order to only use the `target_branches` input, it can be useful to
completely disable the `label_pattern` input.

This adds the ability to completely disable determining the target
branches based on labels. Simply set the `label_pattern` input to an
empty string `''`.
  • Loading branch information
korthout committed May 29, 2023
1 parent 0e02fa9 commit d86425a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ Default: `^backport ([^ ]+)$` (e.g. matches `backport release-3.4`)

Regex pattern to match the backport labels on the merged pull request.
Must contain a capture group for the target branch.
Label matching can be disabled entirely using an empty string `''` as pattern.

The action will backport the pull request to each matched target branch.
See [How it works](#how-it-works).
Expand Down
16 changes: 9 additions & 7 deletions src/backport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type PRContent = {
type Config = {
pwd: string;
labels: {
pattern: RegExp;
pattern?: RegExp;
};
pull: {
description: string;
Expand Down Expand Up @@ -62,7 +62,7 @@ export class Backport {
const target_branches = this.findTargetBranches(mainpr, this.config);
if (target_branches.length === 0) {
console.log(
`Nothing to backport: no 'target_branches' specified and none of the labels match the backport pattern '${this.config.labels.pattern.source}'`
`Nothing to backport: no 'target_branches' specified and none of the labels match the backport pattern '${this.config.labels.pattern?.source}'`
);
return; // nothing left to do here
}
Expand Down Expand Up @@ -91,7 +91,8 @@ export class Backport {
.filter(
(label) =>
label.match(copyLabelsPattern) &&
!label.match(this.config.labels.pattern)
(this.config.labels.pattern === undefined ||
!label.match(this.config.labels.pattern))
);
}
console.log(
Expand Down Expand Up @@ -386,21 +387,22 @@ function findTargetBranchesFromLabels(
labels: string[],
config: Pick<Config, "labels">
) {
if (config.labels.pattern.source === new RegExp("").source) {
const pattern = config.labels.pattern;
if (pattern === undefined) {
return [];
}
return labels
.map((label) => {
return { label: label, match: config.labels.pattern.exec(label) };
return { label: label, match: pattern.exec(label) };
})
.filter((result) => {
if (!result.match) {
console.log(
`label '${result.label}' doesn't match \`label_pattern\` '${config.labels.pattern.source}'`
`label '${result.label}' doesn't match \`label_pattern\` '${pattern.source}'`
);
} else if (result.match.length < 2) {
console.error(
dedent`label '${result.label}' matches \`label_pattern\` '${config.labels.pattern.source}', \
dedent`label '${result.label}' matches \`label_pattern\` '${pattern.source}', \
but no branchname could be captured. Please make sure to provide a regex with a capture group as \
\`label_pattern\`.`
);
Expand Down
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Github } from "./github";
async function run(): Promise<void> {
const token = core.getInput("github_token", { required: true });
const pwd = core.getInput("github_workspace", { required: true });
const pattern = new RegExp(core.getInput("label_pattern"));
const pattern = 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");
Expand All @@ -19,7 +19,7 @@ async function run(): Promise<void> {
const github = new Github(token);
const backport = new Backport(github, {
pwd,
labels: { pattern },
labels: { pattern: pattern === "" ? undefined : new RegExp(pattern) },
pull: { description, title },
copy_labels_pattern:
copy_labels_pattern === "" ? undefined : new RegExp(copy_labels_pattern),
Expand Down
6 changes: 4 additions & 2 deletions src/test/backport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ describe("find target branches", () => {

it("when a label matches the pattern but doesn't capture a target branch", () => {
expect(
findTargetBranches({ labels: { pattern: /^no capture group$/ } }, ["no capture group"])
findTargetBranches({ labels: { pattern: /^no capture group$/ } }, [
"no capture group",
])
).toEqual([]);
});

it("when the label pattern is an empty string", () => {
expect(
findTargetBranches({ labels: { pattern: new RegExp("") } }, [
findTargetBranches({ labels: { pattern: undefined } }, [
"an empty string",
])
).toEqual([]);
Expand Down

0 comments on commit d86425a

Please sign in to comment.