1
0
Fork 0
This commit is contained in:
Gregory Eremin 2015-03-06 17:23:13 +07:00
parent 522d53b14b
commit 2c4de085e7
8 changed files with 151 additions and 149 deletions

View File

@ -14,6 +14,6 @@ var (
func Connect(params string) (err error) { func Connect(params string) (err error) {
conn, err = sqlx.Connect("mysql", params) conn, err = sqlx.Connect("mysql", params)
conn.Mappper = reflectx.NewMapperFunc("json", strings.ToLower) conn.Mapper = reflectx.NewMapperFunc("json", strings.ToLower)
return return
} }

View File

@ -6,8 +6,7 @@ import (
"net/url" "net/url"
"github.com/localhots/empact/config" "github.com/localhots/empact/config"
"github.com/localhots/empact/db"
"github.com/localhots/empact/job"
"github.com/localhots/empact/task" "github.com/localhots/empact/task"
) )
@ -31,17 +30,10 @@ func authCallbackHandler(w http.ResponseWriter, r *http.Request) {
code := r.FormValue("code") code := r.FormValue("code")
fmt.Println("Got code: ", code) fmt.Println("Got code: ", code)
res := make(chan string) if _, login, err := task.Authenticate(code); err == nil {
job.Enqueue(&task.FetchAccessTokenTask{
Code: code,
Result: res,
Task: &db.Task{},
})
if login, ok := <-res; ok {
authorize(r, login) authorize(r, login)
} else { } else {
panic("Failed to access token or user info") panic(err)
} }
} }
} }

View File

@ -1,67 +0,0 @@
package task
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"github.com/localhots/empact/config"
"github.com/localhots/empact/db"
)
func FetchAccessToken(code string, result chan string) {
payload := url.Values{}
payload.Set("client_id", config.C().ClientID)
payload.Set("client_secret", config.C().ClientSecret)
payload.Set("code", code)
payload.Set("redirect_uri", config.C().RedirectURI)
buf := bytes.NewBuffer([]byte(payload.Encode()))
fmt.Println("Requesting token")
resp, err := http.Post(config.C().AccessTokenURL, "application/x-www-form-urlencoded", buf)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println(string(body))
pairs, _ := url.ParseQuery(string(body))
token := pairs.Get("access_token")
fmt.Println("Got token: " + token)
fmt.Println("Requesting info")
resp, err = http.Get("https://api.github.com/user?access_token=" + token)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err = ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println(string(body))
user := &db.User{}
json.Unmarshal(body, &user)
user.Save()
fmt.Println("Saving user", user)
tok := &db.Token{
User: user.Login,
Token: token,
}
fmt.Println("Saving token", tok)
tok.Save()
result <- user.Login
}

82
task/auth.go Normal file
View File

@ -0,0 +1,82 @@
package task
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"github.com/localhots/empact/config"
"github.com/localhots/empact/db"
)
func Authenticate(code string) (token, login string, err error) {
if token, err = FetchAccessToken(code); err != nil {
return
}
fmt.Println("Got token: " + token)
var user *db.User
if user, err = FetchUserInfoWithToken(token); err != nil {
return
}
login = user.Login
fmt.Println("Saving user", user)
user.Save()
tok := &db.Token{
User: login,
Token: token,
}
fmt.Println("Saving token", tok)
tok.Save()
return
}
func FetchAccessToken(code string) (token string, err error) {
payload := url.Values{}
payload.Set("client_id", config.C().ClientID)
payload.Set("client_secret", config.C().ClientSecret)
payload.Set("code", code)
payload.Set("redirect_uri", config.C().RedirectURI)
fmt.Println("Requesting token")
buf := bytes.NewBuffer([]byte(payload.Encode()))
var resp *http.Response
if resp, err = http.Post(config.C().AccessTokenURL, "application/x-www-form-urlencoded", buf); err != nil {
return
}
defer resp.Body.Close()
var body []byte
if body, err = ioutil.ReadAll(resp.Body); err != nil {
return
}
pairs, _ := url.ParseQuery(string(body))
if token = pairs.Get("access_token"); token == "" {
err = fmt.Errorf("Failed to fetch access token usign code %q: %s", code, pairs.Get("error_description"))
}
return
}
func FetchUserInfoWithToken(token string) (u *db.User, err error) {
var resp *http.Response
if resp, err = http.Get("https://api.github.com/user?access_token=" + token); err != nil {
return
}
defer resp.Body.Close()
var body []byte
if body, err = ioutil.ReadAll(resp.Body); err != nil {
return
}
u = &db.User{}
err = json.Unmarshal(body, &u)
return
}

