1
0
Fork 0

Cleanup old code

This commit is contained in:
Gregory Eremin 2015-03-05 02:14:11 +07:00
parent cd6fb6923a
commit 0d973127c7
6 changed files with 2 additions and 222 deletions

View File

@ -1,4 +0,0 @@
{
"github_client_id": "XXXXXXXXXXXXXXXXXXXX",
"github_secret": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
}

View File

@ -2,6 +2,8 @@ package db
import (
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
var (

View File

@ -1,85 +0,0 @@
package mysql
import (
"database/sql"
"strconv"
_ "github.com/go-sql-driver/mysql"
"github.com/localhots/steward/steward"
)
type (
Config struct {
Hostname string
Port int
Username string
Password string
Database string
}
Storage struct {
db *sql.DB
importStmt *sql.Stmt
}
)
const (
importQuery = "" +
"replace into contributions (author, repo, week, commits, additions, deletions) " +
"values (?, ?, ?, ?, ?, ?)"
)
func New(c *Config) *Storage {
var (
s = &Storage{}
err error
)
if s.db, err = sql.Open("mysql", c.URI()); err != nil {
panic(err)
}
if s.importStmt, err = s.db.Prepare(importQuery); err != nil {
panic(err)
}
return s
}
func (s *Storage) ImportContributions(contrib []*steward.Contribution) {
for _, c := range contrib {
if _, err := s.importStmt.Exec(
c.Author,
c.Repo,
c.Week,
c.Commits,
c.Additions,
c.Deletions,
); err != nil {
panic(err)
}
}
}
func (c *Config) URI() (uri string) {
if c.Username == "" && c.Password != "" {
panic("Password is set but no username specified")
}
if c.Username != "" {
uri += c.Username
}
if c.Password != "" {
uri += ":" + c.Password
}
if c.Username != "" {
uri += "@"
}
if c.Hostname != "" {
uri += c.Hostname
}
if c.Port != 0 {
uri += ":" + strconv.Itoa(c.Port)
}
uri += "/" + c.Database + "?parseTime=true"
return
}

View File

@ -1,8 +0,0 @@
package steward
type (
Config struct {
GithubClientID string
GithubSecret string
}
)

View File

@ -1,12 +0,0 @@
package steward
type (
Contribution struct {
Author string
Repo string
Week int64
Commits int
Additions int
Deletions int
}
)

View File

@ -1,113 +0,0 @@
package github
import (
"fmt"
"time"
"code.google.com/p/goauth2/oauth"
gh "github.com/google/go-github/github"
"github.com/localhots/steward/steward"
)
const (
DefaultPerPage = 30
)
type (
GithubClient struct {
owner string
client *gh.Client
limit int
remaining int
limitEnds time.Time
}
)
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, resp, err := c.client.Repositories.ListByOrg(c.owner, opt)
c.saveResponseMeta(resp)
if err != nil {
panic(err)
}
for _, repo := range repos {
names = append(names, *repo.Name)
}
if len(repos) < DefaultPerPage {
break
}
}
return names
}
func (c *GithubClient) ListContributors(repo string) []*steward.Contribution {
var (
contrib = []*steward.Contribution{}
)
cslist, resp, err := c.client.Repositories.ListContributorsStats(c.owner, repo)
c.saveResponseMeta(resp)
if err != nil {
if err.Error() == "EOF" {
// Empty repository, not an actual error
return contrib
}
fmt.Println("Error loading contributors stats for repo", repo)
fmt.Println(err.Error())
return contrib
}
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,
})
}
}
return contrib
}
func (c *GithubClient) saveResponseMeta(res *gh.Response) {
c.limit = res.Limit
c.remaining = res.Remaining
c.limitEnds = res.Reset.Time
}