1
0
Fork 0
empact/task/sync.go

141 lines
2.8 KiB
Go
Raw Normal View History

2015-03-06 10:23:13 +00:00
package task
import (
2015-03-06 13:23:01 +00:00
"time"
2015-03-06 10:23:13 +00:00
"github.com/google/go-github/github"
"github.com/localhots/empact/db"
)
func SyncRepos(token, owner string) {
2015-03-06 13:35:13 +00:00
defer report("SyncRepos", time.Now())
2015-03-06 10:23:13 +00:00
client := newGithubClient(token)
opt := &github.RepositoryListByOrgOptions{ListOptions: github.ListOptions{PerPage: 100}}
2015-03-06 10:23:13 +00:00
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 opt.Page == resp.LastPage {
2015-03-06 10:23:13 +00:00
break
}
}
}
func SyncContrib(token, owner, repo string) {
2015-03-06 13:35:13 +00:00
defer report("SyncContrib", time.Now())
2015-03-06 10:23:13 +00:00
client := newGithubClient(token)
2015-03-06 10:23:13 +00:00
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 _, contrib := range contribs {
for _, week := range contrib.Weeks {
2015-03-06 10:23:13 +00:00
if *week.Commits == 0 {
continue
}
c := &db.Contrib{
2015-03-06 11:18:15 +00:00
Week: uint64(week.Week.Time.Unix()),
Author: *contrib.Author.Login,
2015-03-06 10:23:13 +00:00
Owner: owner,
Repo: repo,
2015-03-06 11:18:15 +00:00
Commits: uint64(*week.Commits),
Additions: uint64(*week.Additions),
Deletions: uint64(*week.Deletions),
2015-03-06 10:23:13 +00:00
}
c.Save()
}
}
}
2015-03-19 12:51:20 +00:00
func SyncUserOrgs(token string) (err error) {
defer report("SyncUserOrgs", time.Now())
client := newGithubClient(token)
opt := &github.ListOptions{PerPage: 100}
for {
var orgs []github.Organization
var resp *github.Response
if orgs, resp, err = client.Organizations.List("", opt); err != nil {
return
}
saveResponseMeta(token, resp)
for _, org := range orgs {
var company, avatarURL string
if org.Company != nil {
company = *org.Company
}
if org.AvatarURL != nil {
avatarURL = *org.AvatarURL
}
o := &db.Org{
ID: uint64(*org.ID),
Login: *org.Login,
Company: company,
AvatarURL: avatarURL,
}
go SyncOrgTeams(token, o)
go SyncOrgMembers(token, o)
o.Save()
}
if opt.Page == resp.LastPage {
break
}
}
2015-03-19 12:55:17 +00:00
return
}
func SyncOrgTeams(token string, org *db.Org) (err error) {
defer report("SyncOrgTeams", time.Now())
client := newGithubClient(token)
opt := &github.ListOptions{PerPage: 100}
for {
opt.Page++
var teams []github.Team
var resp *github.Response
if teams, resp, err = client.Organizations.ListTeams(org.Login, opt); err != nil {
return
}
saveResponseMeta(token, resp)
for _, team := range teams {
t := &db.Team{
ID: uint64(*team.ID),
Name: *team.Name,
Slug: *team.Slug,
Permission: *team.Permission,
OrgID: org.ID,
}
t.Save()
}
if opt.Page == resp.LastPage {
break
}
}
2015-03-19 12:51:20 +00:00
return
}
2015-03-06 10:23:13 +00:00
}
}
}