1
0
Fork 0

Sync repos task

This commit is contained in:
Gregory Eremin 2015-03-05 02:15:05 +07:00
parent 0d973127c7
commit d2231d3193
2 changed files with 57 additions and 0 deletions

View File

@ -11,3 +11,9 @@ func newGithubClient(token string) *github.Client {
} }
return github.NewClient(trans.Client()) return github.NewClient(trans.Client())
} }
// func (c *GithubClient) saveResponseMeta(res *gh.Response) {
// c.limit = res.Limit
// c.remaining = res.Remaining
// c.limitEnds = res.Reset.Time
// }

51
task/sync_repos.go Normal file
View File

@ -0,0 +1,51 @@
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) {
repos := fetchRepos(newGithubClient(t.Token), t.Owner)
for _, repo := range repos {
db.ImportRepo(&db.Repo{
Owner: t.Owner,
Name: repo,
})
}
}
func fetchRepos(client *github.Client, owner string) {
var (
names = []string{}
opt = &github.RepositoryListByOrgOptions{
ListOptions: github.ListOptions{},
}
)
for {
opt.Page++
repos, resp, err := client.Repositories.ListByOrg(owner, opt)
// c.saveResponseMeta(resp) // Save current usage/limits
if err != nil {
panic(err)
}
for _, repo := range repos {
names = append(names, *repo.Name)
}
if len(repos) < 30 {
break
}
}
return names
}