Skip to content

Commit

Permalink
Fix bug in author_can_merge
Browse files Browse the repository at this point in the history
Signed-off-by: Galo Navarro <anglorvaroa@gmail.com>
  • Loading branch information
srvaroa committed Dec 4, 2023
1 parent 8b4be66 commit dd3beb9
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 6 deletions.
26 changes: 21 additions & 5 deletions pkg/condition_author_can_merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package labeler

import (
"fmt"
"strconv"
)

func AuthorCanMergeCondition() Condition {
Expand All @@ -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
},
}
}
52 changes: 51 additions & 1 deletion pkg/labeler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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

Expand Down

0 comments on commit dd3beb9

Please sign in to comment.