forked from GoesToEleven/GolangTraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.go
executable file
·268 lines (238 loc) · 6.36 KB
/
github.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
package githubexample
import (
"encoding/json"
"io/ioutil"
"net/url"
"sync"
"time"
"golang.org/x/net/context"
"google.golang.org/appengine/datastore"
"google.golang.org/appengine/delay"
"google.golang.org/appengine/log"
"google.golang.org/appengine/urlfetch"
)
const redirectURI = "http://localhost:8080/oauth2callback"
const githubAPIURL = "https://api.github.com"
var delayedGetStats *delay.Function
func init() {
delayedGetStats = delay.Func(
"my-favorite-dog",
func(ctx context.Context, accessToken, username string) error {
log.Infof(ctx, "delayedGetStats CALLED")
api := &GithubAPI{
ctx: ctx,
accessToken: accessToken,
username: username,
}
since := time.Now().Add(-time.Hour * 24 * 30)
stats, err := api.getCommitSummaryStats(since)
if err != nil {
return err
}
log.Infof(ctx, "STATS: %v", stats)
key := datastore.NewKey(ctx, "Stats", username, 0, nil)
_, err = datastore.Put(ctx, key, &stats)
if err != nil {
return err
}
return nil
},
)
}
type GithubAPI struct {
ctx context.Context
accessToken string
username string
}
type CommitStats struct {
Additions, Deletions int
}
func NewGithubAPI(ctx context.Context) *GithubAPI {
return &GithubAPI{
ctx: ctx,
}
}
func (api *GithubAPI) getUsername() (string, error) {
client := urlfetch.Client(api.ctx)
response, err := client.Get(
githubAPIURL + "/user?access_token=" + api.accessToken)
if err != nil {
return "", err
}
defer response.Body.Close()
var data struct {
Login string
}
err = json.NewDecoder(response.Body).Decode(&data)
if err != nil {
return "", err
}
return data.Login, nil
}
func (api *GithubAPI) getAccessToken(state, code string) (string, error) {
values := make(url.Values)
values.Add("client_id", "767154e6915134caade5")
values.Add("client_secret", "50648a71510c21bb77e229692fc882dfbe8ea35d")
values.Add("code", code)
values.Add("state", state)
client := urlfetch.Client(api.ctx)
response, err := client.PostForm("https://github.com/login/oauth/access_token", values)
if err != nil {
return "", err
}
defer response.Body.Close()
bs, err := ioutil.ReadAll(response.Body)
if err != nil {
return "", err
}
values, err = url.ParseQuery(string(bs))
if err != nil {
return "", err
}
return values.Get("access_token"), nil
}
func (api *GithubAPI) getCommitSummaryStats(since time.Time) (CommitStats, error) {
var stats CommitStats
type Repo struct {
Organization, Repository string
}
var wg sync.WaitGroup
repositoryChannel := make(chan Repo)
statsChannel := make(chan CommitStats)
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
for repo := range repositoryChannel {
// list all commits for repository: GET /repos/:owner/:repo/commits
shas, err := api.getUserCommitShas(repo.Organization, repo.Repository, since)
if err != nil {
log.Errorf(api.ctx, "Error getting user commit shas: %v", err)
return
}
log.Infof(api.ctx, "REPO: %v, SHAS:%v", repo, shas)
for _, sha := range shas {
// get a single commit: GET /repos/:owner/:repo/commits/:sha
cs, err := api.getCommitStats(repo.Organization, repo.Repository, sha)
if err != nil {
log.Errorf(api.ctx, "Error getting stats: %v", err)
return
}
statsChannel <- cs
}
}
wg.Done()
}()
}
// list organizations: GET /user/orgs
go func() {
organizations, err := api.getOrganizations()
if err != nil {
log.Errorf(api.ctx, "ERROR GETTING ORGANIZATIONS: %v", err)
return
}
for _, organization := range organizations {
// list repositories: GET /orgs/:org/repos
repositories, err := api.getRepositories(organization)
if err != nil {
log.Errorf(api.ctx, "ERROR GETTING REPOSITORIES: %v", err)
return
}
log.Infof(api.ctx, "REPOSITORIES:%v", repositories)
for _, repository := range repositories {
repositoryChannel <- Repo{organization, repository}
}
}
close(repositoryChannel)
}()
go func() {
wg.Wait()
close(statsChannel)
}()
for cs := range statsChannel {
stats.Additions += cs.Additions
stats.Deletions += cs.Deletions
}
return stats, nil
}
func (api *GithubAPI) getOrganizations() ([]string, error) {
endpoint := "/user/orgs"
var data []struct {
Login string `json:"login"`
}
err := api.makeAPIRequest(endpoint, nil, &data)
if err != nil {
return nil, err
}
names := make([]string, len(data))
for i, v := range data {
names[i] = v.Login
}
return names, nil
}
func (api *GithubAPI) getRepositories(organization string) ([]string, error) {
// GET /orgs/:org/repos
var data []struct {
Name string `json:"name"`
}
err := api.makeAPIRequest("/orgs/"+organization+"/repos", nil, &data)
if err != nil {
return nil, err
}
names := make([]string, len(data))
for i, v := range data {
names[i] = v.Name
}
return names, nil
}
func (api *GithubAPI) getUserCommitShas(organization, repository string, since time.Time) ([]string, error) {
values := make(url.Values)
values.Add("author", api.username)
values.Add("since", since.Format(time.RFC3339))
// GET /repos/:owner/:repo/commits
endpoint := "/repos/" + organization + "/" + repository + "/commits"
var data []struct {
SHA string `json:"sha"`
}
err := api.makeAPIRequest(endpoint, values, &data)
if err != nil {
return nil, err
}
shas := make([]string, len(data))
for i, v := range data {
shas[i] = v.SHA
}
return shas, nil
}
func (api *GithubAPI) getCommitStats(organization, repository, sha string) (CommitStats, error) {
var stats CommitStats
endpoint := "/repos/" + organization + "/" + repository + "/commits/" + sha
var data struct {
Stats struct {
Additions int `json:"additions"`
Deletions int `json:"deletions"`
} `json:"stats"`
}
err := api.makeAPIRequest(endpoint, nil, &data)
if err != nil {
return stats, err
}
stats.Additions = data.Stats.Additions
stats.Deletions = data.Stats.Deletions
return stats, nil
}
func (api *GithubAPI) makeAPIRequest(endpoint string, values url.Values, dst interface{}) error {
client := urlfetch.Client(api.ctx)
if values == nil {
values = make(url.Values)
}
values.Add("access_token", api.accessToken)
// GET /user/orgs
response, err := client.Get(githubAPIURL + endpoint + "?" + values.Encode())
if err != nil {
return err
}
defer response.Body.Close()
bs, _ := ioutil.ReadAll(response.Body)
//log.Infof(api.ctx, "GET %s RESPONSE: %s", endpoint, string(bs))
return json.Unmarshal(bs, dst)
}