Skip to content

Commit

Permalink
New rule to match on whether author can merge
Browse files Browse the repository at this point in the history
Allow matching on whether the author of a PR can merge it. For now,
implementing it using the author_association field in the PR. Which
gives a value among these
[1](https://docs.github.com/en/graphql/reference/enums#commentauthorassociation).
Not sure if it's the best way of achieving it, but let's see.

Signed-off-by: Galo Navarro <[email protected]>
  • Loading branch information
srvaroa committed Feb 20, 2023
1 parent 4c875d6 commit 9fd2561
Show file tree
Hide file tree
Showing 7 changed files with 553 additions and 24 deletions.
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,13 @@ state](https://developer.github.com/v3/pulls/#response-1) matches that of the
PR.

```yaml
draft: true
draft: True
```

Matches if the PR is a draft.

```yaml
draft: false
draft: False
```

Matches if the PR is not a draft.
Expand All @@ -302,13 +302,13 @@ state](https://developer.github.com/v3/pulls/#response-1) matches that
of the PR.

```yaml
mergeable: true
mergeable: True
```

Will match if the label is mergeable.

```yaml
mergeable: false
mergeable: False
```

Will match if the label is not mergeable.
Expand All @@ -322,6 +322,16 @@ any of the given usernames.
authors: ["serubin"]
```

### Author can merge (PRs)

This condition is satisfied when the author of the PR can merge it.
This is implemented by checking if the author is an owner of the repo.

```yaml
author-can-merge: True
```


### Size (PRs only)

This condition is satisfied when the total number of changed lines in
Expand Down
4 changes: 4 additions & 0 deletions cmd/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ func TestGetLabelerConfigV1(t *testing.T) {
Label: "TestMergeable",
Mergeable: "True",
},
{
Label: "TestAuthorCanMerge",
AuthorCanMerge: "True",
},
},
}

Expand Down
25 changes: 25 additions & 0 deletions pkg/condition_author_can_merge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package labeler

import (
"fmt"
)

func AuthorCanMergeCondition() Condition {
return Condition{
GetName: func() string {
return "Author can merge"
},
CanEvaluate: func(target *Target) bool {
return true
},
Evaluate: func(target *Target, matcher LabelMatcher) (bool, error) {
if len(matcher.AuthorCanMerge) <= 0 {
return false, fmt.Errorf("AuthorCanMerge not set in repository")
}
ghRepo := target.ghPR.GetAuthorAssociation()
canMerge := ghRepo == "OWNER"
fmt.Printf("User: %s can merge? %t\n", target.Author, canMerge)
return canMerge, nil
},
}
}
38 changes: 20 additions & 18 deletions pkg/labeler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ import (
)

type LabelMatcher struct {
Label string
Negate bool
Title string
Branch string
BaseBranch string `yaml:"base-branch"`
Body string
Files []string
Authors []string
Mergeable string
Draft string
SizeBelow string `yaml:"size-below"`
SizeAbove string `yaml:"size-above"`
AuthorCanMerge string `yaml:"author-can-merge"`
Authors []string
BaseBranch string `yaml:"base-branch"`
Body string
Branch string
Draft string
Files []string
Label string
Mergeable string
Negate bool
SizeAbove string `yaml:"size-above"`
SizeBelow string `yaml:"size-below"`
Title string
}

type LabelerConfigV0 map[string]LabelMatcher
Expand Down Expand Up @@ -183,15 +184,16 @@ func (l *Labeler) findMatches(target *Target, config *LabelerConfigV1) (LabelUpd
set: map[string]bool{},
}
conditions := []Condition{
TitleCondition(),
BranchCondition(),
AuthorCondition(),
AuthorCanMergeCondition(),
BaseBranchCondition(),
IsMergeableCondition(),
IsDraftCondition(),
SizeCondition(),
BodyCondition(),
BranchCondition(),
FilesCondition(l),
AuthorCondition(),
IsDraftCondition(),
IsMergeableCondition(),
SizeCondition(),
TitleCondition(),
}

for _, matcher := range config.Labels {
Expand Down
32 changes: 32 additions & 0 deletions pkg/labeler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,38 @@ func TestHandleEvent(t *testing.T) {
// BUT because AppendOnly is set, we do not erase it
expectedLabels: []string{"Fix"},
},
{
event: "pull_request",
payloads: []string{"create_pr", "reopen_pr"},
name: "Add a label when the author can merge",
config: LabelerConfigV1{
Version: 1,
Labels: []LabelMatcher{
{
Label: "Test",
AuthorCanMerge: "True",
},
},
},
initialLabels: []string{},
expectedLabels: []string{"Test"},
},
{
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: "True",
},
},
},
initialLabels: []string{"Test"},
expectedLabels: []string{},
},

// Issues

Expand Down
6 changes: 4 additions & 2 deletions test_data/config_v1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ labels:
- "Test1"
- "Test2"
- label: "TestDraft"
draft: "True"
draft: True
- label: "TestMergeable"
mergeable: "True"
mergeable: True
- label: "TestAuthorCanMerge"
author-can-merge: True
Loading

0 comments on commit 9fd2561

Please sign in to comment.