Skip to content

Commit

Permalink
Untangle label regexps
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmichalis committed May 16, 2018
1 parent 86a6ceb commit a8b5ebc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
14 changes: 9 additions & 5 deletions prow/plugins/label/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ import (
const pluginName = "label"

var (
labelRegex = regexp.MustCompile(`(?m)^/(area|committee|kind|priority|sig|triage|label)\s*(.*)$`)
removeLabelRegex = regexp.MustCompile(`(?m)^/remove-(area|committee|kind|priority|sig|triage|label)\s*(.*)$`)
labelRegex = regexp.MustCompile(`(?m)^/(area|committee|kind|priority|sig|triage)\s*(.*)$`)
removeLabelRegex = regexp.MustCompile(`(?m)^/remove-(area|committee|kind|priority|sig|triage)\s*(.*)$`)
customLabelRegex = regexp.MustCompile(`(?m)^/label\s*(.*)$`)
customRemoveLabelRegex = regexp.MustCompile(`(?m)^/remove-label\s*(.*)$`)
nonExistentLabelOnIssue = "Those labels are not set on the issue: `%v`"
)

Expand Down Expand Up @@ -106,7 +108,9 @@ func getLabelsFromGenericMatches(matches [][]string, additionalLabels []string)
func handle(gc githubClient, log *logrus.Entry, additionalLabels []string, e *github.GenericCommentEvent) error {
labelMatches := labelRegex.FindAllStringSubmatch(e.Body, -1)
removeLabelMatches := removeLabelRegex.FindAllStringSubmatch(e.Body, -1)
if len(labelMatches) == 0 && len(removeLabelMatches) == 0 {
customLabelMatches := customLabelRegex.FindAllStringSubmatch(e.Body, -1)
customRemoveLabelMatches := customRemoveLabelRegex.FindAllStringSubmatch(e.Body, -1)
if len(labelMatches) == 0 && len(removeLabelMatches) == 0 && len(customLabelMatches) == 0 && len(customRemoveLabelMatches) == 0 {
return nil
}

Expand Down Expand Up @@ -134,8 +138,8 @@ func handle(gc githubClient, log *logrus.Entry, additionalLabels []string, e *gi
)

// Get labels to add and labels to remove from regexp matches
labelsToAdd = append(getLabelsFromREMatches(labelMatches), getLabelsFromGenericMatches(labelMatches, additionalLabels)...)
labelsToRemove = append(getLabelsFromREMatches(removeLabelMatches), getLabelsFromGenericMatches(removeLabelMatches, additionalLabels)...)
labelsToAdd = append(getLabelsFromREMatches(labelMatches), getLabelsFromGenericMatches(customLabelMatches, additionalLabels)...)
labelsToRemove = append(getLabelsFromREMatches(removeLabelMatches), getLabelsFromGenericMatches(customRemoveLabelMatches, additionalLabels)...)

// Add labels
for _, labelToAdd := range labelsToAdd {
Expand Down
17 changes: 14 additions & 3 deletions prow/plugins/label/label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func TestLabel(t *testing.T) {
extraLabels []string
expectedNewLabels []string
expectedRemovedLabels []string
expectedBotComment bool
repoLabels []string
issueLabels []string
}
Expand Down Expand Up @@ -252,6 +253,7 @@ func TestLabel(t *testing.T) {
expectedNewLabels: []string{},
expectedRemovedLabels: []string{},
commenter: orgMember,
expectedBotComment: true,
},
{
name: "Remove Area Label when no such Label on Issue",
Expand All @@ -261,6 +263,7 @@ func TestLabel(t *testing.T) {
expectedNewLabels: []string{},
expectedRemovedLabels: []string{},
commenter: orgMember,
expectedBotComment: true,
},
{
name: "Remove Area Label",
Expand Down Expand Up @@ -324,6 +327,7 @@ func TestLabel(t *testing.T) {
expectedNewLabels: []string{},
expectedRemovedLabels: formatLabels("priority/low", "priority/high", "kind/api-server", "area/infra"),
commenter: orgMember,
expectedBotComment: true,
},
{
name: "Add and Remove Label at the same time",
Expand Down Expand Up @@ -415,6 +419,7 @@ func TestLabel(t *testing.T) {
}

for _, tc := range testcases {
t.Logf("Running scenario %q", tc.name)
sort.Strings(tc.expectedNewLabels)
fakeClient := &fakegithub.FakeClient{
Issues: make([]github.Issue, 1),
Expand All @@ -437,7 +442,7 @@ func TestLabel(t *testing.T) {
}
err := handle(fakeClient, logrus.WithField("plugin", pluginName), tc.extraLabels, e)
if err != nil {
t.Errorf("For case %s, didn't expect error from label test: %v", tc.name, err)
t.Errorf("didn't expect error from label test: %v", err)
continue
}

Expand All @@ -449,13 +454,19 @@ func TestLabel(t *testing.T) {
sort.Strings(expectLabels)
sort.Strings(fakeClient.LabelsAdded)
if !reflect.DeepEqual(expectLabels, fakeClient.LabelsAdded) {
t.Errorf("(%s): Expected the labels %q to be added, but %q were added.", tc.name, expectLabels, fakeClient.LabelsAdded)
t.Errorf("expected the labels %q to be added, but %q were added.", expectLabels, fakeClient.LabelsAdded)
}

sort.Strings(tc.expectedRemovedLabels)
sort.Strings(fakeClient.LabelsRemoved)
if !reflect.DeepEqual(tc.expectedRemovedLabels, fakeClient.LabelsRemoved) {
t.Errorf("(%s): Expected the labels %q to be removed, but %q were removed.", tc.name, tc.expectedRemovedLabels, fakeClient.LabelsRemoved)
t.Errorf("expected the labels %q to be removed, but %q were removed.", tc.expectedRemovedLabels, fakeClient.LabelsRemoved)
}
if len(fakeClient.IssueCommentsAdded) > 0 && !tc.expectedBotComment {
t.Errorf("unexpected bot comments: %#v", fakeClient.IssueCommentsAdded)
}
if len(fakeClient.IssueCommentsAdded) == 0 && tc.expectedBotComment {
t.Error("expected a bot comment but got none")
}
}
}

0 comments on commit a8b5ebc

Please sign in to comment.