Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in author_can_merge #124

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading