1
0
Fork 0
empact/task/sync.go

273 lines
6.0 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 SyncUserInfo(token, login string) {
defer report(time.Now(), "SyncUserInfo (%s)", login)
client := newGithubClient(token)
var user *github.User
var resp *github.Response
var err error
if user, resp, err = client.Users.Get(login); err != nil {
panic(err)
}
saveResponseMeta(token, resp)
var name, avatarURL string
if user.Name != nil {
name = *user.Name
}
if user.AvatarURL != nil {
avatarURL = *user.AvatarURL
}
u := &db.User{
Login: *user.Login,
Name: name,
ID: *user.ID,
AvatarURL: avatarURL,
}
db.Now(func() { u.Save() })
return
}
2015-03-21 14:19:17 +00:00
func SyncOrgRepos(token string, org *db.Org) {
defer report(time.Now(), "SyncOrgRepos (%s)", org.Login)
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++
var repos []github.Repository
var resp *github.Response
var err error
if repos, resp, err = client.Repositories.ListByOrg(org.Login, opt); err != nil {
2015-03-06 10:23:13 +00:00
panic(err)
}
saveResponseMeta(token, resp)
2015-03-06 10:23:13 +00:00
for _, repo := range repos {
2015-03-20 11:43:58 +00:00
var descr string
if repo.Description != nil {
descr = *repo.Description
}
2015-03-06 10:23:13 +00:00
r := &db.Repo{
2015-03-21 14:19:17 +00:00
ID: *repo.ID,
OrgID: *repo.Owner.ID,
2015-03-20 11:43:58 +00:00
Name: *repo.Name,
Description: descr,
IsPrivate: *repo.Private,
IsFork: *repo.Fork,
2015-03-06 10:23:13 +00:00
}
2015-03-21 14:19:17 +00:00
go SyncContrib(token, org.Login, r)
db.Now(func() { r.Save() })
2015-03-06 10:23:13 +00:00
}
2015-03-19 13:33:58 +00:00
if opt.Page >= resp.LastPage {
2015-03-06 10:23:13 +00:00
break
}
}
}
2015-03-20 11:48:42 +00:00
func SyncContrib(token, owner string, repo *db.Repo) {
2015-03-21 14:19:17 +00:00
defer report(time.Now(), "SyncContrib (%s/%s)", owner, repo.Name)
2015-03-06 10:23:13 +00:00
client := newGithubClient(token)
var contribs []github.ContributorStats
var resp *github.Response
var err error
if contribs, resp, err = client.Repositories.ListContributorsStats(owner, repo.Name); err != nil {
2015-03-06 10:23:13 +00:00
if err.Error() == "EOF" {
return // Empty repository, not an actual error
}
panic(err)
}
saveResponseMeta(token, resp)
2015-03-06 10:23:13 +00:00
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-20 15:22:49 +00:00
Week: int(week.Week.Time.Unix()),
2015-03-20 11:48:42 +00:00
OrgID: repo.OrgID,
RepoID: repo.ID,
2015-03-20 15:22:49 +00:00
UserID: *contrib.Author.ID,
Commits: *week.Commits,
Additions: *week.Additions,
Deletions: *week.Deletions,
2015-03-06 10:23:13 +00:00
}
db.Now(func() { c.Save() })
}
}
}
2015-03-19 12:51:20 +00:00
func SyncUserOrgs(token string) {
2015-03-21 14:19:17 +00:00
defer report(time.Now(), "SyncUserOrgs")
2015-03-19 12:51:20 +00:00
client := newGithubClient(token)
opt := &github.ListOptions{PerPage: 100}
for {
opt.Page++
2015-03-19 12:51:20 +00:00
var orgs []github.Organization
var resp *github.Response
var err error
2015-03-19 12:51:20 +00:00
if orgs, resp, err = client.Organizations.List("", opt); err != nil {
panic(err)
2015-03-19 12:51:20 +00:00
}
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{
2015-03-20 15:22:49 +00:00
ID: *org.ID,
2015-03-19 12:51:20 +00:00
Login: *org.Login,
Company: company,
AvatarURL: avatarURL,
}
go SyncOrgTeams(token, o)
go SyncOrgMembers(token, o)
2015-03-21 14:19:17 +00:00
go SyncOrgRepos(token, o)
db.Now(func() { o.Save() })
2015-03-19 12:51:20 +00:00
}
2015-03-19 13:33:58 +00:00
if opt.Page >= resp.LastPage {
2015-03-19 12:51:20 +00:00
break
}
}
2015-03-19 12:55:17 +00:00
return
}
func SyncOrgTeams(token string, org *db.Org) {
2015-03-21 14:19:17 +00:00
defer report(time.Now(), "SyncOrgTeams (%s)", org.Login)
2015-03-19 12:55:17 +00:00
client := newGithubClient(token)
opt := &github.ListOptions{PerPage: 100}
for {
opt.Page++
var teams []github.Team
var resp *github.Response
var err error
2015-03-19 12:55:17 +00:00
if teams, resp, err = client.Organizations.ListTeams(org.Login, opt); err != nil {
panic(err)
2015-03-19 12:55:17 +00:00
}
saveResponseMeta(token, resp)
for _, team := range teams {
t := &db.Team{
Name: *team.Name,
2015-03-20 15:22:49 +00:00
ID: *team.ID,
2015-03-19 12:55:17 +00:00
Slug: *team.Slug,
Permission: *team.Permission,
OrgID: org.ID,
}
2015-03-19 13:33:58 +00:00
go SyncTeamMembers(token, t)
2015-03-20 10:45:04 +00:00
go SyncTeamRepos(token, t)
db.Now(func() { t.Save() })
2015-03-19 12:55:17 +00:00
}
2015-03-19 13:33:58 +00:00
if opt.Page >= resp.LastPage {
2015-03-19 12:55:17 +00:00
break
}
}
2015-03-19 12:51:20 +00:00
return
}
2015-03-19 12:55:29 +00:00
func SyncOrgMembers(token string, org *db.Org) {
2015-03-21 14:19:17 +00:00
defer report(time.Now(), "SyncOrgMembers (%s)", org.Login)
2015-03-19 12:55:29 +00:00
client := newGithubClient(token)
opt := &github.ListMembersOptions{ListOptions: github.ListOptions{PerPage: 100}}
2015-03-20 15:22:49 +00:00
var ids = []int{}
2015-03-19 12:55:29 +00:00
for {
opt.Page++
var users []github.User
var resp *github.Response
var err error
2015-03-19 12:55:29 +00:00
if users, resp, err = client.Organizations.ListMembers(org.Login, opt); err != nil {
panic(err)
2015-03-19 12:55:29 +00:00
}
saveResponseMeta(token, resp)
for _, user := range users {
2015-03-20 15:22:49 +00:00
ids = append(ids, *user.ID)
go SyncUserInfo(token, *user.Login)
2015-03-19 12:55:29 +00:00
}
2015-03-19 13:33:58 +00:00
if opt.Page >= resp.LastPage {
2015-03-19 12:55:29 +00:00
break
2015-03-06 10:23:13 +00:00
}
}
db.Now(func() { db.SaveOrgMembers(org.ID, ids) })
2015-03-19 12:55:29 +00:00
return
2015-03-06 10:23:13 +00:00
}
2015-03-19 13:33:58 +00:00
func SyncTeamMembers(token string, team *db.Team) {
2015-03-21 14:19:17 +00:00
defer report(time.Now(), "SyncTeamMembers (%d/%s)", team.OrgID, team.Name)
2015-03-19 13:33:58 +00:00
client := newGithubClient(token)
opt := &github.ListOptions{PerPage: 100}
2015-03-20 15:22:49 +00:00
var ids = []int{}
2015-03-19 13:33:58 +00:00
for {
opt.Page++
var users []github.User
var resp *github.Response
var err error
2015-03-19 13:33:58 +00:00
if users, resp, err = client.Organizations.ListTeamMembers(int(team.ID), opt); err != nil {
panic(err)
2015-03-19 13:33:58 +00:00
}
saveResponseMeta(token, resp)
for _, user := range users {
2015-03-20 15:22:49 +00:00
ids = append(ids, *user.ID)
2015-03-19 13:33:58 +00:00
}
if opt.Page >= resp.LastPage {
break
}
}
db.Now(func() { db.SaveTeamMembers(team.OrgID, team.ID, ids) })
2015-03-19 13:33:58 +00:00
return
}
2015-03-20 10:45:04 +00:00
func SyncTeamRepos(token string, team *db.Team) {
2015-03-21 14:19:17 +00:00
defer report(time.Now(), "SyncTeamRepos (%d/%s)", team.OrgID, team.Name)
2015-03-20 10:45:04 +00:00
client := newGithubClient(token)
opt := &github.ListOptions{PerPage: 100}
2015-03-20 15:22:49 +00:00
var ids = []int{}
2015-03-20 10:45:04 +00:00
for {
opt.Page++
var repos []github.Repository
var resp *github.Response
var err error
2015-03-20 10:45:04 +00:00
if repos, resp, err = client.Organizations.ListTeamRepos(int(team.ID), opt); err != nil {
panic(err)
2015-03-20 10:45:04 +00:00
}
saveResponseMeta(token, resp)
for _, repo := range repos {
2015-03-20 15:22:49 +00:00
ids = append(ids, *repo.ID)
2015-03-20 10:45:04 +00:00
}
if opt.Page >= resp.LastPage {
break
}
}
db.Now(func() { db.SaveTeamRepos(team.OrgID, team.ID, ids) })
2015-03-20 10:45:04 +00:00
return
}