-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
115 lines (94 loc) · 2.36 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
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"time"
"github.com/google/go-github/github"
)
var (
org = "gansoi"
repo = "gansoi"
clientID = os.Getenv("CLIENT_ID")
clientSecret = os.Getenv("CLIENT_SECRET")
epoch = monday()
kpis = make(map[string]int)
)
func monday() time.Time {
t := time.Now()
for {
if t.Weekday() != time.Monday {
t = t.AddDate(0, 0, -1)
} else {
return t.Truncate(24 * time.Hour)
}
}
}
func init() {
kpis["pullRequests"] = 0
kpis["pullRequestsCreated"] = 0
kpis["pullRequestsClosed"] = 0
kpis["issues"] = 0
kpis["issuesCreated"] = 0
kpis["issuesClosed"] = 0
}
func main() {
var client *github.Client
if clientID != "" && clientSecret != "" {
t := &github.UnauthenticatedRateLimitedTransport{
// https://github.com/settings/applications
ClientID: clientID,
ClientSecret: clientSecret,
}
client = github.NewClient(t.Client())
} else {
client = github.NewClient(nil)
}
pullRequestOptions := &github.PullRequestListOptions{
State: "all",
}
pullRequests, _, _ := client.PullRequests.List(context.Background(), org, repo, pullRequestOptions)
for _, pr := range pullRequests {
add := 0
if pr.CreatedAt.After(epoch) {
add = 1
kpis["pullRequestsCreated"]++
}
if pr.ClosedAt != nil && pr.ClosedAt.After(epoch) {
add = 1
kpis["pullRequestsClosed"]++
}
kpis["pullRequests"] += add
}
issuesOptions := &github.IssueListByRepoOptions{
State: "all",
}
issues, _, _ := client.Issues.ListByRepo(context.Background(), org, repo, issuesOptions)
for _, i := range issues {
add := 0
if i.CreatedAt.After(epoch) {
add = 1
kpis["issuesCreated"]++
}
if i.ClosedAt != nil && i.ClosedAt.After(epoch) {
add = 1
kpis["issuesClosed"]++
}
kpis["issues"] += add
kpis["score"] += add
}
commitsOptions := &github.CommitsListOptions{
Since: epoch,
}
commits, _, _ := client.Repositories.ListCommits(context.Background(), org, repo, commitsOptions)
kpis["commits"] = len(commits)
kpis["score"] += len(commits)
// pull requests is included in the issue-count. Subtract.
kpis["issues"] -= kpis["pullRequests"]
kpis["issuesCreated"] -= kpis["pullRequestsCreated"]
kpis["issuesClosed"] -= kpis["pullRequestsClosed"]
kpis["elapsed"] = int(time.Now().Sub(epoch) / time.Second)
j, _ := json.MarshalIndent(kpis, "", " ")
fmt.Printf("%s\n", j)
}