empact/steward/github/github.go

113 lines
2.0 KiB
Go
Raw Normal View History

2015-01-10 20:48:57 +07:00
package github
import (
"fmt"
2015-01-11 19:20:59 +07:00
"time"
2015-01-10 20:48:57 +07:00
"code.google.com/p/goauth2/oauth"
gh "github.com/google/go-github/github"
"github.com/localhots/steward/steward"
)
const (
2015-01-11 19:20:59 +07:00
DefaultPerPage = 30
2015-01-10 20:48:57 +07:00
)
type (
GithubClient struct {
2015-01-11 21:08:09 +07:00
owner string
client *gh.Client
limit int
remaining int
limitEnds time.Time
2015-01-10 20:48:57 +07:00
}
)
func New(clientID, clientSecret, owner string) *GithubClient {
// Auth here
return nil
}
// Temp method
func NewClient(token, owner string) *GithubClient {
trans := &oauth.Transport{
Token: &oauth.Token{AccessToken: token},
}
return &GithubClient{
owner: owner,
client: gh.NewClient(trans.Client()),
}
}
func (c *GithubClient) ListRepos() []string {
var (
names = []string{}
opt = &gh.RepositoryListByOrgOptions{
ListOptions: gh.ListOptions{},
}
)
for {
opt.Page++
repos, _, err := c.client.Repositories.ListByOrg(c.owner, opt)
if err != nil {
panic(err)
}
for _, repo := range repos {
names = append(names, *repo.Name)
}
2015-01-11 19:20:59 +07:00
if len(repos) < DefaultPerPage {
2015-01-10 20:48:57 +07:00
break
}
}
return names
}
2015-01-11 21:08:09 +07:00
func (c *GithubClient) ListContributors(repo string) []*steward.Contribution {
var (
contrib = []*steward.Contribution{}
)
2015-01-11 19:20:59 +07:00
2015-01-11 21:08:09 +07:00
cslist, resp, err := c.client.Repositories.ListContributorsStats(c.owner, repo)
c.saveResponseMeta(resp)
2015-01-11 19:20:59 +07:00
if err != nil {
2015-01-11 21:08:09 +07:00
if err.Error() == "EOF" {
// Empty repository, not an actual error
return contrib
2015-01-10 20:48:57 +07:00
}
2015-01-11 21:08:09 +07:00
fmt.Println("Error loading contributors stats for repo", repo)
fmt.Println(err.Error())
return contrib
2015-01-11 19:20:59 +07:00
}
2015-01-11 21:08:09 +07:00
for _, cs := range cslist {
for _, week := range cs.Weeks {
if *week.Commits == 0 {
continue
}
contrib = append(contrib, &steward.Contribution{
Author: *cs.Author.Login,
Repo: repo,
Week: week.Week.Time.Unix(),
Commits: *week.Commits,
Additions: *week.Additions,
Deletions: *week.Deletions,
})
2015-01-10 20:48:57 +07:00
}
}
2015-01-11 21:08:09 +07:00
return contrib
}
func (c *GithubClient) saveResponseMeta(res *gh.Response) {
c.limit = res.Limit
c.remaining = res.Remaining
c.limitEnds = res.Reset.Time
2015-01-10 20:48:57 +07:00
}