From d2231d31934cb1de3a6907c1f4c5d63a7fc8f1b7 Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Thu, 5 Mar 2015 02:15:05 +0700 Subject: [PATCH] Sync repos task --- task/common.go | 6 ++++++ task/sync_repos.go | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 task/sync_repos.go diff --git a/task/common.go b/task/common.go index 6a7f3f5..fb05bf3 100644 --- a/task/common.go +++ b/task/common.go @@ -11,3 +11,9 @@ func newGithubClient(token string) *github.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 +// } diff --git a/task/sync_repos.go b/task/sync_repos.go new file mode 100644 index 0000000..eb15703 --- /dev/null +++ b/task/sync_repos.go @@ -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 +}