forked from ArchCI/archci
/
api.go
442 lines (381 loc) · 15.5 KB
/
api.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
package controllers
import (
"net/http"
"crypto/rand"
"encoding/base64"
"fmt"
"time"
log "github.com/Sirupsen/logrus"
"github.com/astaxie/beego"
"github.com/astaxie/beego/session"
"golang.org/x/oauth2"
"github.com/google/go-github/github"
)
// ApiController is the custom controller to provide APIs.
type ApiController struct {
beego.Controller
}
const (
githubAuthorizeUrl = "https://github.com/login/oauth/authorize"
githubTokenUrl = "https://github.com/login/oauth/access_token"
redirectUrl = ""
)
var oauthCfg *oauth2.Config
var tkn *oauth2.Token
type GithubOauthUser struct {
Login string `json:"login"`
ID int `json:"id"`
AvatarURL string `json:"avatar_url"`
HTMLURL string `json:"html_url"`
GravatarID string `json:"gravatar_id"`
Name string `json:"name"`
Company string `json:"company"`
Blog string `json:"blog"`
Location string `json:"location"`
Email string `json:"email"`
Hireable bool `json:"hireable"`
PublicRepos int `json:"public_repos"`
PublicGists int `json:"public_gists"`
Followers int `json:"followers"`
Following int `json:"following"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Type string `json:"type"`
SiteAdmin bool `json:"site_admin"`
URL string `json:"url"`
EventsURL string `json:"events_url"`
FollowingURL string `json:"following_url"`
FollowersURL string `json:"followers_url"`
GistsURL string `json:"gists_url"`
OrganizationsURL string `json:"organizations_url"`
ReceivedEventsURL string `json:"received_events_url"`
ReposURL string `json:"repos_url"`
StarredURL string `json:"starred_url"`
SubscriptionsURL string `json:"subscriptions_url"`
}
type GithubOauthRepositories []struct {
ID int `json:"id"`
Owner struct {
Login string `json:"login"`
ID int `json:"id"`
AvatarURL string `json:"avatar_url"`
HTMLURL string `json:"html_url"`
GravatarID string `json:"gravatar_id"`
Type string `json:"type"`
SiteAdmin bool `json:"site_admin"`
URL string `json:"url"`
EventsURL string `json:"events_url"`
FollowingURL string `json:"following_url"`
FollowersURL string `json:"followers_url"`
GistsURL string `json:"gists_url"`
OrganizationsURL string `json:"organizations_url"`
ReceivedEventsURL string `json:"received_events_url"`
ReposURL string `json:"repos_url"`
StarredURL string `json:"starred_url"`
SubscriptionsURL string `json:"subscriptions_url"`
} `json:"owner"`
Name string `json:"name"`
FullName string `json:"full_name"`
Description string `json:"description"`
Homepage string `json:"homepage"`
DefaultBranch string `json:"default_branch"`
CreatedAt time.Time `json:"created_at"`
PushedAt time.Time `json:"pushed_at"`
UpdatedAt time.Time `json:"updated_at"`
HTMLURL string `json:"html_url"`
CloneURL string `json:"clone_url"`
GitURL string `json:"git_url"`
SSHURL string `json:"ssh_url"`
SvnURL string `json:"svn_url"`
Language string `json:"language"`
Fork bool `json:"fork"`
ForksCount int `json:"forks_count"`
OpenIssuesCount int `json:"open_issues_count"`
StargazersCount int `json:"stargazers_count"`
WatchersCount int `json:"watchers_count"`
Size int `json:"size"`
Permissions struct {
Admin bool `json:"admin"`
Pull bool `json:"pull"`
Push bool `json:"push"`
} `json:"permissions"`
Private bool `json:"private"`
HasIssues bool `json:"has_issues"`
HasWiki bool `json:"has_wiki"`
HasDownloads bool `json:"has_downloads"`
TeamID interface{} `json:"team_id"`
URL string `json:"url"`
ArchiveURL string `json:"archive_url"`
AssigneesURL string `json:"assignees_url"`
BlobsURL string `json:"blobs_url"`
BranchesURL string `json:"branches_url"`
CollaboratorsURL string `json:"collaborators_url"`
CommentsURL string `json:"comments_url"`
CommitsURL string `json:"commits_url"`
CompareURL string `json:"compare_url"`
ContentsURL string `json:"contents_url"`
ContributorsURL string `json:"contributors_url"`
DownloadsURL string `json:"downloads_url"`
EventsURL string `json:"events_url"`
ForksURL string `json:"forks_url"`
GitCommitsURL string `json:"git_commits_url"`
GitRefsURL string `json:"git_refs_url"`
GitTagsURL string `json:"git_tags_url"`
HooksURL string `json:"hooks_url"`
IssueCommentURL string `json:"issue_comment_url"`
IssueEventsURL string `json:"issue_events_url"`
IssuesURL string `json:"issues_url"`
KeysURL string `json:"keys_url"`
LabelsURL string `json:"labels_url"`
LanguagesURL string `json:"languages_url"`
MergesURL string `json:"merges_url"`
MilestonesURL string `json:"milestones_url"`
NotificationsURL string `json:"notifications_url"`
PullsURL string `json:"pulls_url"`
ReleasesURL string `json:"releases_url"`
StargazersURL string `json:"stargazers_url"`
StatusesURL string `json:"statuses_url"`
SubscribersURL string `json:"subscribers_url"`
SubscriptionURL string `json:"subscription_url"`
TagsURL string `json:"tags_url"`
TreesURL string `json:"trees_url"`
TeamsURL string `json:"teams_url"`
}
var globalSessions *session.Manager
func init() {
globalSessions, _ = session.NewManager("memory", `{"cookieName":"gosessionid", "enableSetCookie,omitempty": true, "gclifetime":3600, "maxLifetime": 3600, "secure": false, "sessionIDHashFunc": "sha1", "sessionIDHashKey": "", "cookieLifeTime": 3600, "providerConfig": ""}`)
go globalSessions.GC()
}
func (c *ApiController) LoginGithub() {
log.Debug("Start to login github")
oauthCfg = &oauth2.Config{
ClientID: "69028d944609d7243cbf",
ClientSecret: "efe2f3d0a547f0840fd17411e729d20f02ece615",
Endpoint: oauth2.Endpoint{
AuthURL: githubAuthorizeUrl,
TokenURL: githubTokenUrl,
},
RedirectURL: redirectUrl,
Scopes: []string{"repo"},
}
b := make([]byte, 16)
rand.Read(b)
state := base64.URLEncoding.EncodeToString(b)
url := oauthCfg.AuthCodeURL(state)
http.Redirect(c.Ctx.ResponseWriter, c.Ctx.Request, url, 302)
}
func (c *ApiController) LoginGithubCallback() {
fmt.Println("Start login github callback")
r := c.Ctx.Request
/*
if r.URL.Query().Get("state") != session.Values["state"] {
fmt.Println("no state match; possible csrf OR cookies not enabled")
return
}
*/
tkn, _ = oauthCfg.Exchange(oauth2.NoContext, r.URL.Query().Get("code"))
/*
if err != nil {
fmt.Println("there was an issue getting your token")
//return
}
*/
fmt.Println(tkn)
w := c.Ctx.ResponseWriter
sess, _ := globalSessions.SessionStart(w, r)
defer sess.SessionRelease(w)
//token := sess.Get("token")
//fmt.Println(token)
sess.Set("token", tkn)
if !tkn.Valid() {
fmt.Println("retreived invalid token")
//return
}
client := github.NewClient(oauthCfg.Client(oauth2.NoContext, tkn))
user, _, err := client.Users.Get("")
if err != nil {
fmt.Println("error getting name")
//return
}
fmt.Println(user)
url := "/account"
http.Redirect(c.Ctx.ResponseWriter, c.Ctx.Request, url, 302)
}
// GetAccountInfo return the info of github user.
func (c *ApiController) GetAccountInfo() {
fmt.Println("Start get account info")
//user := GithubOauthUser{Name:"tobe", HTMLURL: "https://github.com/tobegit3hub", Company: "UnitedStack Inc"}
client := github.NewClient(oauthCfg.Client(oauth2.NoContext, tkn))
user, _, err := client.Users.Get("")
if err != nil {
fmt.Println("error getting name")
//return
}
/*
{
"login": "tobegit3hub",
"id": 2715000,
"avatar_url": "https://avatars.githubusercontent.com/u/2715000?v=3",
"html_url": "https://github.com/tobegit3hub",
"gravatar_id": "",
"name": "tobe",
"company": "UnitedStack Inc",
"blog": "http://chendihao.cn",
"location": "Beijing",
"email": "tobeg3oogle@gmail.com",
"hireable": false,
"public_repos": 161,
"public_gists": 1,
"followers": 155,
"following": 68,
"created_at": "2012-11-03T13:17:18Z",
"updated_at": "2015-08-04T19:01:39Z",
"type": "User",
"site_admin": false,
"url": "https://api.github.com/users/tobegit3hub",
"events_url": "https://api.github.com/users/tobegit3hub/events{/privacy}",
"following_url": "https://api.github.com/users/tobegit3hub/following{/other_user}",
"followers_url": "https://api.github.com/users/tobegit3hub/followers",
"gists_url": "https://api.github.com/users/tobegit3hub/gists{/gist_id}",
"organizations_url": "https://api.github.com/users/tobegit3hub/orgs",
"received_events_url": "https://api.github.com/users/tobegit3hub/received_events",
"repos_url": "https://api.github.com/users/tobegit3hub/repos",
"starred_url": "https://api.github.com/users/tobegit3hub/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/tobegit3hub/subscriptions"
}
*/
c.Data["json"] = user
c.ServeJson()
}
// GetAccountOrganizations return the organizations of github user.
func (c *ApiController) GetAccountOrganizations() {
fmt.Println("Start get account organizations")
client := github.NewClient(oauthCfg.Client(oauth2.NoContext, tkn))
organizations, _, err := client.Organizations.List("", nil)
if err != nil {
fmt.Println("error getting organizations")
fmt.Println(nil)
}
c.Data["json"] = organizations
c.ServeJson()
}
// GetAccountProjects returns the projects of github user.
func (c *ApiController) GetAccountProjects() {
//projects := GithubOauthRepositories{{FullName:"ArchCI/archci", HTMLURL:"https://github.com/ArchCI/archci", Language: "Go", Description: "The best CI system"}, {FullName:"ArchCI/simple-worker", Homepage:"https://github.com/ArchCI/simple-worker", Language:"Go", Description: "The client of ArchCI"}}
/*
r := c.Ctx.Request
w := c.Ctx.ResponseWriter
token := tkn
sess, _ := globalSessions.SessionStart(w, r)
defer sess.SessionRelease(w)
if sess.Get("token") == nil {
token = sess.Get("token").(*oauth2.Token)
fmt.Println("hit token")
}
*/
client := github.NewClient(oauthCfg.Client(oauth2.NoContext, tkn))
projects, _, err := client.Repositories.List("", nil)
if err != nil {
fmt.Println("error getting repos")
//return
}
/*
[
{
"id": 36988569,
"owner": {
"login": "ArchCI",
"id": 12673804,
"avatar_url": "https://avatars.githubusercontent.com/u/12673804?v=3",
"html_url": "https://github.com/ArchCI",
"gravatar_id": "",
"type": "Organization",
"site_admin": false,
"url": "https://api.github.com/users/ArchCI",
"events_url": "https://api.github.com/users/ArchCI/events{/privacy}",
"following_url": "https://api.github.com/users/ArchCI/following{/other_user}",
"followers_url": "https://api.github.com/users/ArchCI/followers",
"gists_url": "https://api.github.com/users/ArchCI/gists{/gist_id}",
"organizations_url": "https://api.github.com/users/ArchCI/orgs",
"received_events_url": "https://api.github.com/users/ArchCI/received_events",
"repos_url": "https://api.github.com/users/ArchCI/repos",
"starred_url": "https://api.github.com/users/ArchCI/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/ArchCI/subscriptions"
},
"name": "aci",
"full_name": "ArchCI/aci",
"description": "Command-line client for ArchCI",
"homepage": "http://archci.com",
"default_branch": "master",
"created_at": "2015-06-06T17:21:16Z",
"pushed_at": "2015-06-14T01:00:11Z",
"updated_at": "2015-06-28T13:58:55Z",
"html_url": "https://github.com/ArchCI/aci",
"clone_url": "https://github.com/ArchCI/aci.git",
"git_url": "git://github.com/ArchCI/aci.git",
"ssh_url": "git@github.com:ArchCI/aci.git",
"svn_url": "https://github.com/ArchCI/aci",
"language": "Go",
"fork": false,
"forks_count": 0,
"open_issues_count": 0,
"stargazers_count": 1,
"watchers_count": 1,
"size": 168,
"permissions": {
"admin": true,
"pull": true,
"push": true
},
"private": false,
"has_issues": true,
"has_wiki": true,
"has_downloads": true,
"team_id": null,
"url": "https://api.github.com/repos/ArchCI/aci",
"archive_url": "https://api.github.com/repos/ArchCI/aci/{archive_format}{/ref}",
"assignees_url": "https://api.github.com/repos/ArchCI/aci/assignees{/user}",
"blobs_url": "https://api.github.com/repos/ArchCI/aci/git/blobs{/sha}",
"branches_url": "https://api.github.com/repos/ArchCI/aci/branches{/branch}",
"collaborators_url": "https://api.github.com/repos/ArchCI/aci/collaborators{/collaborator}",
"comments_url": "https://api.github.com/repos/ArchCI/aci/comments{/number}",
"commits_url": "https://api.github.com/repos/ArchCI/aci/commits{/sha}",
"compare_url": "https://api.github.com/repos/ArchCI/aci/compare/{base}...{head}",
"contents_url": "https://api.github.com/repos/ArchCI/aci/contents/{+path}",
"contributors_url": "https://api.github.com/repos/ArchCI/aci/contributors",
"downloads_url": "https://api.github.com/repos/ArchCI/aci/downloads",
"events_url": "https://api.github.com/repos/ArchCI/aci/events",
"forks_url": "https://api.github.com/repos/ArchCI/aci/forks",
"git_commits_url": "https://api.github.com/repos/ArchCI/aci/git/commits{/sha}",
"git_refs_url": "https://api.github.com/repos/ArchCI/aci/git/refs{/sha}",
"git_tags_url": "https://api.github.com/repos/ArchCI/aci/git/tags{/sha}",
"hooks_url": "https://api.github.com/repos/ArchCI/aci/hooks",
"issue_comment_url": "https://api.github.com/repos/ArchCI/aci/issues/comments{/number}",
"issue_events_url": "https://api.github.com/repos/ArchCI/aci/issues/events{/number}",
"issues_url": "https://api.github.com/repos/ArchCI/aci/issues{/number}",
"keys_url": "https://api.github.com/repos/ArchCI/aci/keys{/key_id}",
"labels_url": "https://api.github.com/repos/ArchCI/aci/labels{/name}",
"languages_url": "https://api.github.com/repos/ArchCI/aci/languages",
"merges_url": "https://api.github.com/repos/ArchCI/aci/merges",
"milestones_url": "https://api.github.com/repos/ArchCI/aci/milestones{/number}",
"notifications_url": "https://api.github.com/repos/ArchCI/aci/notifications{?since,all,participating}",
"pulls_url": "https://api.github.com/repos/ArchCI/aci/pulls{/number}",
"releases_url": "https://api.github.com/repos/ArchCI/aci/releases{/id}",
"stargazers_url": "https://api.github.com/repos/ArchCI/aci/stargazers",
"statuses_url": "https://api.github.com/repos/ArchCI/aci/statuses/{sha}",
"subscribers_url": "https://api.github.com/repos/ArchCI/aci/subscribers",
"subscription_url": "https://api.github.com/repos/ArchCI/aci/subscription",
"tags_url": "https://api.github.com/repos/ArchCI/aci/tags",
"trees_url": "https://api.github.com/repos/ArchCI/aci/git/trees{/sha}",
"teams_url": "https://api.github.com/repos/ArchCI/aci/teams"
}
]
*/
c.Data["json"] = projects
c.ServeJson()
}
// GetAccountToken returns github oauth token as string.
func (c *ApiController) GetAccountToken() {
result := tkn.AccessToken
c.Ctx.WriteString(result)
}