Skip to content

Commit 76d281f

Browse files
authored
ignore case when evaluating labels (#64)
1 parent c368ee1 commit 76d281f

4 files changed

Lines changed: 72 additions & 11 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ merge:
3030
# "whitelist" defines how to select PRs to evaluate and merge
3131
whitelist:
3232

33-
# "labels" is a list of labels that must be matched to whitelist a PR for merging
33+
# "labels" is a list of labels that must be matched to whitelist a PR for merging (case-insensitive)
3434
labels: ["merge when ready"]
3535

3636
# "comment_substrings" matches on substrings in comments
@@ -39,7 +39,7 @@ merge:
3939
# "blacklist" defines how to exclude PRs from evaluation and merging
4040
blacklist:
4141

42-
# similar as above, "labels" defines a list of labels. In this case, matched labels cause exclusion.
42+
# similar as above, "labels" defines a list of labels. In this case, matched labels cause exclusion. (case-insensitive)
4343
labels: ["do not merge"]
4444

4545
# "comment_substrings" matches substrings in comments. In this case, matched substrings cause exclusion.

bulldozer/config_fetcher.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,12 @@ func (cf *ConfigFetcher) unmarshalConfigV0(bytes []byte) (*Config, error) {
166166
Version: 1,
167167
Update: UpdateConfig{
168168
Whitelist: Signals{
169-
Labels: []string{"update me", "Update Me", "UPDATE ME", "update-me", "Update-Me", "UPDATE-ME", "update_me", "Update_Me", "UPDATE_ME"},
169+
Labels: []string{"update me", "update-me", "update_me"},
170170
},
171171
},
172172
Merge: MergeConfig{
173173
Whitelist: Signals{
174-
Labels: []string{"merge when ready", "Merge When Ready", "MERGE WHEN READY", "merge-when-ready", "Merge-When-Ready", "MERGE-WHEN-READY", "merge_when_ready", "Merge_When_Ready", "MERGE_WHEN_READY"},
174+
Labels: []string{"merge when ready", "merge-when-ready", "merge_when_ready"},
175175
},
176176
DeleteAfterMerge: configv0.DeleteAfterMerge,
177177
Method: configv0.Strategy,
@@ -185,12 +185,12 @@ func (cf *ConfigFetcher) unmarshalConfigV0(bytes []byte) (*Config, error) {
185185
Version: 1,
186186
Update: UpdateConfig{
187187
Whitelist: Signals{
188-
Labels: []string{"update me", "Update Me", "UPDATE ME", "update-me", "Update-Me", "UPDATE-ME", "update_me", "Update_Me", "UPDATE_ME"},
188+
Labels: []string{"update me", "update-me", "update_me"},
189189
},
190190
},
191191
Merge: MergeConfig{
192192
Blacklist: Signals{
193-
Labels: []string{"do not merge", "Do Not Merge", "DO NOT MERGE", "wip", "WIP", "do-not-merge", "Do-Not-Merge", "DO-NOT-MERGE", "do_not_merge", "Do_Not_Merge", "DO_NOT_MERGE"},
193+
Labels: []string{"wip", "do not merge", "do-not-merge", "do_not_merge"},
194194
},
195195
DeleteAfterMerge: configv0.DeleteAfterMerge,
196196
Method: configv0.Strategy,
@@ -204,7 +204,7 @@ func (cf *ConfigFetcher) unmarshalConfigV0(bytes []byte) (*Config, error) {
204204
Version: 1,
205205
Update: UpdateConfig{
206206
Whitelist: Signals{
207-
Labels: []string{"update me", "Update Me", "UPDATE ME", "update-me", "Update-Me", "UPDATE-ME", "update_me", "Update_Me", "UPDATE_ME"},
207+
Labels: []string{"update me", "update-me", "update_me"},
208208
},
209209
},
210210
Merge: MergeConfig{

bulldozer/evaluate.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func IsPRBlacklisted(ctx context.Context, pullCtx pull.Context, config Signals)
3333
return true, "unable to list PR labels", err
3434
}
3535

36-
if inSlice, idx := anyInSlice(labels, config.Labels); inSlice {
36+
if inSlice, idx := anyInSliceCaseInsensitive(labels, config.Labels); inSlice {
3737
return true, fmt.Sprintf("PR label matches one of specified blacklist labels: %q", config.Labels[idx]), nil
3838
}
3939

@@ -80,7 +80,7 @@ func IsPRWhitelisted(ctx context.Context, pullCtx pull.Context, config Signals)
8080
return false, "unable to list PR labels", err
8181
}
8282

83-
if inSlice, idx := anyInSlice(labels, config.Labels); inSlice {
83+
if inSlice, idx := anyInSliceCaseInsensitive(labels, config.Labels); inSlice {
8484
return true, fmt.Sprintf("PR label matches one of specified whitelist labels: %q", config.Labels[idx]), nil
8585
}
8686

@@ -130,6 +130,18 @@ func anyInSlice(testValues []string, elements []string) (bool, int) {
130130
return false, -1
131131
}
132132

133+
func anyInSliceCaseInsensitive(testValues []string, elements []string) (bool, int) {
134+
for _, testValue := range testValues {
135+
for index, element := range elements {
136+
if strings.EqualFold(testValue, element) {
137+
return true, index
138+
}
139+
}
140+
}
141+
142+
return false, -1
143+
}
144+
133145
// setDifference returns all elements in set1 that
134146
// are not in set2.
135147
func setDifference(set1, set2 []string) []string {

bulldozer/evaluate_test.go

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,40 @@ func TestSimpleXListed(t *testing.T) {
133133
require.Nil(t, err)
134134
assert.True(t, actualBlacklist)
135135
assert.Equal(t, "PR label matches one of specified blacklist labels: \"LABEL_NOMERGE\"", actualBlacklistReason)
136+
})
137+
138+
t.Run("labelCausesBlacklistCaseInsensitive", func(t *testing.T) {
139+
pc := &pulltest.MockPullContext{
140+
LabelValue: []string{"LABEL_nomERGE"},
141+
}
142+
143+
actualBlacklist, actualBlacklistReason, err := IsPRBlacklisted(ctx, pc, mergeConfig.Blacklist)
144+
require.Nil(t, err)
145+
assert.True(t, actualBlacklist)
146+
assert.Equal(t, "PR label matches one of specified blacklist labels: \"LABEL_NOMERGE\"", actualBlacklistReason)
147+
})
148+
149+
t.Run("labelCausesWhitelist", func(t *testing.T) {
150+
pc := &pulltest.MockPullContext{
151+
LabelValue: []string{"LABEL_MERGE"},
152+
}
136153

137154
actualWhitelist, actualWhitelistReason, err := IsPRWhitelisted(ctx, pc, mergeConfig.Whitelist)
138155
require.Nil(t, err)
139-
assert.False(t, actualWhitelist)
140-
assert.Equal(t, "no matching whitelist found", actualWhitelistReason)
156+
assert.True(t, actualWhitelist)
157+
assert.Equal(t, "PR label matches one of specified whitelist labels: \"LABEL_MERGE\"", actualWhitelistReason)
141158
})
142159

160+
t.Run("labelCausesWhitelistCaseInsensitive", func(t *testing.T) {
161+
pc := &pulltest.MockPullContext{
162+
LabelValue: []string{"LABEL_meRGE"},
163+
}
164+
165+
actualWhitelist, actualWhitelistReason, err := IsPRWhitelisted(ctx, pc, mergeConfig.Whitelist)
166+
require.Nil(t, err)
167+
assert.True(t, actualWhitelist)
168+
assert.Equal(t, "PR label matches one of specified whitelist labels: \"LABEL_MERGE\"", actualWhitelistReason)
169+
})
143170
}
144171

145172
func TestShouldMerge(t *testing.T) {
@@ -191,6 +218,17 @@ func TestShouldMerge(t *testing.T) {
191218
assert.True(t, actualShouldMerge)
192219
})
193220

221+
t.Run("labelShouldMergeCaseInsensitive", func(t *testing.T) {
222+
pc := &pulltest.MockPullContext{
223+
LabelValue: []string{"LABEL_merGE"},
224+
}
225+
226+
actualShouldMerge, err := ShouldMergePR(ctx, pc, mergeConfig)
227+
228+
require.Nil(t, err)
229+
assert.True(t, actualShouldMerge)
230+
})
231+
194232
t.Run("noContextShouldntMerge", func(t *testing.T) {
195233
pc := &pulltest.MockPullContext{
196234
LabelValue: []string{"NOT_A_LABEL"},
@@ -235,6 +273,17 @@ func TestShouldMerge(t *testing.T) {
235273
assert.False(t, actualShouldMerge)
236274
})
237275

276+
t.Run("labelCausesBlacklistCaseInsensitive", func(t *testing.T) {
277+
pc := &pulltest.MockPullContext{
278+
LabelValue: []string{"LABEL_nomERGE"},
279+
}
280+
281+
actualShouldMerge, err := ShouldMergePR(ctx, pc, mergeConfig)
282+
283+
require.Nil(t, err)
284+
assert.False(t, actualShouldMerge)
285+
})
286+
238287
t.Run("substringCausesWhitelist", func(t *testing.T) {
239288
pc := &pulltest.MockPullContext{
240289
LabelValue: []string{"NOT_A_LABEL"},

0 commit comments

Comments
 (0)