Skip to content

Commit 0514087

Browse files
authored
trim the labels (#419)
* trim the labels leading and trailing spaces are not possible in github so trim the labels before matching them fixes #416 * lint * fix bug
1 parent b9b2b61 commit 0514087

4 files changed

Lines changed: 37 additions & 7 deletions

File tree

app/command-enabled.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ function notEnabled(command) {
99
};
1010
}
1111

12+
function trimLabels(labels) {
13+
return labels.map((it) => it.trim());
14+
}
15+
1216
export function transferEnabled(config) {
1317
if (!config.commands.transfer.enabled) {
1418
return notEnabled("transfer");
@@ -24,14 +28,15 @@ export function labelEnabled(config, labels) {
2428
return notEnabled("label");
2529
}
2630

31+
const allowedLabels = trimLabels(labelConfig.allowedLabels);
2732
if (
2833
// if length is = 0 then all labels are allowed
29-
labelConfig.allowedLabels.length > 0 &&
30-
!labels.every((label) => labelConfig.allowedLabels.includes(label))
34+
allowedLabels.length > 0 &&
35+
!labels.every((label) => allowedLabels.includes(label))
3136
) {
3237
return {
3338
enabled: false,
34-
error: `${labels} doesn't match the allowed labels \`${labelConfig.allowedLabels.join(
39+
error: `${labels} doesn't match the allowed labels \`${allowedLabels.join(
3540
",",
3641
)}\``,
3742
};
@@ -62,14 +67,15 @@ export function removeLabelEnabled(config, labels) {
6267
return notEnabled("remove-label");
6368
}
6469

70+
const allowedLabels = trimLabels(labelConfig.allowedLabels);
6571
if (
6672
// if length is = 0 then all labels are allowed
67-
labelConfig.allowedLabels.length > 0 &&
68-
!labels.every((label) => labelConfig.allowedLabels.includes(label))
73+
allowedLabels.length > 0 &&
74+
!labels.every((label) => allowedLabels.includes(label))
6975
) {
7076
return {
7177
enabled: false,
72-
error: `${labels} doesn't match the allowed labels \`${labelConfig.allowedLabels.join(
78+
error: `${labels} doesn't match the allowed labels \`${allowedLabels.join(
7379
",",
7480
)}\``,
7581
};

app/command-enabled.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,22 @@ describe("command-enabled", () => {
127127
});
128128
});
129129

130+
test("is enabled when label is in allowedLabels with blanks", () => {
131+
const sut = labelEnabled(
132+
{
133+
commands: {
134+
label: {
135+
enabled: true,
136+
allowedLabels: ["label2", "label1 "],
137+
},
138+
},
139+
},
140+
["label1"],
141+
);
142+
143+
expect(sut.enabled).toEqual(true);
144+
});
145+
130146
describe("closeEnabled", () => {
131147
test("is enabled when config is enabled", () => {
132148
const sut = closeEnabled({

app/converters.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,8 @@ export function extractUsersAndTeams(orgName, reviewers) {
88
}
99

1010
export function extractCommaSeparated(item) {
11-
return item.split(",").filter(Boolean);
11+
return item
12+
.split(",")
13+
.filter(Boolean)
14+
.map((it) => it.trim());
1215
}

app/converters.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ describe("converters", () => {
9292
test("remove empty string entries in split", () => {
9393
const labels = extractCommaSeparated("label1, ,label2");
9494

95+
expect(labels).toEqual(expect.arrayContaining(actual));
96+
});
97+
test("remove trailing whitespace in split", () => {
98+
const labels = extractCommaSeparated("label1, label2");
99+
95100
expect(labels).toEqual(expect.arrayContaining(actual));
96101
});
97102
});

0 commit comments

Comments
 (0)