nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: srvaroa/labeler
Pull request #7 (2020-05-10 21:07:53)

Add branch matcher
for PR #6 


Patch:
diff --git a/pkg/labeler.go b/pkg/labeler.go
index 96fe7a7..18ceeba 100644
--- a/pkg/labeler.go
+++ b/pkg/labeler.go
@@ -13,6 +13,7 @@ import (
 type LabelerConfig map[string]LabelMatcher
 type LabelMatcher struct {
 	Title     string
+	Branch    string
 	Mergeable string
 	SizeBelow string `yaml:"size-below"`
 	SizeAbove string `yaml:"size-above"`
@@ -50,6 +51,23 @@ func NewTitleCondition() Condition {
 	}
 }
 
+func NewBranchCondition() Condition {
+	return Condition{
+		GetName: func() string {
+			return "Branch matches regex"
+		},
+		Evaluate: func(pr *gh.PullRequest, matcher LabelMatcher) (bool, error) {
+			if len(matcher.Branch) <= 0 {
+				return false, fmt.Errorf("branch is not set in config")
+			}
+			prBranchName := pr.Head.GetRef()
+			log.Printf("Matching `%s` against: `%s`", matcher.Branch, prBranchName)
+			isMatched, _ := regexp.Match(matcher.Title, []byte(prBranchName))
+			return isMatched, nil
+		},
+	}
+}
+
 func NewIsMergeableCondition() Condition {
 	return Condition{
 		GetName: func() string {
@@ -163,6 +181,7 @@ func (l *Labeler) findMatches(pr *gh.PullRequest, config *LabelerConfig) (LabelU
 	}
 	conditions := []Condition{
 		NewTitleCondition(),
+		NewBranchCondition(),
 		NewIsMergeableCondition(),
 		NewSizeCondition(),
 	}


Test patch:
diff --git a/pkg/labeler_test.go b/pkg/labeler_test.go
index b0dbcb4..3421531 100644
--- a/pkg/labeler_test.go
+++ b/pkg/labeler_test.go
@@ -150,6 +150,17 @@ func TestHandleEvent(t *testing.T) {
 			initialLabels:  []string{},
 			expectedLabels: []string{"L"},
 		},
+		TestCase{
+			payloads: []string{"small_pr"},
+			name:     "Test the branch rule",
+			config: LabelerConfig{
+				"Branch": LabelMatcher{
+					Branch: "^srvaroa-patch.*",
+				},
+			},
+			initialLabels:  []string{},
+			expectedLabels: []string{"Branch"},
+		},
 	}
 
 	for _, tc := range testCases {
Repository: srvaroa/labeler
Pull request #10 (2020-05-11 22:48:24)

Fix bug where branch matcher was looking at title
Also added a test for not matching.


Patch:
diff --git a/pkg/labeler.go b/pkg/labeler.go
index 18ceeba..49d478c 100644
--- a/pkg/labeler.go
+++ b/pkg/labeler.go
@@ -62,7 +62,7 @@ func NewBranchCondition() Condition {
 			}
 			prBranchName := pr.Head.GetRef()
 			log.Printf("Matching `%s` against: `%s`", matcher.Branch, prBranchName)
-			isMatched, _ := regexp.Match(matcher.Title, []byte(prBranchName))
+			isMatched, _ := regexp.Match(matcher.Branch, []byte(prBranchName))
 			return isMatched, nil
 		},
 	}


Test patch:
diff --git a/pkg/labeler_test.go b/pkg/labeler_test.go
index 3421531..7525134 100644
--- a/pkg/labeler_test.go
+++ b/pkg/labeler_test.go
@@ -152,7 +152,7 @@ func TestHandleEvent(t *testing.T) {
 		},
 		TestCase{
 			payloads: []string{"small_pr"},
-			name:     "Test the branch rule",
+			name:     "Test the branch rule (matching)",
 			config: LabelerConfig{
 				"Branch": LabelMatcher{
 					Branch: "^srvaroa-patch.*",
@@ -161,6 +161,17 @@ func TestHandleEvent(t *testing.T) {
 			initialLabels:  []string{},
 			expectedLabels: []string{"Branch"},
 		},
+		TestCase{
+			payloads: []string{"small_pr"},
+			name:     "Test the branch rule (not matching)",
+			config: LabelerConfig{
+				"Branch": LabelMatcher{
+					Branch: "^does/not-match/*",
+				},
+			},
+			initialLabels:  []string{},
+			expectedLabels: []string{},
+		},
 	}
 
 	for _, tc := range testCases {
Repository: srvaroa/labeler
Pull request #14 (2020-09-12 13:57:42)

Correct calculation on multiple conditions
The previous version would fail in some cases with multiple conditions
for a single label depending on the order of evaluation of conditions.

Ref. #11

Signed-off-by: Galo Navarro <anglorvaroa@gmail.com>


Patch:
diff --git a/go.mod b/go.mod
index 312b8b0..1536a11 100644
--- a/go.mod
+++ b/go.mod
@@ -7,7 +7,7 @@ require (
 	github.com/google/go-cmp v0.3.1
 	github.com/google/go-github/v27 v27.0.6
 	github.com/kr/pretty v0.1.0 // indirect
+	github.com/waigani/diffparser v0.0.0-20190828052634-7391f219313d
 	golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be
 	gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
-	github.com/waigani/diffparser v0.0.0-20190828052634-7391f219313d
 )
diff --git a/pkg/labeler.go b/pkg/labeler.go
index 7b3077f..9a84c2d 100644
--- a/pkg/labeler.go
+++ b/pkg/labeler.go
@@ -236,7 +236,14 @@ func (l *Labeler) findMatches(pr *gh.PullRequest, config *LabelerConfig) (LabelU
 				continue
 			}
 
-			labelUpdates.set[label] = isMatched
+			prev, ok := labelUpdates.set[label]
+
+			if ok { // Other conditions were evaluated for the label
+				labelUpdates.set[label] = prev && isMatched
+			} else { // First condition evaluated for this label
+				labelUpdates.set[label] = isMatched
+			}
+
 			log.Printf("%s: condition %s yields %t", label, c.GetName(), isMatched)
 			if isMatched {
 				continue


Test patch:
diff --git a/pkg/labeler_test.go b/pkg/labeler_test.go
index 7675f21..3d7f758 100644
--- a/pkg/labeler_test.go
+++ b/pkg/labeler_test.go
@@ -106,7 +106,7 @@ func TestHandleEvent(t *testing.T) {
 		},
 		TestCase{
 			payloads: []string{"create_pr", "reopen_pr"},
-			name:     "Add a label with two conditions, one not matching",
+			name:     "Add a label with two conditions, one not matching (1)",
 			config: LabelerConfig{
 				"WIP": LabelMatcher{
 					Title:     "^WIP:.*",
@@ -116,6 +116,20 @@ func TestHandleEvent(t *testing.T) {
 			initialLabels:  []string{},
 			expectedLabels: []string{},
 		},
+		TestCase{
+			// covers evaluation order making a True in the last
+			// condition, while previous ones are false
+			payloads: []string{"create_pr", "reopen_pr"},
+			name:     "Add a label with two conditions, one not matching (2)",
+			config: LabelerConfig{
+				"WIP": LabelMatcher{
+					Title:     "^DOES NOT MATCH:.*",
+					Mergeable: "False",
+				},
+			},
+			initialLabels:  []string{},
+			expectedLabels: []string{},
+		},
 		TestCase{
 			payloads: []string{"small_pr"},
 			name:     "Test the size_below rule",