Skip to content

Commit

Permalink
Refactor config.labels to source_labels_pattern
Browse files Browse the repository at this point in the history
Signed-off-by: Chance Zibolski <[email protected]>
  • Loading branch information
chancez committed Jul 23, 2024
1 parent 924c817 commit 8f74ddf
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 32 deletions.
12 changes: 6 additions & 6 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ 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 '${(_c = this.config.labels.pattern) === null || _c === void 0 ? void 0 : _c.source}'`);
console.log(`Nothing to backport: no 'target_branches' specified and none of the labels match the backport pattern '${(_c = this.config.source_labels_pattern) === null || _c === void 0 ? void 0 : _c.source}'`);
return; // nothing left to do here
}
console.log(`Fetching all the commits from the pull request: ${mainpr.commits + 1}`);
Expand Down Expand Up @@ -181,8 +181,8 @@ class Backport {
labelsToCopy = mainpr.labels
.map((label) => label.name)
.filter((label) => label.match(copyLabelsPattern) &&
(this.config.labels.pattern === undefined ||
!label.match(this.config.labels.pattern)));
(this.config.source_labels_pattern === undefined ||
!label.match(this.config.source_labels_pattern)));
}
console.log(`Will copy labels matching ${this.config.copy_labels_pattern}. Found matching labels: ${labelsToCopy}`);
if (this.shouldUseDownstreamRepo()) {
Expand Down Expand Up @@ -467,7 +467,7 @@ class Backport {
const suggestionToResolve = this.composeMessageToResolveCommittedConflicts(target, branchname, commitShasToCherryPick, conflictResolution);
return (0, dedent_1.default) `Created backport PR for \`${target}\`:
- ${downstream}#${pr_number} with remaining conflicts!

${suggestionToResolve}`;
}
createOutput(successByTarget, createdPullRequestNumbers) {
Expand Down Expand Up @@ -497,7 +497,7 @@ function findTargetBranches(config, labels, headref) {
}
exports.findTargetBranches = findTargetBranches;
function findTargetBranchesFromLabels(labels, config) {
const pattern = config.labels.pattern;
const pattern = config.source_labels_pattern;
if (pattern === undefined) {
return [];
}
Expand Down Expand Up @@ -1115,7 +1115,7 @@ function run() {
const git = new git_1.Git(execa_1.execa);
const config = {
pwd,
labels: { pattern: pattern === "" ? undefined : new RegExp(pattern) },
source_labels_pattern: pattern === "" ? undefined : new RegExp(pattern),
pull: { description, title, branch_name },
copy_labels_pattern: copy_labels_pattern === "" ? undefined : new RegExp(copy_labels_pattern),
target_branches: target_branches === "" ? undefined : target_branches,
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

18 changes: 8 additions & 10 deletions src/backport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ type PRContent = {

export type Config = {
pwd: string;
labels: {
pattern?: RegExp;
};
source_labels_pattern?: RegExp;
pull: {
description: string;
title: string;
Expand Down Expand Up @@ -121,7 +119,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.source_labels_pattern?.source}'`,
);
return; // nothing left to do here
}
Expand Down Expand Up @@ -240,8 +238,8 @@ export class Backport {
.filter(
(label) =>
label.match(copyLabelsPattern) &&
(this.config.labels.pattern === undefined ||
!label.match(this.config.labels.pattern)),
(this.config.source_labels_pattern === undefined ||
!label.match(this.config.source_labels_pattern)),
);
}
console.log(
Expand Down Expand Up @@ -677,7 +675,7 @@ export class Backport {
);
return dedent`Created backport PR for \`${target}\`:
- ${downstream}#${pr_number} with remaining conflicts!
${suggestionToResolve}`;
}

Expand All @@ -702,7 +700,7 @@ export class Backport {
}

export function findTargetBranches(
config: Pick<Config, "labels" | "target_branches">,
config: Pick<Config, "source_labels_pattern" | "target_branches">,
labels: string[],
headref: string,
) {
Expand Down Expand Up @@ -736,9 +734,9 @@ export function findTargetBranches(

function findTargetBranchesFromLabels(
labels: string[],
config: Pick<Config, "labels">,
config: Pick<Config, "source_labels_pattern">,
) {
const pattern = config.labels.pattern;
const pattern = config.source_labels_pattern;
if (pattern === undefined) {
return [];
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ async function run(): Promise<void> {
const git = new Git(execa);
const config: Config = {
pwd,
labels: { pattern: pattern === "" ? undefined : new RegExp(pattern) },
source_labels_pattern: pattern === "" ? undefined : new RegExp(pattern),
pull: { description, title, branch_name },
copy_labels_pattern:
copy_labels_pattern === "" ? undefined : new RegExp(copy_labels_pattern),
Expand Down
28 changes: 14 additions & 14 deletions src/test/backport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe("find target branches", () => {
it("when labels is an empty list", () => {
expect(
findTargetBranches(
{ labels: { pattern: default_pattern } },
{ source_labels_pattern: default_pattern },
[],
"feature/one",
),
Expand All @@ -17,7 +17,7 @@ describe("find target branches", () => {
it("when none of the labels match the pattern", () => {
expect(
findTargetBranches(
{ labels: { pattern: default_pattern } },
{ source_labels_pattern: default_pattern },
["a label", "another-label", "a/third/label"],
"feature/one",
),
Expand All @@ -27,7 +27,7 @@ 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$/ } },
{ source_labels_pattern: /^no capture group$/ },
["no capture group"],
"feature/one",
),
Expand All @@ -37,7 +37,7 @@ describe("find target branches", () => {
it("when the label pattern is an empty string", () => {
expect(
findTargetBranches(
{ labels: { pattern: undefined } },
{ source_labels_pattern: undefined },
["an empty string"],
"feature/one",
),
Expand All @@ -47,7 +47,7 @@ describe("find target branches", () => {
it("when target_branches is an empty string", () => {
expect(
findTargetBranches(
{ labels: { pattern: default_pattern }, target_branches: "" },
{ source_labels_pattern: default_pattern, target_branches: "" },
["a label"],
"feature/one",
),
Expand All @@ -57,7 +57,7 @@ describe("find target branches", () => {
it("when the label pattern only matches the headref", () => {
expect(
findTargetBranches(
{ labels: { pattern: default_pattern } },
{ source_labels_pattern: default_pattern },
["backport feature/one"],
"feature/one",
),
Expand All @@ -67,7 +67,7 @@ describe("find target branches", () => {
it("when target_branches only contains the headref", () => {
expect(
findTargetBranches(
{ labels: {}, target_branches: "feature/one" },
{ target_branches: "feature/one" },
[],
"feature/one",
),
Expand All @@ -79,7 +79,7 @@ describe("find target branches", () => {
it("when a label matches the pattern and captures a target branch", () => {
expect(
findTargetBranches(
{ labels: { pattern: default_pattern } },
{ source_labels_pattern: default_pattern },
["backport release-1"],
"feature/one",
),
Expand All @@ -89,7 +89,7 @@ describe("find target branches", () => {
it("when several labels match the pattern and capture a target branch", () => {
expect(
findTargetBranches(
{ labels: { pattern: default_pattern } },
{ source_labels_pattern: default_pattern },
["backport release-1", "backport another/target/branch"],
"feature/one",
),
Expand All @@ -100,7 +100,7 @@ describe("find target branches", () => {
expect(
findTargetBranches(
{
labels: { pattern: default_pattern },
source_labels_pattern: default_pattern,
target_branches: "release-1",
},
[],
Expand All @@ -113,7 +113,7 @@ describe("find target branches", () => {
expect(
findTargetBranches(
{
labels: { pattern: default_pattern },
source_labels_pattern: default_pattern,
target_branches: "release-1 another/target/branch",
},
[],
Expand All @@ -126,7 +126,7 @@ describe("find target branches", () => {
expect(
findTargetBranches(
{
labels: { pattern: default_pattern },
source_labels_pattern: default_pattern,
target_branches: "release-1",
},
["backport release-1"],
Expand All @@ -138,7 +138,7 @@ describe("find target branches", () => {
it("when several labels match the pattern the headref is excluded", () => {
expect(
findTargetBranches(
{ labels: { pattern: default_pattern } },
{ source_labels_pattern: default_pattern },
["backport feature/one", "backport feature/two"],
"feature/one",
),
Expand All @@ -148,7 +148,7 @@ describe("find target branches", () => {
it("when several target branches are specified the headref is excluded", () => {
expect(
findTargetBranches(
{ labels: {}, target_branches: "feature/one feature/two" },
{ target_branches: "feature/one feature/two" },
[],
"feature/one",
),
Expand Down

0 comments on commit 8f74ddf

Please sign in to comment.