-
Notifications
You must be signed in to change notification settings - Fork 13
/
contrib.go
228 lines (184 loc) · 6.31 KB
/
contrib.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
package main
import (
"context"
"flag"
"fmt"
"os"
"time"
"golang.org/x/oauth2"
"github.com/google/go-github/github"
)
const (
// BANNER is what is printed for help/info output.
BANNER = "github-contrib : %s\n"
// USAGE is an example of how the command should be used.
USAGE = "USAGE:\ngithub-contrib -token=<your-token> <org> <github-handle>"
// VERSION is the binary version.
VERSION = "v0.1.0"
)
var (
token string
version bool
)
func init() {
flag.StringVar(&token, "token", "", "Mandatory GitHub API token")
flag.BoolVar(&version, "version", false, "print version and exit")
flag.BoolVar(&version, "v", false, "print version and exit (shorthand)")
flag.Usage = func() {
fmt.Fprint(os.Stderr, fmt.Sprintf(BANNER, VERSION))
fmt.Println(USAGE)
flag.PrintDefaults()
}
flag.Parse()
if version {
fmt.Printf("%s", VERSION)
os.Exit(0)
}
if token == "" {
usageAndExit("GitHub token cannot be empty", 1)
}
}
func main() {
args := flag.Args()
if len(args) != 2 {
fmt.Println("Wrong number of arguments!")
os.Exit(1)
}
org := args[0]
author := args[1]
ctx := context.Background()
// Create an authenticated client.
// Authenticated clients have a rate limit of 30 requests per minute.
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
getAllRepos(ctx, client, org, author)
}
// getAllRepos gets all stats for a contributor across all public repos in an org.
func getAllRepos(ctx context.Context, client *github.Client, org, author string) {
opt := &github.RepositoryListByOrgOptions{Type: "public"}
repos, _, err := client.Repositories.ListByOrg(ctx, org, opt)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
for _, repository := range repos {
repo := repository.GetName()
var output []string
output = append(output, getCreatedPullRequests(ctx, client, org, repo, author)...)
output = append(output, getIssues(ctx, client, org, repo, author)...)
output = append(output, getReviewedPullRequests(ctx, client, org, repo, author)...)
if len(output) != 0 {
// for markdown-friendly output
// TODO: refractor to be plain text friendly
fmt.Printf("**Repository: %s**\n", repo)
for _, line := range output {
fmt.Println(line)
}
fmt.Printf("\n\n")
}
}
}
// getPullRequests gets all Pull Requests created by the author.
func getCreatedPullRequests(ctx context.Context, client *github.Client, org, repo, author string) []string {
sleepIfRateLimitExceeded(ctx, client)
var createdPullRequests []string
allPullRequestsquery := "is:pr repo:" + org + "/" + repo + " author:" + author
opt := &github.SearchOptions{
ListOptions: github.ListOptions{
PerPage: 100,
},
}
pullRequestResults, _, err := client.Search.Issues(ctx, allPullRequestsquery, opt)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
totalPullRequests := pullRequestResults.GetTotal()
if totalPullRequests != 0 {
createdPullRequests = append(createdPullRequests, fmt.Sprintf("\nTotal Pull Requests Created: %v", totalPullRequests))
}
for key, pr := range pullRequestResults.Issues {
serialNumber := fmt.Sprintf("%v. ", key+1)
pullRequestLink := fmt.Sprintf("[%s/%s#%v](%s) - ", org, repo, pr.GetNumber(), pr.GetHTMLURL()) // org/repo#number
pullRequestTitle := fmt.Sprintf("%s", pr.GetTitle())
createdPullRequests = append(createdPullRequests, fmt.Sprintf("%s%s%s", serialNumber, pullRequestLink, pullRequestTitle))
}
return createdPullRequests
}
// getIssues gets all issues created by the author.
func getIssues(ctx context.Context, client *github.Client, org, repo, author string) []string {
sleepIfRateLimitExceeded(ctx, client)
var createdIssues []string
allIssuesquery := "is:issue repo:" + org + "/" + repo + " author:" + author
opt := &github.SearchOptions{
ListOptions: github.ListOptions{
PerPage: 100,
},
}
issuesResults, _, err := client.Search.Issues(ctx, allIssuesquery, opt)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
totalIssues := issuesResults.GetTotal()
if totalIssues != 0 {
createdIssues = append(createdIssues, fmt.Sprintf("\nTotal Issues Opened: %v", totalIssues))
}
for key, issue := range issuesResults.Issues {
serialNumber := fmt.Sprintf("%v. ", key+1)
issueLink := fmt.Sprintf("[%s/%s#%v](%s) - ", org, repo, issue.GetNumber(), issue.GetHTMLURL()) // org/repo#number
issueTitle := fmt.Sprintf("%s", issue.GetTitle())
createdIssues = append(createdIssues, fmt.Sprintf("%s%s%s", serialNumber, issueLink, issueTitle))
}
return createdIssues
}
// getReviewedPullRequests gets all Pull Requests reviewed by the author.
func getReviewedPullRequests(ctx context.Context, client *github.Client, org, repo, author string) []string {
sleepIfRateLimitExceeded(ctx, client)
var reviewedPullRequests []string
allReviewedPullRequestsquery := "is:pr repo:" + org + "/" + repo + " reviewed-by:" + author + " -author:" + author
opt := &github.SearchOptions{
ListOptions: github.ListOptions{
PerPage: 100,
},
}
reviewedPullRequestResults, _, err := client.Search.Issues(ctx, allReviewedPullRequestsquery, opt)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
totalReviewedPullRequests := reviewedPullRequestResults.GetTotal()
if totalReviewedPullRequests != 0 {
reviewedPullRequests = append(reviewedPullRequests, fmt.Sprintf("\nTotal Pull Requests Reviewed: %v", totalReviewedPullRequests))
}
for key, pr := range reviewedPullRequestResults.Issues {
serialNumber := fmt.Sprintf("%v. ", key+1)
pullRequestLink := fmt.Sprintf("[%s/%s#%v](%s) - ", org, repo, pr.GetNumber(), pr.GetHTMLURL()) // org/repo#number
pullRequestTitle := fmt.Sprintf("%s", pr.GetTitle())
reviewedPullRequests = append(reviewedPullRequests, fmt.Sprintf("%s%s%s", serialNumber, pullRequestLink, pullRequestTitle))
}
return reviewedPullRequests
}
func sleepIfRateLimitExceeded(ctx context.Context, client *github.Client) {
rateLimit, _, err := client.RateLimits(ctx)
if err != nil {
fmt.Printf("Problem in getting rate limit information %v\n", err)
return
}
if rateLimit.Search.Remaining == 1 {
timeToSleep := rateLimit.Search.Reset.Sub(time.Now()) + time.Second
time.Sleep(timeToSleep)
}
}
func usageAndExit(message string, exitCode int) {
if message != "" {
fmt.Fprintf(os.Stderr, message)
fmt.Fprintf(os.Stderr, "\n\n")
}
flag.Usage()
fmt.Fprintf(os.Stderr, "\n")
os.Exit(exitCode)
}