From dd3beb9e15cb16a3227a7ac76f670d6b4fb13dfc Mon Sep 17 00:00:00 2001 From: Galo Navarro Date: Mon, 4 Dec 2023 20:53:42 +0100 Subject: [PATCH] Fix bug in author_can_merge Signed-off-by: Galo Navarro --- pkg/condition_author_can_merge.go | 26 +++++++++++++--- pkg/labeler_test.go | 52 ++++++++++++++++++++++++++++++- 2 files changed, 72 insertions(+), 6 deletions(-) diff --git a/pkg/condition_author_can_merge.go b/pkg/condition_author_can_merge.go index 995cee5..0364285 100644 --- a/pkg/condition_author_can_merge.go +++ b/pkg/condition_author_can_merge.go @@ -2,6 +2,7 @@ package labeler import ( "fmt" + "strconv" ) func AuthorCanMergeCondition() Condition { @@ -10,16 +11,31 @@ func AuthorCanMergeCondition() Condition { return "Author can merge" }, CanEvaluate: func(target *Target) bool { - return true + return target.ghPR != nil }, Evaluate: func(target *Target, matcher LabelMatcher) (bool, error) { - if len(matcher.AuthorCanMerge) <= 0 { - return false, fmt.Errorf("AuthorCanMerge not set in repository") + expected, err := strconv.ParseBool(matcher.AuthorCanMerge) + if err != nil { + return false, fmt.Errorf("author-can-merge doesn't have a valid value in config") } + ghRepo := target.ghPR.GetAuthorAssociation() canMerge := ghRepo == "OWNER" - fmt.Printf("User: %s can merge? %t\n", target.Author, canMerge) - return canMerge, nil + + if expected && canMerge { + fmt.Printf("User: %s can merge %t, condition matched\n", + target.Author, canMerge) + return true, nil + } + + if !expected && !canMerge { + fmt.Printf("User: %s can not merge %t, condition matched\n", + target.Author, canMerge) + return true, nil + } + + fmt.Printf("Condition not matched") + return false, nil }, } } diff --git a/pkg/labeler_test.go b/pkg/labeler_test.go index 04296a7..a65edca 100644 --- a/pkg/labeler_test.go +++ b/pkg/labeler_test.go @@ -592,7 +592,7 @@ func TestHandleEvent(t *testing.T) { { event: "pull_request", payloads: []string{"create_pr_non_owner"}, - name: "Add a label when the author cannot merge", + name: "Remove a label when the author cannot merge", config: LabelerConfigV1{ Version: 1, Labels: []LabelMatcher{ @@ -605,6 +605,56 @@ func TestHandleEvent(t *testing.T) { initialLabels: []string{"Test"}, expectedLabels: []string{}, }, + { + event: "pull_request", + payloads: []string{"create_pr_non_owner"}, + name: "Add a label when the author cannot merge", + config: LabelerConfigV1{ + Version: 1, + Labels: []LabelMatcher{ + { + Label: "Test", + AuthorCanMerge: "False", + }, + }, + }, + initialLabels: []string{}, + expectedLabels: []string{"Test"}, + }, + { + event: "pull_request", + payloads: []string{"create_pr"}, + name: "Remove label when the author can merge", + config: LabelerConfigV1{ + Version: 1, + Labels: []LabelMatcher{ + { + Label: "Test", + AuthorCanMerge: "False", + }, + }, + }, + initialLabels: []string{"Test"}, + expectedLabels: []string{}, + }, + { + event: "pull_request", + payloads: []string{"create_pr"}, + name: "Negate when the author can merge", + config: LabelerConfigV1{ + Version: 1, + Labels: []LabelMatcher{ + { + Label: "Test", + Draft: "False", // payload is not a draft + AuthorCanMerge: "True", // payload author is the owner + Negate: true, // both matchers are true, result should be false + }, + }, + }, + initialLabels: []string{"Test"}, + expectedLabels: []string{}, + }, // Issues