Repository: srvaroa/labeler
Pull request #45 (2022-07-16 02:49:32)

Add label matching for authors
Adds a matcher for PR author. 

Tests and README updated.

srvaroa: Thanks!

Patch:
diff --git a/README.md b/README.md
index abecfa2..23827a9 100644
--- a/README.md
+++ b/README.md
@@ -165,6 +165,14 @@ This condition is satisfied when the PR is in a [mergeable state](https://develo
 mergeable: true
 ```
 
+### Match to PR Author
+
+This condition is satisfied when the PR author matches any of the given usernames.
+
+```yaml
+author: "serubin"
+```
+
 ### PR size
 
 This condition is satisfied when the total number of changed lines in
diff --git a/pkg/labeler.go b/pkg/labeler.go
index da2e340..e08c1db 100644
--- a/pkg/labeler.go
+++ b/pkg/labeler.go
@@ -22,6 +22,7 @@ type LabelMatcher struct {
 	BaseBranch string `yaml:"base-branch"`
 	Body       string
 	Files      []string
+	Authors    []string
 	Mergeable  string
 	SizeBelow  string `yaml:"size-below"`
 	SizeAbove  string `yaml:"size-above"`
@@ -152,6 +153,29 @@ func NewFilesCondition(l *Labeler) Condition {
 	}
 }
 
+func NewAuthorCondition() Condition {
+	return Condition{
+		GetName: func() string {
+			return "Author matches"
+		},
+		Evaluate: func(pr *gh.PullRequest, matcher LabelMatcher) (bool, error) {
+			if len(matcher.Authors) <= 0 {
+				return false, fmt.Errorf("Users are not set in config")
+			}
+
+			prAuthor := pr.GetUser().Login
+
+			log.Printf("Matching `%s` against: `%v`", matcher.Authors, *prAuthor)
+			for _, author := range matcher.Authors {
+				if strings.ToLower(author) == strings.ToLower(*prAuthor) {
+					return true, nil
+				}
+			}
+			return false, nil
+		},
+	}
+}
+
 func NewIsMergeableCondition() Condition {
 	return Condition{
 		GetName: func() string {
@@ -273,6 +297,7 @@ func (l *Labeler) findMatches(pr *gh.PullRequest, config *LabelerConfigV1) (Labe
 		NewSizeCondition(),
 		NewBodyCondition(),
 		NewFilesCondition(l),
+		NewAuthorCondition(),
 	}
 
 	for _, matcher := range config.Labels {


Test patch:
diff --git a/cmd/action_test.go b/cmd/action_test.go
index 9727dfa..d85c40a 100644
--- a/cmd/action_test.go
+++ b/cmd/action_test.go
@@ -125,6 +125,13 @@ func TestGetLabelerConfigV1(t *testing.T) {
 					"pkg/.*.go",
 				},
 			},
+			{
+				Label: "Test",
+				Authors: []string{
+					"Test1",
+					"Test2",
+				},
+			},
 		},
 	}
 
diff --git a/pkg/labeler_test.go b/pkg/labeler_test.go
index 816611e..04382bc 100644
--- a/pkg/labeler_test.go
+++ b/pkg/labeler_test.go
@@ -339,6 +339,25 @@ func TestHandleEvent(t *testing.T) {
 			initialLabels:  []string{},
 			expectedLabels: []string{"Branch"},
 		},
+		{
+			payloads: []string{"small_pr"},
+			name:     "Multiple conditions for the same tag function as OR",
+			config: LabelerConfigV1{
+				Version: 1,
+				Labels: []LabelMatcher{
+					{
+						Label:   "Author",
+						Authors: []string{"srvaroa"},
+					},
+					{
+						Label:  "Branch",
+						Branch: "WONT MATCH",
+					},
+				},
+			},
+			initialLabels:  []string{},
+			expectedLabels: []string{"Author"},
+		},
 	}
 
 	for _, tc := range testCases {
diff --git a/test_data/config_v1.yml b/test_data/config_v1.yml
index 5daa753..7cd93ee 100644
--- a/test_data/config_v1.yml
+++ b/test_data/config_v1.yml
@@ -17,3 +17,7 @@ labels:
     files: 
     - "cmd/.*.go"
     - "pkg/.*.go"
+  - label: "Test"
+    authors: 
+      - "Test1"
+      - "Test2"