Skip to content

Commit 5be5e66

Browse files
committed
pass in caseSensitiveLabels rather than using a shared global variable in the func
1 parent f6bffef commit 5be5e66

File tree

2 files changed

+87
-72
lines changed

2 files changed

+87
-72
lines changed

internal/cmd/match_criteria.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func PrMatchesCriteria(branch string, prLabels []string) bool {
2020
}
2121

2222
// Check label criteria if any are specified
23-
if !labelsMatch(prLabels, ignoreLabels, selectLabels) {
23+
if !labelsMatch(prLabels, ignoreLabels, selectLabels, caseSensitiveLabels) {
2424
return false
2525
}
2626

@@ -72,13 +72,14 @@ func branchMatchesCriteria(branch string) bool {
7272
return true
7373
}
7474

75-
func labelsMatch(prLabels, ignoreLabels, selectLabels []string) bool {
75+
func labelsMatch(prLabels, ignoreLabels, selectLabels []string, caseSensitive bool) bool {
7676
// If no ignoreLabels or selectLabels are specified, all labels pass this check
7777
if len(ignoreLabels) == 0 && len(selectLabels) == 0 {
7878
return true
7979
}
8080

81-
if !caseSensitiveLabels {
81+
// Normalize labels for case-insensitive matching if caseSensitive is false
82+
if !caseSensitive {
8283
prLabels = common.NormalizeArray(prLabels)
8384
ignoreLabels = common.NormalizeArray(ignoreLabels)
8485
selectLabels = common.NormalizeArray(selectLabels)

internal/cmd/match_criteria_test.go

Lines changed: 83 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -18,101 +18,115 @@ func TestLabelsMatch(t *testing.T) {
1818
},
1919

2020
{
21-
name: "--ignore-labels match",
22-
prLabels: []string{"a", "b"},
23-
ignoreLabels: []string{"b"},
24-
want: false,
21+
name: "--ignore-labels match",
22+
prLabels: []string{"a", "b"},
23+
ignoreLabels: []string{"b"},
24+
want: false,
25+
caseSensitive: false,
2526
},
2627
{
27-
name: "--ignore-labels match (with one out of two)",
28-
prLabels: []string{"a", "b"},
29-
ignoreLabels: []string{"b", "c"},
30-
want: false,
28+
name: "--ignore-labels match (with one out of two)",
29+
prLabels: []string{"a", "b"},
30+
ignoreLabels: []string{"b", "c"},
31+
want: false,
32+
caseSensitive: false,
3133
},
3234

3335
{
34-
name: "no labels match (select or ignore)",
35-
prLabels: []string{"a"},
36-
ignoreLabels: []string{"b"},
37-
selectLabels: []string{"c"},
38-
want: false,
36+
name: "no labels match (select or ignore)",
37+
prLabels: []string{"a"},
38+
ignoreLabels: []string{"b"},
39+
selectLabels: []string{"c"},
40+
want: false,
41+
caseSensitive: false,
3942
},
4043
{
41-
name: "--select-labels match",
42-
prLabels: []string{"a", "c"},
43-
ignoreLabels: []string{"b"},
44-
selectLabels: []string{"c"},
45-
want: true,
44+
name: "--select-labels match",
45+
prLabels: []string{"a", "c"},
46+
ignoreLabels: []string{"b"},
47+
selectLabels: []string{"c"},
48+
want: true,
49+
caseSensitive: false,
4650
},
4751
{
48-
name: "--select-labels match (with one out of two) and ignore labels don't match",
49-
prLabels: []string{"a"},
50-
ignoreLabels: []string{"b"},
51-
selectLabels: []string{"a", "c"},
52-
want: true,
52+
name: "--select-labels match (with one out of two) and ignore labels don't match",
53+
prLabels: []string{"a"},
54+
ignoreLabels: []string{"b"},
55+
selectLabels: []string{"a", "c"},
56+
want: true,
57+
caseSensitive: false,
5358
},
5459
{
55-
name: "the pull request has no labels",
56-
prLabels: []string{},
57-
ignoreLabels: []string{"b"},
58-
selectLabels: []string{"a", "c"},
59-
want: false,
60+
name: "the pull request has no labels",
61+
prLabels: []string{},
62+
ignoreLabels: []string{"b"},
63+
selectLabels: []string{"a", "c"},
64+
want: false,
65+
caseSensitive: false,
6066
},
6167
{
62-
name: "the pull request has no labels and ignore labels don't match so it matches - but select labels is empty so it means all labels or even no labels match",
63-
prLabels: []string{},
64-
ignoreLabels: []string{"b"},
65-
selectLabels: []string{},
66-
want: true,
68+
name: "the pull request has no labels and ignore labels don't match so it matches - but select labels is empty so it means all labels or even no labels match",
69+
prLabels: []string{},
70+
ignoreLabels: []string{"b"},
71+
selectLabels: []string{},
72+
want: true,
73+
caseSensitive: false,
6774
},
6875
{
69-
name: "the pull request has no labels but we want to match the a label",
70-
prLabels: []string{},
71-
ignoreLabels: []string{},
72-
selectLabels: []string{"a"},
73-
want: false,
76+
name: "the pull request has no labels but we want to match the a label",
77+
prLabels: []string{},
78+
ignoreLabels: []string{},
79+
selectLabels: []string{"a"},
80+
want: false,
81+
caseSensitive: false,
7482
},
7583
{
76-
name: "no label match criteria, so it matches",
77-
prLabels: []string{},
78-
ignoreLabels: []string{},
79-
selectLabels: []string{},
80-
want: true,
84+
name: "no label match criteria, so it matches",
85+
prLabels: []string{},
86+
ignoreLabels: []string{},
87+
selectLabels: []string{},
88+
want: true,
89+
caseSensitive: false,
8190
},
8291
{
83-
name: "with one matching label and no matching ignore labels so it matches",
84-
prLabels: []string{"a"},
85-
selectLabels: []string{"a"},
86-
ignoreLabels: []string{"b"},
87-
want: true,
92+
name: "with one matching label and no matching ignore labels so it matches",
93+
prLabels: []string{"a"},
94+
selectLabels: []string{"a"},
95+
ignoreLabels: []string{"b"},
96+
want: true,
97+
caseSensitive: false,
8898
},
8999
{
90-
name: "the pr labels match the select and ignore labels so it doesn't match",
91-
prLabels: []string{"a"},
92-
selectLabels: []string{"a"},
93-
ignoreLabels: []string{"a"},
94-
want: false,
100+
name: "the pr labels match the select and ignore labels so it doesn't match",
101+
prLabels: []string{"a"},
102+
selectLabels: []string{"a"},
103+
ignoreLabels: []string{"a"},
104+
want: false,
105+
caseSensitive: false,
95106
},
96107
{
97-
name: "the pr has one label but no defined ignore or select labels so it matches",
98-
prLabels: []string{"a"},
99-
selectLabels: []string{},
100-
ignoreLabels: []string{},
101-
want: true,
108+
name: "the pr has one label but no defined ignore or select labels so it matches",
109+
prLabels: []string{"a"},
110+
selectLabels: []string{},
111+
ignoreLabels: []string{},
112+
want: true,
113+
caseSensitive: false,
102114
},
103115
{
104-
name: "the pr has one label and it is the select label so it matches",
105-
prLabels: []string{"a"},
106-
selectLabels: []string{"a"},
107-
ignoreLabels: []string{},
108-
want: true,
116+
name: "the pr has one label and it is the select label so it matches",
117+
prLabels: []string{"a"},
118+
selectLabels: []string{"a"},
119+
ignoreLabels: []string{},
120+
want: true,
121+
caseSensitive: false,
109122
},
110123
{
111-
name: "the pr has labels and matching select labels but it matches an ignore label so it doesn't match",
112-
prLabels: []string{"a", "b", "c"},
113-
selectLabels: []string{"a", "b"},
114-
ignoreLabels: []string{"c"},
115-
want: false,
124+
name: "the pr has labels and matching select labels but it matches an ignore label so it doesn't match",
125+
prLabels: []string{"a", "b", "c"},
126+
selectLabels: []string{"a", "b"},
127+
ignoreLabels: []string{"c"},
128+
want: false,
129+
caseSensitive: false,
116130
},
117131
{
118132
name: "the pr has uppercase labels and we are using case insensitive labels so it matches",
@@ -144,7 +158,7 @@ func TestLabelsMatch(t *testing.T) {
144158
caseSensitiveLabels = test.caseSensitive
145159

146160
// Run the function
147-
got := labelsMatch(test.prLabels, test.ignoreLabels, test.selectLabels)
161+
got := labelsMatch(test.prLabels, test.ignoreLabels, test.selectLabels, test.caseSensitive)
148162
if got != test.want {
149163
t.Errorf("Test %q failed: want %v, got %v", test.name, test.want, got)
150164
}

0 commit comments

Comments
 (0)