diff --git a/config.example.json b/config.example.json index 452275b..cf19dc6 100644 --- a/config.example.json +++ b/config.example.json @@ -1,6 +1,6 @@ { "app_domain": "localhost", - "database_uri": "root@/empact", + "database_uri": "root@/empact?parseTime=true", "github_auth_url": "https://github.com/login/oauth/authorize", "github_access_token_url": "https://github.com/login/oauth/access_token", "github_client_id": "XXXXXXXXXXXXXXXXXXXX", diff --git a/db/contrib.go b/db/contrib.go index 62b85cf..0a54d6b 100644 --- a/db/contrib.go +++ b/db/contrib.go @@ -1,13 +1,13 @@ package db type Contrib struct { - Week int64 `json:"week"` + Week uint64 `json:"week"` Author string `json:"author"` Owner string `json:"owner"` Repo string `json:"repo"` - Commits int64 `json:"commits"` - Additions int64 `json:"additions"` - Deletions int64 `json:"deletions"` + Commits uint64 `json:"commits"` + Additions uint64 `json:"additions"` + Deletions uint64 `json:"deletions"` } const saveContribQuery = ` diff --git a/db/org.go b/db/org.go index bd8bc03..1c2ae0e 100644 --- a/db/org.go +++ b/db/org.go @@ -3,7 +3,7 @@ package db type Org struct { Login string `json:"login"` Descr string `json:"descr"` - ID int64 `json:"id"` + ID uint64 `json:"id"` AvatarURL string `json:"avatar_url"` } diff --git a/db/repo.go b/db/repo.go index ccfe59e..b67b4e0 100644 --- a/db/repo.go +++ b/db/repo.go @@ -1,12 +1,32 @@ package db +import ( + "time" +) + type Repo struct { - Owner string `json:"owner"` - Name string `json:"name"` + ID uint64 `json:"id"` + Owner string `json:"owner"` + Name string `json:"name"` + UpdatedAt time.Time `json:"updated_at"` + IsPrivate bool `json:"is_private"` + IsForm bool `json:"is_fork"` } -const saveRepoQuery = `replace into repos (owner, name, updated_at) values (?, ?, now())` +const orgReposQuery = `select * from repos where owner = ?` +const saveRepoQuery = ` +insert into repos (owner, name, updated_at) +values (?, ?, now()) +on duplicate key update +updated_at=now()` func (r *Repo) Save() { conn.MustExec(saveRepoQuery, r.Owner, r.Name) } + +func OrgRepos(login string) (repos []*Repo) { + if err := conn.Select(&repos, orgReposQuery, login); err != nil { + panic(err) + } + return +} diff --git a/db/team.go b/db/team.go index 1939f15..f03ee22 100644 --- a/db/team.go +++ b/db/team.go @@ -1,7 +1,7 @@ package db type Team struct { - ID int64 `json:"id"` + ID uint64 `json:"id"` Owner string `json:"owner"` Name string `json:"name"` } diff --git a/db/token.go b/db/token.go index 8453fc3..06b1005 100644 --- a/db/token.go +++ b/db/token.go @@ -5,11 +5,11 @@ import ( ) type Token struct { - ID int64 `json:"id"` + ID uint64 `json:"id"` User string `json:"user"` Token string `json:"token"` - Quota int64 `json:"quota"` - Remaining int64 `json:"remaining"` + Quota uint64 `json:"quota"` + Remaining uint64 `json:"remaining"` ResetAt time.Time `json:"reset_at"` CreatedAt time.Time `json:"created_at"` } diff --git a/db/user.go b/db/user.go index f6bc21e..b103738 100644 --- a/db/user.go +++ b/db/user.go @@ -3,7 +3,7 @@ package db type User struct { Login string `json:"login"` Name string `json:"name"` - ID int64 `json:"id"` + ID uint64 `json:"id"` AvatarURL string `json:"avatar_url"` } diff --git a/server/api.go b/server/api.go index bedb367..f5135bc 100644 --- a/server/api.go +++ b/server/api.go @@ -7,7 +7,7 @@ import ( "github.com/localhots/empact/db" ) -func orgsListHandler(w http.ResponseWriter, r *http.Request) { +func apiOrgsHandler(w http.ResponseWriter, r *http.Request) { login := currentUser(r) orgs := db.UserOrgs(login) b, _ := json.Marshal(orgs) @@ -16,10 +16,18 @@ func orgsListHandler(w http.ResponseWriter, r *http.Request) { w.Write(b) } -func orgTeamsHandler(w http.ResponseWriter, r *http.Request) { +func apiTeamsHandler(w http.ResponseWriter, r *http.Request) { teams := db.OrgTeams(r.FormValue("org")) b, _ := json.Marshal(teams) w.Header().Set("Content-Type", "application/json; charset=utf8") w.Write(b) } + +func apiReposHandler(w http.ResponseWriter, r *http.Request) { + repos := db.OrgRepos(r.FormValue("org")) + b, _ := json.Marshal(repos) + + w.Header().Set("Content-Type", "application/json; charset=utf8") + w.Write(b) +} diff --git a/server/server.go b/server/server.go index e35b7fd..1fc2dc0 100644 --- a/server/server.go +++ b/server/server.go @@ -20,8 +20,9 @@ func init() { http.HandleFunc("/", sessionHandler) http.HandleFunc("/api/", authHandler) - http.HandleFunc("/api/orgs", orgsListHandler) - http.HandleFunc("/api/teams", orgTeamsHandler) + http.HandleFunc("/api/orgs", apiOrgsHandler) + http.HandleFunc("/api/teams", apiTeamsHandler) + http.HandleFunc("/api/repos", apiReposHandler) http.HandleFunc("/auth/hello", authHelloHandler) http.HandleFunc("/auth/signin", authSigninHandler) http.HandleFunc("/auth/callback", authCallbackHandler) diff --git a/server/session.go b/server/session.go index aa90cca..3be26d2 100644 --- a/server/session.go +++ b/server/session.go @@ -38,11 +38,9 @@ func sessionID(r *http.Request) string { return cook.Value } -func currentUser(r *http.Request) string { - conn := redisC.Get() - login, _ := redis.String(conn.Do("HGET", "sessions", sessionID(r))) - - return login +func currentUser(r *http.Request) (login string) { + login, _ = redis.String(redisC.Get().Do("HGET", "sessions", sessionID(r))) + return } func authorize(r *http.Request, login string) { diff --git a/task/common.go b/task/common.go index a171c9b..0e79bdc 100644 --- a/task/common.go +++ b/task/common.go @@ -19,8 +19,8 @@ func saveResponseMeta(token string, res *github.Response) { } tok := &db.Token{ Token: token, - Quota: int64(res.Limit), - Remaining: int64(res.Remaining), + Quota: uint64(res.Limit), + Remaining: uint64(res.Remaining), ResetAt: res.Reset.Time, } tok.Save() diff --git a/task/sync.go b/task/sync.go index f20f487..d6a1763 100644 --- a/task/sync.go +++ b/task/sync.go @@ -49,13 +49,13 @@ func SyncContrib(token, owner, repo string) { } contrib := &db.Contrib{ - Week: week.Week.Time.Unix(), + Week: uint64(week.Week.Time.Unix()), Author: *c.Author.Login, Owner: owner, Repo: repo, - Commits: int64(*week.Commits), - Additions: int64(*week.Additions), - Deletions: int64(*week.Deletions), + Commits: uint64(*week.Commits), + Additions: uint64(*week.Additions), + Deletions: uint64(*week.Deletions), } contrib.Save() }