-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathissue_manager.go
157 lines (129 loc) · 4.02 KB
/
issue_manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"context"
"errors"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
"regexp"
"strings"
)
type IssueManager struct {
Client *github.Client
Organization string
Team string
Repository string
DryRun bool
}
type Users []*github.User
func NewIssueManager(organization string, repository string, team string, token string, dryRun bool) *IssueManager {
tc := oauth2.NewClient(oauth2.NoContext, oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token}))
im := &IssueManager{}
im.Client = github.NewClient(tc)
im.Organization = organization
im.Repository = repository
im.Team = team
im.DryRun = dryRun
return im
}
func (im *IssueManager) FindIssues(spec string) ([]*github.Issue, error) {
members, err := im.findUsersByTeamName(im.Team)
if err != nil {
return nil, err
}
queryString := im.buildQuery(spec)
searchResult, _, err := im.Client.Search.Issues(context.Background(), queryString, &github.SearchOptions{})
if err != nil {
return nil, err
}
var targets []*github.Issue
Loop:
for i, issue := range searchResult.Issues {
for _, member := range members {
if *issue.User.Login == *member.Login {
targets = append(targets, &searchResult.Issues[i])
continue Loop
}
}
}
return targets, nil
}
func (im *IssueManager) FindCandidatesOfReviewers(issue *github.Issue) ([]string, error) {
var users Users
users, err := im.findUsersByTeamName(im.Team)
if err != nil {
return nil, err
}
candidates := Users(users.removeUser(issue.User))
return candidates.GetLoginNames(), nil
}
func (im *IssueManager) Assign(issue *github.Issue, assignee string, assignAuthor bool) (*github.Issue, error) {
assignees := []string{assignee}
if assignAuthor {
assignees = append(assignees, *issue.User.Login)
}
if im.DryRun {
return issue, nil
}
i, _, err := im.Client.Issues.AddAssignees(context.Background(), im.Organization, im.Repository, *issue.Number, assignees)
return i, err
}
func (im *IssueManager) IsAlreadyAssignedExpectAuthor(issue *github.Issue) bool {
assignUsersExpectAuthor := im.getAssignUsersExpectAuthor(issue)
return len(assignUsersExpectAuthor) > 0
}
func (im *IssueManager) UnassignUsersExpectAuthor(issue *github.Issue) (*github.Issue, error) {
users := im.getAssignUsersExpectAuthor(issue)
var asignees []string
for _, u := range users {
asignees = append(asignees, *u.Login)
}
i, _, err := im.Client.Issues.RemoveAssignees(context.Background(), im.Organization, im.Repository, *issue.Number, asignees)
return i, err
}
func (im *IssueManager) getAssignUsersExpectAuthor(issue *github.Issue) []*github.User {
var assignees Users
assignees = issue.Assignees
return assignees.removeUser(issue.User)
}
func (im *IssueManager) Comment(issue *github.Issue, comment string) bool {
ic := &github.IssueComment{Body: &comment}
if im.DryRun {
return true
}
_, _, err := im.Client.Issues.CreateComment(context.Background(), im.Organization, im.Repository, *issue.Number, ic)
return err != nil
}
func (im *IssueManager) findUsersByTeamName(name string) ([]*github.User, error) {
teams, _, err := im.Client.Repositories.ListTeams(context.Background(), im.Organization, im.Repository, nil)
if err != nil {
return nil, err
}
for _, t := range teams {
if *t.Name == name {
users, _, err := im.Client.Organizations.ListTeamMembers(context.Background(), *t.ID, &github.OrganizationListTeamMembersOptions{})
return users, err
}
}
return nil, errors.New("team not found")
}
func (users Users) removeUser(user *github.User) []*github.User {
var candidates []*github.User
for _, u := range users {
if *u.Login != *user.Login {
candidates = append(candidates, u)
}
}
return candidates
}
func (users Users) GetLoginNames() []string {
var names []string
for _, u := range users {
names = append(names, *u.Login)
}
return names
}
func (im *IssueManager) buildQuery(spec string) string {
queries := regexp.MustCompile(" +").Split(spec, -1)
queries = append(queries, "repo:"+im.Organization+"/"+im.Repository)
return strings.Join(queries, " ")
}