-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathissue_manager.go
More file actions
119 lines (92 loc) · 2.82 KB
/
issue_manager.go
File metadata and controls
119 lines (92 loc) · 2.82 KB
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
package main
import (
"errors"
"fmt"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
"regexp"
"strings"
"time"
)
type IssueManager struct {
Client *github.Client
Organization string
Team string
Repository string
Duration int
}
type Users []*github.User
func String(v string) *string { return &v }
func NewIssueManager(organization string, repository string, team string, token string, duration int) *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.Duration = duration
return im
}
func (im *IssueManager) FindIssues(spec string) ([]*github.Issue, error) {
queryString := im.buildQuery(spec)
searchResult, _, err := im.Client.Search.Issues(queryString, &github.SearchOptions{})
if err != nil {
return nil, err
}
var targets []*github.Issue
if im.Team == "" {
for i, _ := range searchResult.Issues {
targets = append(targets, &searchResult.Issues[i])
}
return targets, nil
}
members, err := im.findUsersByTeamName(im.Team)
if err != nil {
return nil, err
}
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) Close(issue *github.Issue) (*github.Issue, error) {
issueRequest := &github.IssueRequest{}
issueRequest.State = String("closed")
i, _, err := im.Client.Issues.Edit(im.Organization, im.Repository, *issue.Number, issueRequest)
return i, err
}
func (im *IssueManager) isUpdatedWithinDuration(issue *github.Issue) bool {
dhInt := im.Duration * 24
dur, _ := time.ParseDuration(fmt.Sprintf("%dh", dhInt))
updatedAt := issue.UpdatedAt
return time.Since(*updatedAt) < dur
}
func (im *IssueManager) Comment(issue *github.Issue, comment string) bool {
ic := &github.IssueComment{Body: &comment}
_, _, err := im.Client.Issues.CreateComment(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(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(*t.ID, &github.OrganizationListTeamMembersOptions{})
return users, err
}
}
return nil, errors.New("team not found")
}
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, " ")
}