1
0
Fork 0

Commits import now works

This commit is contained in:
Gregory Eremin 2015-01-11 19:20:59 +07:00
parent a414952482
commit 69c0aec575
2 changed files with 72 additions and 29 deletions

View File

@ -11,7 +11,7 @@ import (
type ( type (
MysqlStorage struct { MysqlStorage struct {
db *sql.DB db *sql.DB
state map[string]*steward.State State map[string]*steward.State
importStmt *sql.Stmt importStmt *sql.Stmt
saveStateStmt *sql.Stmt saveStateStmt *sql.Stmt
@ -28,7 +28,7 @@ const (
func New(host, user, pass, db string) *MysqlStorage { func New(host, user, pass, db string) *MysqlStorage {
var ( var (
s = &MysqlStorage{ s = &MysqlStorage{
state: map[string]*steward.State{}, State: map[string]*steward.State{},
} }
err error err error
databaseURI = makeDatabaseURI(host, user, pass, db) databaseURI = makeDatabaseURI(host, user, pass, db)
@ -55,6 +55,9 @@ func (ms *MysqlStorage) Import(repo string, hist map[string]*steward.Commit) {
lastSha1 string lastSha1 string
) )
// fmt.Println("saving", len(hist), "commits")
// pretty.Println(hist)
for sha1, c := range hist { for sha1, c := range hist {
if _, err := ms.importStmt.Exec(sha1, c.Author, repo, c.Timestamp); err != nil { if _, err := ms.importStmt.Exec(sha1, c.Author, repo, c.Timestamp); err != nil {
panic(err) panic(err)
@ -65,10 +68,16 @@ func (ms *MysqlStorage) Import(repo string, hist map[string]*steward.Commit) {
} }
} }
ms.saveRepoState(repo, lastSha1, *lastTimestamp) if len(hist) > 0 {
ms.saveRepoState(repo, lastSha1, *lastTimestamp)
}
} }
func (ms *MysqlStorage) saveRepoState(repo string, sha1 string, ts time.Time) { func (ms *MysqlStorage) saveRepoState(repo string, sha1 string, ts time.Time) {
ms.State[repo] = &steward.State{
Sha1: sha1,
Timestamp: ts,
}
if _, err := ms.saveStateStmt.Exec(repo, sha1, ts); err != nil { if _, err := ms.saveStateStmt.Exec(repo, sha1, ts); err != nil {
panic(err) panic(err)
} }
@ -89,7 +98,7 @@ func (ms *MysqlStorage) loadGlobalState() {
if err := rows.Scan(&repo, &sha1, &ts); err != nil { if err := rows.Scan(&repo, &sha1, &ts); err != nil {
panic(err) panic(err)
} }
ms.state[repo] = &steward.State{ ms.State[repo] = &steward.State{
Sha1: sha1, Sha1: sha1,
Timestamp: ts, Timestamp: ts,
} }

View File

@ -2,14 +2,16 @@ package github
import ( import (
"fmt" "fmt"
"time"
"code.google.com/p/goauth2/oauth" "code.google.com/p/goauth2/oauth"
gh "github.com/google/go-github/github" gh "github.com/google/go-github/github"
"github.com/kr/pretty"
"github.com/localhots/steward/steward" "github.com/localhots/steward/steward"
) )
const ( const (
DEFAULT_PER_PAGE = 30 DefaultPerPage = 30
) )
type ( type (
@ -57,7 +59,7 @@ func (c *GithubClient) ListRepos() []string {
names = append(names, *repo.Name) names = append(names, *repo.Name)
} }
if len(repos) < DEFAULT_PER_PAGE { if len(repos) < DefaultPerPage {
break break
} }
} }
@ -66,34 +68,66 @@ func (c *GithubClient) ListRepos() []string {
return names return names
} }
func (c *GithubClient) ListCommits(repo string) map[string]*steward.Commit { func (c *GithubClient) ListCommits(repo string, until *time.Time) (hist map[string]*steward.Commit, hasMore bool) {
var ( hist = map[string]*steward.Commit{}
history = map[string]*steward.Commit{}
opt = &gh.CommitsListOptions{}
)
fmt.Print(repo, " ") opt := &gh.CommitsListOptions{}
for { if until != nil {
fmt.Print(".") opt.Until = *until
commits, _, err := c.client.Repositories.ListCommits(c.owner, repo, opt) }
commits, _, err := c.client.Repositories.ListCommits(c.owner, repo, opt)
if err != nil {
fmt.Println("Error fetching commits: ", err.Error())
return
}
// fmt.Println("Fetched", len(commits), "commits until", opt.Until)
hasMore = (len(commits) == DefaultPerPage)
for _, c := range commits {
commit, err := makeCommit(&c)
if err != nil { if err != nil {
fmt.Println("Error:", err.Error())
continue
}
hist[commit.Sha1] = commit
}
return
}
func makeCommit(c *gh.RepositoryCommit) (commit *steward.Commit, err error) {
defer func() {
if err := recover(); err != nil {
fmt.Print("\n\n\nTroubles with commit:")
pretty.Println(c)
fmt.Println("")
panic(err) panic(err)
} }
}()
for _, c := range commits { commit = &steward.Commit{}
history[*c.SHA] = &steward.Commit{ if c.SHA != nil {
Repo: repo, commit.Sha1 = *c.SHA
Author: *c.Author.Login, } else {
Timestamp: *c.Commit.Author.Date, return nil, fmt.Errorf("Missing commit SHA1 field")
}
opt.Until = *c.Commit.Author.Date
}
if len(commits) < DEFAULT_PER_PAGE {
break
}
} }
fmt.Print("\n")
return history if c.Author != nil {
commit.Author = *c.Author.Login
} else {
return nil, fmt.Errorf("Missing author field")
}
if c.Commit != nil {
if c.Commit.Author != nil {
commit.Timestamp = *c.Commit.Author.Date
} else {
return nil, fmt.Errorf("Missing commit author field")
}
} else {
return nil, fmt.Errorf("Missing commit field")
}
return
} }