From 0f7d5b28b4d8ca2f598e6c68b60d758da277fc1f Mon Sep 17 00:00:00 2001 From: Thomas Rodgers Date: Tue, 24 Oct 2023 22:40:17 +0000 Subject: [PATCH] Make issue labeler require exact match (#9322) --- tools/issue-labeler/labels.go | 8 +++++--- tools/issue-labeler/labels_test.go | 30 ++++++++++++++++++++++-------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/tools/issue-labeler/labels.go b/tools/issue-labeler/labels.go index b4fd8f233104..588588f65120 100644 --- a/tools/issue-labeler/labels.go +++ b/tools/issue-labeler/labels.go @@ -6,6 +6,7 @@ import ( "sort" _ "embed" + "github.com/golang/glog" "gopkg.in/yaml.v2" ) @@ -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, }) } @@ -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) diff --git a/tools/issue-labeler/labels_test.go b/tools/issue-labeler/labels_test.go index be1813938bc4..1112cd9565e2 100644 --- a/tools/issue-labeler/labels_test.go +++ b/tools/issue-labeler/labels_test.go @@ -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", }, }, @@ -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", }, }, @@ -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 @@ -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,