2015-03-04 19:15:05 +00:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/google/go-github/github"
|
|
|
|
"github.com/localhots/steward/db"
|
|
|
|
"github.com/localhots/steward/job"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
SyncReposTask struct {
|
|
|
|
Owner string
|
|
|
|
Token string
|
|
|
|
job.Task
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func SyncRepos(t SyncReposTask) {
|
2015-03-04 20:14:14 +00:00
|
|
|
repos := fetchRepos(t.Token, t.Owner)
|
2015-03-04 19:15:05 +00:00
|
|
|
for _, repo := range repos {
|
|
|
|
db.ImportRepo(&db.Repo{
|
|
|
|
Owner: t.Owner,
|
|
|
|
Name: repo,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-04 20:14:14 +00:00
|
|
|
func fetchRepos(token, owner string) {
|
2015-03-04 19:15:05 +00:00
|
|
|
var (
|
2015-03-04 20:14:14 +00:00
|
|
|
client = newGithubClient(token)
|
|
|
|
names = []string{}
|
|
|
|
opt = &github.RepositoryListByOrgOptions{
|
2015-03-04 19:15:05 +00:00
|
|
|
ListOptions: github.ListOptions{},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
for {
|
|
|
|
opt.Page++
|
|
|
|
repos, resp, err := client.Repositories.ListByOrg(owner, opt)
|
2015-03-04 20:14:14 +00:00
|
|
|
saveResponseMeta(token, resp)
|
2015-03-04 19:15:05 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
for _, repo := range repos {
|
|
|
|
names = append(names, *repo.Name)
|
|
|
|
}
|
|
|
|
if len(repos) < 30 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return names
|
|
|
|
}
|