1
0
Fork 0
empact/task/sync_contrib.go

56 lines
1.0 KiB
Go
Raw Normal View History

2015-03-04 19:08:36 +00:00
package task
import (
"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) {
2015-03-04 20:14:14 +00:00
contribs := fetchContrib(t.Token, t.Owner, t.Repo)
2015-03-04 19:08:36 +00:00
for _, c := range contribs {
db.ImportRepo(c)
}
}
2015-03-04 20:14:14 +00:00
func fetchContrib(token, owner, repo string) (res []*db.Contrib) {
client := newGithubClient(token)
2015-03-04 19:08:36 +00:00
contribs, resp, err := client.Repositories.ListContributorsStats(owner, repo)
2015-03-04 20:14:14 +00:00
saveResponseMeta(token, resp)
2015-03-04 19:08:36 +00:00
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
}