-
Notifications
You must be signed in to change notification settings - Fork 12
/
token.go
63 lines (56 loc) · 1.44 KB
/
token.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
package routers
import (
"net/http"
"github.com/Unknwon/macaron"
"github.com/gobuild/gobuild/models"
"github.com/gobuild/gobuild/models/github"
"github.com/gobuild/gobuild/models/goutils"
"github.com/gobuild/oauth2"
)
func Build(tokens oauth2.Tokens, ctx *macaron.Context, req *http.Request) {
gh := github.New(tokens.Access())
user, err := gh.User()
if err != nil {
ctx.Error(500, err.Error())
return
}
muser := &models.User{
Name: user.Name,
Email: user.Email,
GithubToken: tokens.Access(),
Admin: false,
}
models.DB.Insert(muser)
// repos
var repos []*github.Repository
reposKey := "orgs:" + user.Login + ":repos"
if !rdx.Exists(reposKey).Val() || req.FormValue("refresh") != "" {
var err error
repos, err = gh.Repositories()
if err != nil {
ctx.Error(500, err.Error())
return
}
for _, repo := range repos {
rdx.HMSet(reposKey, repo.Fullname, "")
}
} else {
for _, repoName := range rdx.HKeys(reposKey).Val() {
repos = append(repos, &github.Repository{
Fullname: repoName,
})
}
}
// token
rdx.Set("user:"+user.Login+":github_token", tokens.Access(), 0)
tokenKey := "user:" + user.Login + ":token"
if !rdx.Exists(tokenKey).Val() {
rdx.Set(tokenKey, "gr"+goutils.RandNString(40), 0)
}
token := rdx.Get(tokenKey).Val()
rdx.Set("token:"+token+":user", user.Login, 0)
ctx.Data["User"] = user
ctx.Data["Token"] = token
ctx.Data["Repos"] = repos
ctx.HTML(200, "build")
}