Skip to content

Commit 6541b0a

Browse files
committed
improve label matching checks and tests
1 parent 9eb4aea commit 6541b0a

File tree

2 files changed

+52
-10
lines changed

2 files changed

+52
-10
lines changed

internal/cmd/match_criteria.go

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,39 @@ func branchMatchesCriteria(branch string) bool {
6060
}
6161

6262
func labelsMatch(prLabels, ignoreLabels, selectLabels []string) bool {
63-
for _, l := range ignoreLabels {
64-
if i := slices.Index(prLabels, l); i != -1 {
65-
return false
66-
}
63+
// If no ignoreLabels or selectLabels are specified, all labels pass this check
64+
if len(ignoreLabels) == 0 && len(selectLabels) == 0 {
65+
return true
6766
}
6867

69-
for _, l := range selectLabels {
70-
found := false
71-
if i := slices.Index(prLabels, l); i != -1 {
72-
break
68+
// If the pull request contains any of the ignore labels, it doesn't match
69+
if len(ignoreLabels) > 0 && len(prLabels) > 0 {
70+
for _, l := range ignoreLabels {
71+
if i := slices.Index(prLabels, l); i != -1 {
72+
return false
73+
}
7374
}
75+
}
7476

75-
if !found {
76-
return false
77+
// If ignoreLabels are specified without selectLabels and the pull request has no labels, it matches
78+
if len(ignoreLabels) > 0 && len(selectLabels) == 0 && len(prLabels) == 0 {
79+
return true
80+
}
81+
82+
// If selectLabels are specified but the pull request has no labels, it doesn't match
83+
if len(selectLabels) > 0 && len(prLabels) == 0 {
84+
return false
85+
}
86+
87+
// If the pull request contains any of the select labels, it matches
88+
if len(selectLabels) > 0 && len(prLabels) > 0 {
89+
for _, l := range selectLabels {
90+
if i := slices.Index(prLabels, l); i != -1 {
91+
return true
92+
}
7793
}
94+
// If none of the select labels are found, it doesn't match
95+
return false
7896
}
7997

8098
return true

internal/cmd/match_criteria_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,30 @@ func TestLabelsMatch(t *testing.T) {
4444
selectLabels: []string{"a", "c"},
4545
want: true,
4646
},
47+
{
48+
prLabels: []string{},
49+
ignoreLabels: []string{"b"},
50+
selectLabels: []string{"a", "c"},
51+
want: false,
52+
},
53+
{
54+
prLabels: []string{},
55+
ignoreLabels: []string{"b"},
56+
selectLabels: []string{},
57+
want: true,
58+
},
59+
{
60+
prLabels: []string{},
61+
ignoreLabels: []string{},
62+
selectLabels: []string{"a"},
63+
want: false,
64+
},
65+
{
66+
prLabels: []string{},
67+
ignoreLabels: []string{},
68+
selectLabels: []string{},
69+
want: true,
70+
},
4771
}
4872

4973
for _, test := range tests {

0 commit comments

Comments
 (0)