Skip to content

Commit 50320c5

Browse files
authored
Fix bug in author_can_merge (#124)
Signed-off-by: Galo Navarro <[email protected]>
1 parent 87ccdf6 commit 50320c5

File tree

2 files changed

+72
-6
lines changed

2 files changed

+72
-6
lines changed

pkg/condition_author_can_merge.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package labeler
22

33
import (
44
"fmt"
5+
"strconv"
56
)
67

78
func AuthorCanMergeCondition() Condition {
@@ -10,16 +11,31 @@ func AuthorCanMergeCondition() Condition {
1011
return "Author can merge"
1112
},
1213
CanEvaluate: func(target *Target) bool {
13-
return true
14+
return target.ghPR != nil
1415
},
1516
Evaluate: func(target *Target, matcher LabelMatcher) (bool, error) {
16-
if len(matcher.AuthorCanMerge) <= 0 {
17-
return false, fmt.Errorf("AuthorCanMerge not set in repository")
17+
expected, err := strconv.ParseBool(matcher.AuthorCanMerge)
18+
if err != nil {
19+
return false, fmt.Errorf("author-can-merge doesn't have a valid value in config")
1820
}
21+
1922
ghRepo := target.ghPR.GetAuthorAssociation()
2023
canMerge := ghRepo == "OWNER"
21-
fmt.Printf("User: %s can merge? %t\n", target.Author, canMerge)
22-
return canMerge, nil
24+
25+
if expected && canMerge {
26+
fmt.Printf("User: %s can merge %t, condition matched\n",
27+
target.Author, canMerge)
28+
return true, nil
29+
}
30+
31+
if !expected && !canMerge {
32+
fmt.Printf("User: %s can not merge %t, condition matched\n",
33+
target.Author, canMerge)
34+
return true, nil
35+
}
36+
37+
fmt.Printf("Condition not matched")
38+
return false, nil
2339
},
2440
}
2541
}

pkg/labeler_test.go

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ func TestHandleEvent(t *testing.T) {
592592
{
593593
event: "pull_request",
594594
payloads: []string{"create_pr_non_owner"},
595-
name: "Add a label when the author cannot merge",
595+
name: "Remove a label when the author cannot merge",
596596
config: LabelerConfigV1{
597597
Version: 1,
598598
Labels: []LabelMatcher{
@@ -605,6 +605,56 @@ func TestHandleEvent(t *testing.T) {
605605
initialLabels: []string{"Test"},
606606
expectedLabels: []string{},
607607
},
608+
{
609+
event: "pull_request",
610+
payloads: []string{"create_pr_non_owner"},
611+
name: "Add a label when the author cannot merge",
612+
config: LabelerConfigV1{
613+
Version: 1,
614+
Labels: []LabelMatcher{
615+
{
616+
Label: "Test",
617+
AuthorCanMerge: "False",
618+
},
619+
},
620+
},
621+
initialLabels: []string{},
622+
expectedLabels: []string{"Test"},
623+
},
624+
{
625+
event: "pull_request",
626+
payloads: []string{"create_pr"},
627+
name: "Remove label when the author can merge",
628+
config: LabelerConfigV1{
629+
Version: 1,
630+
Labels: []LabelMatcher{
631+
{
632+
Label: "Test",
633+
AuthorCanMerge: "False",
634+
},
635+
},
636+
},
637+
initialLabels: []string{"Test"},
638+
expectedLabels: []string{},
639+
},
640+
{
641+
event: "pull_request",
642+
payloads: []string{"create_pr"},
643+
name: "Negate when the author can merge",
644+
config: LabelerConfigV1{
645+
Version: 1,
646+
Labels: []LabelMatcher{
647+
{
648+
Label: "Test",
649+
Draft: "False", // payload is not a draft
650+
AuthorCanMerge: "True", // payload author is the owner
651+
Negate: true, // both matchers are true, result should be false
652+
},
653+
},
654+
},
655+
initialLabels: []string{"Test"},
656+
expectedLabels: []string{},
657+
},
608658

609659
// Issues
610660

0 commit comments

Comments
 (0)