-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
222 lines (180 loc) · 4.73 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
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"strconv"
"strings"
"sync"
"time"
"runtime"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
"gopkg.in/yaml.v2"
)
type Data struct {
commentDuration int `json:"commentDuration"`
submitDuration int `json:"submitDuration"`
prID int `json:"prID"`
}
type Config struct {
AccessToken string `yaml:"AccessToken"`
Search struct {
User string `yaml:"User"`
Repo string `yaml:"Repo"`
} `yaml:"Search"`
TimeLine struct {
Start string `yaml:"Start"`
End string `yaml:"End"`
} `yaml:"TimeLine"`
OutputFile string `yaml:"OutputFile"`
}
type agent struct {
config Config
startTime time.Time
endTime time.Time
client *github.Client
currentTime time.Time
file *os.File
}
func New() *agent {
a := &agent{}
source, err := ioutil.ReadFile("config.yaml")
check(err)
err = yaml.Unmarshal(source, &(a.config))
check(err)
fmt.Printf("Your token value: %#v\n", a.config.AccessToken)
a.startTime, err = time.Parse("2006-01-02", a.config.TimeLine.Start)
check(err)
a.endTime, err = time.Parse("2006-01-02", a.config.TimeLine.End)
check(err)
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: a.config.AccessToken},
)
tc := oauth2.NewClient(oauth2.NoContext, ts)
a.client = github.NewClient(tc)
currentTime := time.Now()
currentTime.Format("2006-01-02")
a.currentTime = currentTime
a.file, err = os.Create(a.config.OutputFile)
check(err)
return a
}
func main() {
var (
breakLoop bool
wg sync.WaitGroup
)
a := New()
opt := &github.PullRequestListOptions{
ListOptions: github.ListOptions{PerPage: 100},
State: "all",
}
breakLoop = false
for {
prs, resp, err := a.client.PullRequests.List(a.config.Search.User, a.config.Search.Repo, opt)
if _, ok := err.(*github.RateLimitError); ok {
log.Println("Hit rate limit. Try after an hour.")
} else {
check(err)
}
for i := 0; i < len(prs); i++ {
current := *prs[i].CreatedAt
if current.Before(a.startTime) {
if current.Before(a.endTime) {
breakLoop = true
break
fmt.Println("break")
}
wg.Add(1)
go a.process(current, prs[i].Number, prs[i].ClosedAt, prs[i].MergedAt, getName(prs[i].User), getName(prs[i].Assignee), prs[i].State, &wg)
time.Sleep(1000 * time.Millisecond)
}
}
if resp.NextPage == 0 {
break
}
opt.ListOptions.Page = resp.NextPage
if breakLoop {
break
}
}
wg.Wait()
defer a.file.Close()
}
func (a *agent) process(createdAt time.Time, id *int, closedAt *time.Time, mergedAt *time.Time, user string, assignee string, status *string, wg *sync.WaitGroup) {
var (
commentCreatedAt time.Time
commentDuration time.Duration
submitDuration time.Duration
)
optComments := &github.IssueListCommentsOptions{
ListOptions: github.ListOptions{PerPage: 2},
}
comments, _, err := a.client.Issues.ListComments(a.config.Search.User, a.config.Search.Repo, *id, optComments)
if _, ok := err.(*github.RateLimitError); ok {
log.Println("hit rate limit. Try after an hour.")
os.Exit(0)
} else {
check(err)
}
if len(comments) > 0 {
commentCreatedAt = *comments[0].CreatedAt
commentDuration = commentCreatedAt.Sub(createdAt)
if closedAt != nil {
submitDuration = closedAt.Sub(commentCreatedAt)
} else if mergedAt != nil {
submitDuration = mergedAt.Sub(commentCreatedAt)
} else {
if a.endTime.Sub(commentCreatedAt) > 0 {
submitDuration = a.currentTime.Sub(commentCreatedAt)
} else {
submitDuration = commentCreatedAt.Sub(a.endTime)
} //negative
}
} else {
if closedAt != nil {
commentDuration = 0
submitDuration = closedAt.Sub(createdAt)
} else if mergedAt != nil {
commentDuration = 0
submitDuration = mergedAt.Sub(createdAt)
} else {
commentDuration = a.currentTime.Sub(createdAt)
submitDuration = 0
}
}
_commentDuration := strconv.Itoa(int(commentDuration.Hours() / 24))
_submitDuration := strconv.Itoa(int(submitDuration.Hours() / 24))
_prID := strconv.Itoa(*id)
created_on := strings.Split(createdAt.String(), " ")
message := `{` +
`"commentDuration": ` + _commentDuration +
`, "submitDuration": ` + _submitDuration +
`, "prID": "` + _prID +
`", "status": "` + (*status) +
`", "assignee": "` + assignee +
`", "createdBy": "` + user +
`", "created_on": "` + created_on[0] +
`"}`
_, err = a.file.Write([]byte(message + "\n"))
check(err)
fmt.Println(message)
wg.Done()
}
func getName(user *github.User) string {
if user != nil {
return *user.Login
} else {
return "null"
}
}
func check(err error) {
if err != nil {
pc, _, line, _ := runtime.Caller(1)
message := fmt.Sprintf("[error] %s[%d] %s", runtime.FuncForPC(pc).Name(), line, err.Error())
log.Println(message)
os.Exit(0)
}
}