Jobs simplified

This commit is contained in:
2015-03-05 02:08:36 +07:00
parent 9bb84d27b7
commit 3216f261c3
5 changed files with 111 additions and 36 deletions
+13
View File
@@ -0,0 +1,13 @@
package task
import (
"code.google.com/p/goauth2/oauth"
"github.com/google/go-github/github"
)
func newGithubClient(token string) *github.Client {
trans := &oauth.Transport{
Token: &oauth.Token{AccessToken: token},
}
return github.NewClient(trans.Client())
}
+55
View File
@@ -0,0 +1,55 @@
package task
import (
"github.com/google/go-github/github"
"github.com/localhots/steward/db"
"github.com/localhots/steward/job"
)
type (
SyncContribTask struct {
Owner string
Repo string
Token string
job.Task
}
)
func SyncContrib(t SyncContribTask) {
contribs := fetchContrib(newGithubClient(t.Token), t.Owner, t.Repo)
for _, c := range contribs {
db.ImportRepo(c)
}
}
func fetchContrib(client *github.Client, owner, repo string) (res []*db.Contrib) {
contribs, resp, err := client.Repositories.ListContributorsStats(owner, repo)
// c.saveResponseMeta(resp)
if err != nil {
if err.Error() == "EOF" {
// Empty repository, not an actual error
return
}
panic(err)
}
for _, c := range contribs {
for _, week := range c.Weeks {
if *week.Commits == 0 {
continue
}
res = append(res, &db.Contrib{
Week: week.Week.Time.Unix(),
Author: *c.Author.Login,
Owner: owner,
Repo: repo,
Commits: *week.Commits,
Additions: *week.Additions,
Deletions: *week.Deletions,
})
}
}
return
}