diff --git a/db/token.go b/db/token.go new file mode 100644 index 0000000..25bc029 --- /dev/null +++ b/db/token.go @@ -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 + } +) diff --git a/task/common.go b/task/common.go index fb05bf3..664855a 100644 --- a/task/common.go +++ b/task/common.go @@ -3,6 +3,7 @@ package task import ( "code.google.com/p/goauth2/oauth" "github.com/google/go-github/github" + "github.com/localhots/steward/db" ) func newGithubClient(token string) *github.Client { @@ -12,8 +13,14 @@ func newGithubClient(token string) *github.Client { return github.NewClient(trans.Client()) } -// func (c *GithubClient) saveResponseMeta(res *gh.Response) { -// c.limit = res.Limit -// c.remaining = res.Remaining -// c.limitEnds = res.Reset.Time -// } +func saveResponseMeta(token string, res *github.Response) { + if res == nil { + return + } + db.UpdateToken(&db.Token{ + Token: token, + Limit: res.Limit, + Remaining: res.Remaining, + ResetAt: res.Reset.Time, + }) +} diff --git a/task/sync_contrib.go b/task/sync_contrib.go index f66fa21..8554d49 100644 --- a/task/sync_contrib.go +++ b/task/sync_contrib.go @@ -1,7 +1,6 @@ package task import ( - "github.com/google/go-github/github" "github.com/localhots/steward/db" "github.com/localhots/steward/job" ) @@ -16,15 +15,16 @@ type ( ) 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 { 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) - // c.saveResponseMeta(resp) + saveResponseMeta(token, resp) if err != nil { if err.Error() == "EOF" { // Empty repository, not an actual error diff --git a/task/sync_repos.go b/task/sync_repos.go index eb15703..e35d185 100644 --- a/task/sync_repos.go +++ b/task/sync_repos.go @@ -15,7 +15,7 @@ type ( ) func SyncRepos(t SyncReposTask) { - repos := fetchRepos(newGithubClient(t.Token), t.Owner) + repos := fetchRepos(t.Token, t.Owner) for _, repo := range repos { db.ImportRepo(&db.Repo{ Owner: t.Owner, @@ -24,10 +24,11 @@ func SyncRepos(t SyncReposTask) { } } -func fetchRepos(client *github.Client, owner string) { +func fetchRepos(token, owner string) { var ( - names = []string{} - opt = &github.RepositoryListByOrgOptions{ + client = newGithubClient(token) + names = []string{} + opt = &github.RepositoryListByOrgOptions{ ListOptions: github.ListOptions{}, } ) @@ -35,7 +36,7 @@ func fetchRepos(client *github.Client, owner string) { for { opt.Page++ repos, resp, err := client.Repositories.ListByOrg(owner, opt) - // c.saveResponseMeta(resp) // Save current usage/limits + saveResponseMeta(token, resp) if err != nil { panic(err) }