-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
299 lines (235 loc) · 7.17 KB
/
main.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package main
import (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"net/url"
"os"
"regexp"
"strings"
"text/tabwriter"
"time"
log "github.com/sirupsen/logrus"
"github.com/cuotos/outstanding-prs/filter"
"github.com/google/go-github/v33/github"
"github.com/kelseyhightower/envconfig"
"golang.org/x/oauth2"
)
var (
version = "unset"
commit = "unset"
)
const (
envVarPrefix = "PRS"
)
var (
// Set default filters, like "draft:false"
defaultFilters = []filter.FilterOpt{
filter.WithIncludeDraft(false),
filter.WithIsOpen(),
}
)
type config struct {
GithubPat string `split_words:"true" required:"true" envconfig:"GITHUB_TOKEN"`
GithubOrg string `split_words:"true" required:"true"`
GithubTeam string `split_words:"true" required:"false"`
}
type PullRequest struct {
CreatedAt time.Time `json:"created_at,omitempty"`
Title string `json:"title,omitempty"`
Author string `json:"author,omitempty"`
Head string `json:"head,omitempty"`
Base string `json:"base,omitempty"`
Link string `json:"link,omitempty"`
Draft bool `json:"draft,omitempty"`
Mergeable bool `json:"mergeable,omitempty"`
}
func init() {
log.SetOutput(os.Stdout)
log.SetLevel(log.WarnLevel)
}
func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}
func run() error {
v := flag.Bool("v", false, "prints version")
jsonOutput := flag.Bool("json", false, "print output in JSON format")
all := flag.Bool("all", false, "include PRs ready to merge. DEPRECATED: use -approved")
incApproved := flag.Bool("approved", false, "include PRs ready to merge")
searchWholeTeam := flag.Bool("team", false, fmt.Sprintf("should look up all members of the team. defaults to just calling user. Requires the %s_GITHUB_TEAM env var to be set", envVarPrefix))
incDrafts := flag.Bool("drafts", false, "include PRs that are in draft")
flag.Parse()
if *v {
fmt.Printf("%s-%s", version, commit)
os.Exit(0)
}
conf := config{}
err := envconfig.Process(envVarPrefix, &conf)
if err != nil {
return err
}
client := getGithubClient(conf.GithubPat)
members := []*github.User{}
if *searchWholeTeam {
if conf.GithubTeam == "" {
log.Warnf("to view team PRs the environment variables %s_GITHUB_TEAM and %s_GITHUB_ORG must both be set", envVarPrefix, envVarPrefix)
return nil
}
members, err = getOrgTeamMembers(client, conf.GithubOrg, conf.GithubTeam)
if err != nil {
return err
}
} else {
user, _, err := client.Users.Get(context.Background(), "")
if err != nil {
return err
}
members = append(members, user)
}
queryString, err := generateQueryString(conf.GithubOrg, members, filter.WithIncludeApproved(*all || *incApproved), filter.WithIncludeDraft(*incDrafts))
if err != nil {
return err
}
log.Debugf(`Looking for PRs with the following query: "%s"`, queryString)
prs, err := getPullRequests(client, queryString)
if err != nil {
return err
}
if *jsonOutput {
printOutputJSON(prs)
} else {
printOutput(prs, conf.GithubOrg, conf.GithubTeam, *incDrafts)
}
return nil
}
func getPullRequestFromIssue(client *github.Client, issue *github.Issue) (*github.PullRequest, error) {
u, _ := url.Parse(issue.GetPullRequestLinks().GetHTMLURL())
// /{org}/{repo}/pulls/{number}
// but we can get number from the issue itself.
re := regexp.MustCompile("/(.+?)/(.+?)/pull/.+")
match := re.FindStringSubmatch(u.Path)
org := match[1]
repo := match[2]
pr, _, err := client.PullRequests.Get(context.Background(), org, repo, issue.GetNumber())
if err != nil {
return nil, fmt.Errorf("unable to get PR from Issue: %w", err)
}
return pr, nil
}
func getPullRequests(client *github.Client, queryString string) ([]PullRequest, error) {
var allPrs []PullRequest
// Github PRs are "Issues" in regards to searching
// once you have the "issues", convert them to a list of "pulls"
// There doesnt seem to be an easy way to get a PR from its URL, so have to break the URL
// up into its fields and do a github.GetPR() call for each one.
opt := &github.SearchOptions{
Sort: "created-desc",
ListOptions: github.ListOptions{
PerPage: 20,
},
}
for {
foundIssues, resp, err := client.Search.Issues(context.Background(), queryString, opt)
if err != nil {
return nil, err
}
for _, issue := range foundIssues.Issues {
ghPullRequest, err := getPullRequestFromIssue(client, issue)
if err != nil {
return nil, fmt.Errorf("unable to get github pr: %w", err)
}
pr := PullRequest{
CreatedAt: issue.GetCreatedAt(),
Title: issue.GetTitle(),
Author: issue.GetUser().GetLogin(),
Head: ghPullRequest.GetHead().GetRef(),
Base: ghPullRequest.GetBase().GetRef(),
Link: issue.GetHTMLURL(),
Draft: ghPullRequest.GetDraft(),
Mergeable: *ghPullRequest.Mergeable,
}
allPrs = append(allPrs, pr)
}
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
return allPrs, nil
}
func getOrgTeamMembers(client *github.Client, org, team string) ([]*github.User, error) {
var allMembers []*github.User
opts := &github.TeamListTeamMembersOptions{
ListOptions: github.ListOptions{
PerPage: 10,
},
}
for {
members, resp, err := client.Teams.ListTeamMembersBySlug(context.Background(), org, team, opts)
if err != nil {
return nil, err
}
allMembers = append(allMembers, members...)
if resp.NextPage == 0 {
break
}
opts.Page = resp.NextPage
}
return allMembers, nil
}
func printOutputJSON(prs []PullRequest) error {
jsonBytes, err := json.Marshal(prs)
if err != nil {
return fmt.Errorf("unable to marshal PRs: %w", err)
}
fmt.Println(string(jsonBytes))
return nil
}
func printOutput(prs []PullRequest, org, team string, addDraftsCol bool) error {
writer := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
// Add headers to the buffer {
headerBuf := bytes.NewBufferString("CreatedAt\tTitle\tAuthor\tHead (from)\tBase (into)\tMergeable\tLink")
if addDraftsCol {
headerBuf.WriteString("\tDraft")
}
headerBuf.WriteString("\n")
writer.Write(headerBuf.Bytes())
for _, pr := range prs {
issueBuf := bytes.NewBufferString("")
issueBuf.WriteString(fmt.Sprintf("%s\t%s\t%s\t%s\t%s\t%t\t%s", pr.CreatedAt.Format("2006-01-02"), pr.Title, pr.Author, pr.Head, pr.Base, pr.Mergeable, pr.Link))
if addDraftsCol {
issueBuf.WriteString(fmt.Sprintf("\t%t", pr.Draft))
}
issueBuf.WriteString("\n")
writer.Write(issueBuf.Bytes())
}
return writer.Flush()
}
func generateQueryString(org string, members []*github.User, additionalFilters ...filter.FilterOpt) (string, error) {
queryBuilder := strings.Builder{}
queryBuilder.WriteString("type:pr archived:false ")
var users []string
for _, m := range members {
users = append(users, m.GetLogin())
}
filters := append(defaultFilters, filter.WithOrg(org), filter.WithAuthors(users...))
filters = append(filters, additionalFilters...)
fs, err := filter.GetFilterString(filters...)
if err != nil {
return "", err
}
queryBuilder.WriteString(fs)
return queryBuilder.String(), nil
}
func getGithubClient(accessToken string) *github.Client {
ts := oauth2.StaticTokenSource(&oauth2.Token{
AccessToken: accessToken,
})
tc := oauth2.NewClient(context.Background(), ts)
client := github.NewClient(tc)
return client
}