Skip to content

Commit bfe2888

Browse files
authored
Support author-in-team (#150)
Signed-off-by: Galo Navarro <anglorvaroa@gmail.com>
1 parent b48bedc commit bfe2888

File tree

5 files changed

+92
-3
lines changed

5 files changed

+92
-3
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ configurable matching rules. Available conditions:
99

1010
* [Age](#age): label based on the age of a PR or Issue.
1111
* [Author can merge](#author-can-merge): label based on whether the author can merge the PR
12+
* [Author is member of team](#author-in-team): label based on whether the author is an active member of the given team
1213
* [Authors](#authors): label based on the PR/Issue authors
1314
* [Base branch](#base-branch): label based on the PR's base branch name
1415
* [Body](#body): label based on the PR/Issue body
@@ -339,6 +340,17 @@ This is implemented by checking if the author is an owner of the repo.
339340
```yaml
340341
author-can-merge: True
341342
```
343+
344+
345+
### Author is member (PRs and Issues) <a name="author-in-team" />
346+
347+
This condition is satisfied when the author of the PR is an active
348+
member of the given team.
349+
350+
```yaml
351+
author-is-member: CoreTeam
352+
```
353+
342354
### Authors (PRs and Issues) <a name="authors" />
343355

344356
This condition is satisfied when the author of the PR or Issue matches

cmd/action.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,13 @@ func newLabeler(gh *github.Client, config *labeler.LabelerConfigV1) *labeler.Lab
244244
owner, repo, &github.PullRequestListOptions{})
245245
return prs, err
246246
},
247+
IsUserMemberOfTeam: func(user, team string) (bool, error) {
248+
membership, _, err := gh.Organizations.GetOrgMembership(ctx, user, team)
249+
if err != nil {
250+
return false, err
251+
}
252+
return membership.GetState() == "active", nil
253+
},
247254
},
248255
Client: labeler.NewDefaultHttpClient(),
249256
}

pkg/condition_author_in_team.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package labeler
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func AuthorInTeamCondition(l *Labeler) Condition {
8+
return Condition{
9+
GetName: func() string {
10+
return "Author is member of team"
11+
},
12+
CanEvaluate: func(target *Target) bool {
13+
return true
14+
},
15+
Evaluate: func(target *Target, matcher LabelMatcher) (bool, error) {
16+
if len(matcher.AuthorInTeam) <= 0 {
17+
return false, fmt.Errorf("author-in-team is not set in config")
18+
}
19+
// check if author is a member of team
20+
return l.GitHubFacade.IsUserMemberOfTeam(target.Author, matcher.AuthorInTeam)
21+
},
22+
}
23+
}

pkg/labeler.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type LabelMatcher struct {
2222
Age string
2323
AuthorCanMerge string `yaml:"author-can-merge"`
2424
Authors []string
25+
AuthorInTeam string `yaml:"author-in-team"`
2526
BaseBranch string `yaml:"base-branch"`
2627
Body string
2728
Branch string
@@ -65,9 +66,10 @@ type LabelUpdates struct {
6566

6667
// Just to make this mockable..
6768
type GitHubFacade struct {
68-
GetRawDiff func(owner, repo string, prNumber int) (string, error)
69-
ListIssuesByRepo func(owner, repo string) ([]*gh.Issue, error)
70-
ListPRs func(owner, repo string) ([]*gh.PullRequest, error)
69+
GetRawDiff func(owner, repo string, prNumber int) (string, error)
70+
ListIssuesByRepo func(owner, repo string) ([]*gh.Issue, error)
71+
ListPRs func(owner, repo string) ([]*gh.PullRequest, error)
72+
IsUserMemberOfTeam func(user, team string) (bool, error)
7173
}
7274

7375
type Labeler struct {
@@ -225,6 +227,7 @@ func (l *Labeler) findMatches(target *Target, config *LabelerConfigV1) (LabelUpd
225227
AgeCondition(l),
226228
AuthorCondition(),
227229
AuthorCanMergeCondition(),
230+
AuthorInTeamCondition(l),
228231
BaseBranchCondition(),
229232
BodyCondition(),
230233
BranchCondition(),

pkg/labeler_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,46 @@ func TestHandleEvent(t *testing.T) {
983983
initialLabels: []string{"Meh"},
984984
expectedLabels: []string{"Meh", "Test"},
985985
},
986+
{
987+
event: "issues",
988+
payloads: []string{"issue_open"},
989+
name: "Add a label to issue when author is in team",
990+
config: LabelerConfigV1{
991+
Version: 1,
992+
Labels: []LabelMatcher{
993+
{
994+
Label: "ShouldAppear",
995+
AuthorInTeam: "team-with-srvaroa",
996+
},
997+
{
998+
Label: "ShouldNotAppear",
999+
AuthorInTeam: "team-with",
1000+
},
1001+
},
1002+
},
1003+
initialLabels: []string{"Meh"},
1004+
expectedLabels: []string{"Meh", "ShouldAppear"},
1005+
},
1006+
{
1007+
event: "pull_request",
1008+
payloads: []string{"create_pr"},
1009+
name: "Add a label to PR when author is in team",
1010+
config: LabelerConfigV1{
1011+
Version: 1,
1012+
Labels: []LabelMatcher{
1013+
{
1014+
Label: "ShouldAppear",
1015+
AuthorInTeam: "team-with-srvaroa",
1016+
},
1017+
{
1018+
Label: "ShouldNotAppear",
1019+
AuthorInTeam: "team-with",
1020+
},
1021+
},
1022+
},
1023+
initialLabels: []string{"Meh"},
1024+
expectedLabels: []string{"Meh", "ShouldAppear"},
1025+
},
9861026
}
9871027

9881028
for _, tc := range testCases {
@@ -1034,6 +1074,10 @@ func NewTestLabeler(t *testing.T, tc TestCase) Labeler {
10341074
data, err := ioutil.ReadAll(file)
10351075
return string(data), nil
10361076
},
1077+
// Will return true whenever team contains the given user name
1078+
IsUserMemberOfTeam: func(user, team string) (bool, error) {
1079+
return strings.Contains(team, user), nil
1080+
},
10371081
},
10381082
}
10391083
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy