Skip to content

Commit

Permalink
Make issue labeler require exact match (#9322)
Browse files Browse the repository at this point in the history
  • Loading branch information
trodge authored Oct 24, 2023
1 parent fb15c69 commit 0f7d5b2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
8 changes: 5 additions & 3 deletions tools/issue-labeler/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"sort"

_ "embed"

"github.com/golang/glog"
"gopkg.in/yaml.v2"
)
Expand Down Expand Up @@ -38,8 +39,9 @@ func buildRegexLabels(teamsYaml []byte) ([]regexpLabel, error) {

for label, data := range enrolledTeams {
for _, resource := range data.Resources {
exactResource := fmt.Sprintf("^%s$", resource)
regexpLabels = append(regexpLabels, regexpLabel{
Regexp: regexp.MustCompile(resource),
Regexp: regexp.MustCompile(exactResource),
Label: label,
})
}
Expand Down Expand Up @@ -76,8 +78,8 @@ func computeLabels(resources []string, regexpLabels []regexpLabel) []string {

var labels []string

for l, _ := range labelSet {
labels = append(labels, l)
for label := range labelSet {
labels = append(labels, label)
}
sort.Strings(labels)

Expand Down
30 changes: 22 additions & 8 deletions tools/issue-labeler/labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ service/service2:
- google_service2_resource2`),
expectedRegexpLabels: []regexpLabel{
{
Regexp: regexp.MustCompile("google_service1_.*"),
Regexp: regexp.MustCompile("^google_service1_.*$"),
Label: "service/service1",
},
{
Regexp: regexp.MustCompile("google_service2_resource1"),
Regexp: regexp.MustCompile("^google_service2_resource1$"),
Label: "service/service2",
},
{
Regexp: regexp.MustCompile("google_service2_resource2"),
Regexp: regexp.MustCompile("^google_service2_resource2$"),
Label: "service/service2",
},
},
Expand All @@ -93,7 +93,7 @@ service/service1:
- google_service1_resource1`),
expectedRegexpLabels: []regexpLabel{
{
Regexp: regexp.MustCompile("google_service1_resource1"),
Regexp: regexp.MustCompile("^google_service1_resource1$"),
Label: "service/service1",
},
},
Expand All @@ -119,21 +119,25 @@ service/service1:
func TestComputeLabels(t *testing.T) {
defaultRegexpLabels := []regexpLabel{
{
Regexp: regexp.MustCompile("google_service1_.*"),
Regexp: regexp.MustCompile("^google_service1_.*$"),
Label: "service/service1",
},
{
Regexp: regexp.MustCompile("google_service2_resource1"),
Regexp: regexp.MustCompile("^google_service2_resource1$"),
Label: "service/service2-subteam1",
},
{
Regexp: regexp.MustCompile("google_service2_resource2"),
Regexp: regexp.MustCompile("^google_service2_resource2$"),
Label: "service/service2-subteam2",
},
{
Regexp: regexp.MustCompile("google_service1_resource5"),
Regexp: regexp.MustCompile("^google_service1_resource5$"),
Label: "service/service1-subteam1",
},
{
Regexp: regexp.MustCompile("^google_resource6$"),
Label: "service/service3",
},
}
cases := map[string]struct {
resources []string
Expand All @@ -160,6 +164,16 @@ func TestComputeLabels(t *testing.T) {
regexpLabels: defaultRegexpLabels,
expectedLabels: []string{"service/service1"},
},
"exact resource match": {
resources: []string{"google_resource6"},
regexpLabels: defaultRegexpLabels,
expectedLabels: []string{"service/service3"},
},
"no partial match allowed": {
resources: []string{"google_resource6_foo"},
regexpLabels: defaultRegexpLabels,
expectedLabels: []string{},
},
"only return first label matching a resource": {
resources: []string{"google_service1_resource5"},
regexpLabels: defaultRegexpLabels,
Expand Down

0 comments on commit 0f7d5b2

Please sign in to comment.