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)
|
2015-03-19 12:55:01 +00:00
|
|
|
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()
|
|
|
|
}
|
2015-03-19 13:33:58 +00:00
|
|
|
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-19 12:55:01 +00:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2015-03-19 12:55:01 +00:00
|
|
|
for _, contrib := range contribs {
|
|
|
|
for _, week := range contrib.Weeks {
|
2015-03-06 10:23:13 +00:00
|
|
|
if *week.Commits == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-03-19 12:55:01 +00:00
|
|
|
c := &db.Contrib{
|
2015-03-06 11:18:15 +00:00
|
|
|
Week: uint64(week.Week.Time.Unix()),
|
2015-03-19 12:55:01 +00:00
|
|
|
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
|
|
|
}
|
2015-03-19 12:55:01 +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 {
|
2015-03-19 16:20:02 +00:00
|
|
|
opt.Page++
|
2015-03-19 12:51:20 +00:00
|
|
|
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()
|
|
|
|
}
|
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) (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,
|
|
|
|
}
|
2015-03-19 13:33:58 +00:00
|
|
|
go SyncTeamMembers(token, t)
|
2015-03-19 12:55:17 +00:00
|
|
|
t.Save()
|
|
|
|
}
|
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) (err error) {
|
2015-03-19 13:33:58 +00:00
|
|
|
defer report("SyncOrgMembers", time.Now())
|
2015-03-19 12:55:29 +00:00
|
|
|
client := newGithubClient(token)
|
|
|
|
opt := &github.ListMembersOptions{ListOptions: github.ListOptions{PerPage: 100}}
|
|
|
|
|
|
|
|
var ids = []uint64{}
|
|
|
|
for {
|
|
|
|
opt.Page++
|
|
|
|
var users []github.User
|
|
|
|
var resp *github.Response
|
|
|
|
if users, resp, err = client.Organizations.ListMembers(org.Login, opt); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
saveResponseMeta(token, resp)
|
|
|
|
|
|
|
|
for _, user := range users {
|
2015-03-19 16:29:03 +00:00
|
|
|
ids = append(ids, uint64(*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
|
|
|
}
|
|
|
|
}
|
2015-03-19 12:55:29 +00:00
|
|
|
db.SaveOrgMembers(org.ID, ids)
|
|
|
|
|
|
|
|
return
|
2015-03-06 10:23:13 +00:00
|
|
|
}
|
2015-03-19 13:33:58 +00:00
|
|
|
|
|
|
|
func SyncTeamMembers(token string, team *db.Team) (err error) {
|
|
|
|
defer report("SyncTeamMembers", time.Now())
|
|
|
|
client := newGithubClient(token)
|
|
|
|
opt := &github.ListOptions{PerPage: 100}
|
|
|
|
|
|
|
|
var ids = []uint64{}
|
|
|
|
for {
|
|
|
|
opt.Page++
|
|
|
|
var users []github.User
|
|
|
|
var resp *github.Response
|
|
|
|
if users, resp, err = client.Organizations.ListTeamMembers(int(team.ID), opt); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
saveResponseMeta(token, resp)
|
|
|
|
|
|
|
|
for _, user := range users {
|
|
|
|
ids = append(ids, uint64(*user.ID))
|
|
|
|
}
|
|
|
|
if opt.Page >= resp.LastPage {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
db.SaveTeamMembers(team.OrgID, team.ID, ids)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|