1
0
Fork 0

Saving response meta

This commit is contained in:
Gregory Eremin 2015-03-05 03:14:14 +07:00
parent 3dbe630464
commit 8ac16bb695
4 changed files with 39 additions and 14 deletions

17
db/token.go Normal file
View File

@ -0,0 +1,17 @@
package db
import (
"time"
)
type (
Token struct {
ID int
Owner string
Token string
Limit int
Remaining int
ResetAt time.Time
CreatedAt time.Time
}
)

View File

@ -3,6 +3,7 @@ package task
import ( import (
"code.google.com/p/goauth2/oauth" "code.google.com/p/goauth2/oauth"
"github.com/google/go-github/github" "github.com/google/go-github/github"
"github.com/localhots/steward/db"
) )
func newGithubClient(token string) *github.Client { func newGithubClient(token string) *github.Client {
@ -12,8 +13,14 @@ func newGithubClient(token string) *github.Client {
return github.NewClient(trans.Client()) return github.NewClient(trans.Client())
} }
// func (c *GithubClient) saveResponseMeta(res *gh.Response) { func saveResponseMeta(token string, res *github.Response) {
// c.limit = res.Limit if res == nil {
// c.remaining = res.Remaining return
// c.limitEnds = res.Reset.Time }
// } db.UpdateToken(&db.Token{
Token: token,
Limit: res.Limit,
Remaining: res.Remaining,
ResetAt: res.Reset.Time,
})
}

View File

@ -1,7 +1,6 @@
package task package task
import ( import (
"github.com/google/go-github/github"
"github.com/localhots/steward/db" "github.com/localhots/steward/db"
"github.com/localhots/steward/job" "github.com/localhots/steward/job"
) )
@ -16,15 +15,16 @@ type (
) )
func SyncContrib(t SyncContribTask) { func SyncContrib(t SyncContribTask) {
contribs := fetchContrib(newGithubClient(t.Token), t.Owner, t.Repo) contribs := fetchContrib(t.Token, t.Owner, t.Repo)
for _, c := range contribs { for _, c := range contribs {
db.ImportRepo(c) db.ImportRepo(c)
} }
} }
func fetchContrib(client *github.Client, owner, repo string) (res []*db.Contrib) { func fetchContrib(token, owner, repo string) (res []*db.Contrib) {
client := newGithubClient(token)
contribs, resp, err := client.Repositories.ListContributorsStats(owner, repo) contribs, resp, err := client.Repositories.ListContributorsStats(owner, repo)
// c.saveResponseMeta(resp) saveResponseMeta(token, resp)
if err != nil { if err != nil {
if err.Error() == "EOF" { if err.Error() == "EOF" {
// Empty repository, not an actual error // Empty repository, not an actual error

View File

@ -15,7 +15,7 @@ type (
) )
func SyncRepos(t SyncReposTask) { func SyncRepos(t SyncReposTask) {
repos := fetchRepos(newGithubClient(t.Token), t.Owner) repos := fetchRepos(t.Token, t.Owner)
for _, repo := range repos { for _, repo := range repos {
db.ImportRepo(&db.Repo{ db.ImportRepo(&db.Repo{
Owner: t.Owner, Owner: t.Owner,
@ -24,10 +24,11 @@ func SyncRepos(t SyncReposTask) {
} }
} }
func fetchRepos(client *github.Client, owner string) { func fetchRepos(token, owner string) {
var ( var (
names = []string{} client = newGithubClient(token)
opt = &github.RepositoryListByOrgOptions{ names = []string{}
opt = &github.RepositoryListByOrgOptions{
ListOptions: github.ListOptions{}, ListOptions: github.ListOptions{},
} }
) )
@ -35,7 +36,7 @@ func fetchRepos(client *github.Client, owner string) {
for { for {
opt.Page++ opt.Page++
repos, resp, err := client.Repositories.ListByOrg(owner, opt) repos, resp, err := client.Repositories.ListByOrg(owner, opt)
// c.saveResponseMeta(resp) // Save current usage/limits saveResponseMeta(token, resp)
if err != nil { if err != nil {
panic(err) panic(err)
} }