diff --git a/pkg/condition_author_can_merge.go b/pkg/condition_author_can_merge.go index 995cee5..83f792f 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 { @@ -13,13 +14,28 @@ func AuthorCanMergeCondition() Condition { return true }, 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