View File

@ -19,8 +19,8 @@ func saveResponseMeta(token string, res *github.Response) {
} }
tok := &db.Token{ tok := &db.Token{
Token: token, Token: token,
Limit: res.Limit, Quota: int64(res.Limit),
Remaining: res.Remaining, Remaining: int64(res.Remaining),
ResetAt: res.Reset.Time, ResetAt: res.Reset.Time,
} }
tok.Save() tok.Save()

63
task/sync.go Normal file
View File

@ -0,0 +1,63 @@
package task
import (
"github.com/google/go-github/github"
"github.com/localhots/empact/db"
)
func SyncRepos(token, owner string) {
client := newGithubClient(token)
opt := &github.RepositoryListByOrgOptions{
ListOptions: github.ListOptions{},
}
for {
opt.Page++
repos, resp, err := client.Repositories.ListByOrg(owner, opt)
saveResponseMeta(token, resp)
if err != nil {
panic(err)
}
for _, repo := range repos {
r := &db.Repo{
Owner: owner,
Name: *repo.Name,
}
r.Save()
}
if len(repos) < 30 {
break
}
}
}
func SyncContrib(token, owner, repo string) {
client := newGithubClient(token)
contribs, resp, err := client.Repositories.ListContributorsStats(owner, repo)
saveResponseMeta(token, resp)
if err != nil {
if err.Error() == "EOF" {
return // Empty repository, not an actual error
}
panic(err)
}
for _, c := range contribs {
for _, week := range c.Weeks {
if *week.Commits == 0 {
continue
}
contrib := &db.Contrib{
Week: week.Week.Time.Unix(),
Author: *c.Author.Login,
Owner: owner,
Repo: repo,
Commits: int64(*week.Commits),
Additions: int64(*week.Additions),
Deletions: int64(*week.Deletions),
}
contrib.Save()
}
}
}

View File

@ -1,36 +0,0 @@
package task
import (
"github.com/localhots/empact/db"
)
func SyncContrib(token, owner, repo string) {
client := newGithubClient(token)
contribs, resp, err := client.Repositories.ListContributorsStats(owner, repo)
saveResponseMeta(token, resp)
if err != nil {
if err.Error() == "EOF" {
return // Empty repository, not an actual error
}
panic(err)
}
for _, c := range contribs {
for _, week := range c.Weeks {
if *week.Commits == 0 {
continue
}
contrib := &db.Contrib{
Week: week.Week.Time.Unix(),
Author: *c.Author.Login,
Owner: t.Owner,
Repo: t.Repo,
Commits: *week.Commits,
Additions: *week.Additions,
Deletions: *week.Deletions,
}
contrib.Save()
}
}
}

View File

@ -1,32 +0,0 @@
package task
import (
"github.com/google/go-github/github"
"github.com/localhots/empact/db"
)
func SyncRepos(token, owner string) {
client := newGithubClient(token)
opt := &github.RepositoryListByOrgOptions{
ListOptions: github.ListOptions{},
}
for {
opt.Page++
repos, resp, err := client.Repositories.ListByOrg(owner, opt)
saveResponseMeta(token, resp)
if err != nil {
panic(err)
}
for _, repo := range repos {
r := &db.Repo{
Owner: owner,
Name: *repo.Name,
}
r.Save()
}
if len(repos) < 30 {
break
}
}
}