1
0
Fork 0

Repos api

This commit is contained in:
Gregory Eremin 2015-03-06 18:18:15 +07:00
parent 4b6f79f366
commit d03c5e41e0
12 changed files with 56 additions and 29 deletions

View File

@ -1,6 +1,6 @@
{ {
"app_domain": "localhost", "app_domain": "localhost",
"database_uri": "root@/empact", "database_uri": "root@/empact?parseTime=true",
"github_auth_url": "https://github.com/login/oauth/authorize", "github_auth_url": "https://github.com/login/oauth/authorize",
"github_access_token_url": "https://github.com/login/oauth/access_token", "github_access_token_url": "https://github.com/login/oauth/access_token",
"github_client_id": "XXXXXXXXXXXXXXXXXXXX", "github_client_id": "XXXXXXXXXXXXXXXXXXXX",

View File

@ -1,13 +1,13 @@
package db package db
type Contrib struct { type Contrib struct {
Week int64 `json:"week"` Week uint64 `json:"week"`
Author string `json:"author"` Author string `json:"author"`
Owner string `json:"owner"` Owner string `json:"owner"`
Repo string `json:"repo"` Repo string `json:"repo"`
Commits int64 `json:"commits"` Commits uint64 `json:"commits"`
Additions int64 `json:"additions"` Additions uint64 `json:"additions"`
Deletions int64 `json:"deletions"` Deletions uint64 `json:"deletions"`
} }
const saveContribQuery = ` const saveContribQuery = `

View File

@ -3,7 +3,7 @@ package db
type Org struct { type Org struct {
Login string `json:"login"` Login string `json:"login"`
Descr string `json:"descr"` Descr string `json:"descr"`
ID int64 `json:"id"` ID uint64 `json:"id"`
AvatarURL string `json:"avatar_url"` AvatarURL string `json:"avatar_url"`
} }

View File

@ -1,12 +1,32 @@
package db package db
import (
"time"
)
type Repo struct { type Repo struct {
Owner string `json:"owner"` ID uint64 `json:"id"`
Name string `json:"name"` 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() { func (r *Repo) Save() {
conn.MustExec(saveRepoQuery, r.Owner, r.Name) 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
}

View File

@ -1,7 +1,7 @@
package db package db
type Team struct { type Team struct {
ID int64 `json:"id"` ID uint64 `json:"id"`
Owner string `json:"owner"` Owner string `json:"owner"`
Name string `json:"name"` Name string `json:"name"`
} }

View File

@ -5,11 +5,11 @@ import (
) )
type Token struct { type Token struct {
ID int64 `json:"id"` ID uint64 `json:"id"`
User string `json:"user"` User string `json:"user"`
Token string `json:"token"` Token string `json:"token"`
Quota int64 `json:"quota"` Quota uint64 `json:"quota"`
Remaining int64 `json:"remaining"` Remaining uint64 `json:"remaining"`
ResetAt time.Time `json:"reset_at"` ResetAt time.Time `json:"reset_at"`
CreatedAt time.Time `json:"created_at"` CreatedAt time.Time `json:"created_at"`
} }

View File

@ -3,7 +3,7 @@ package db
type User struct { type User struct {
Login string `json:"login"` Login string `json:"login"`
Name string `json:"name"` Name string `json:"name"`
ID int64 `json:"id"` ID uint64 `json:"id"`
AvatarURL string `json:"avatar_url"` AvatarURL string `json:"avatar_url"`
} }

View File

@ -7,7 +7,7 @@ import (
"github.com/localhots/empact/db" "github.com/localhots/empact/db"
) )
func orgsListHandler(w http.ResponseWriter, r *http.Request) { func apiOrgsHandler(w http.ResponseWriter, r *http.Request) {
login := currentUser(r) login := currentUser(r)
orgs := db.UserOrgs(login) orgs := db.UserOrgs(login)
b, _ := json.Marshal(orgs) b, _ := json.Marshal(orgs)
@ -16,10 +16,18 @@ func orgsListHandler(w http.ResponseWriter, r *http.Request) {
w.Write(b) 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")) teams := db.OrgTeams(r.FormValue("org"))
b, _ := json.Marshal(teams) b, _ := json.Marshal(teams)
w.Header().Set("Content-Type", "application/json; charset=utf8") w.Header().Set("Content-Type", "application/json; charset=utf8")
w.Write(b) 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)
}

View File

@ -20,8 +20,9 @@ func init() {
http.HandleFunc("/", sessionHandler) http.HandleFunc("/", sessionHandler)
http.HandleFunc("/api/", authHandler) http.HandleFunc("/api/", authHandler)
http.HandleFunc("/api/orgs", orgsListHandler) http.HandleFunc("/api/orgs", apiOrgsHandler)
http.HandleFunc("/api/teams", orgTeamsHandler) http.HandleFunc("/api/teams", apiTeamsHandler)
http.HandleFunc("/api/repos", apiReposHandler)
http.HandleFunc("/auth/hello", authHelloHandler) http.HandleFunc("/auth/hello", authHelloHandler)
http.HandleFunc("/auth/signin", authSigninHandler) http.HandleFunc("/auth/signin", authSigninHandler)
http.HandleFunc("/auth/callback", authCallbackHandler) http.HandleFunc("/auth/callback", authCallbackHandler)

View File

@ -38,11 +38,9 @@ func sessionID(r *http.Request) string {
return cook.Value return cook.Value
} }
func currentUser(r *http.Request) string { func currentUser(r *http.Request) (login string) {
conn := redisC.Get() login, _ = redis.String(redisC.Get().Do("HGET", "sessions", sessionID(r)))
login, _ := redis.String(conn.Do("HGET", "sessions", sessionID(r))) return
return login
} }
func authorize(r *http.Request, login string) { func authorize(r *http.Request, login string) {

View File

@ -19,8 +19,8 @@ func saveResponseMeta(token string, res *github.Response) {
} }
tok := &db.Token{ tok := &db.Token{
Token: token, Token: token,
Quota: int64(res.Limit), Quota: uint64(res.Limit),
Remaining: int64(res.Remaining), Remaining: uint64(res.Remaining),
ResetAt: res.Reset.Time, ResetAt: res.Reset.Time,
} }
tok.Save() tok.Save()

View File

@ -49,13 +49,13 @@ func SyncContrib(token, owner, repo string) {
} }
contrib := &db.Contrib{ contrib := &db.Contrib{
Week: week.Week.Time.Unix(), Week: uint64(week.Week.Time.Unix()),
Author: *c.Author.Login, Author: *c.Author.Login,
Owner: owner, Owner: owner,
Repo: repo, Repo: repo,
Commits: int64(*week.Commits), Commits: uint64(*week.Commits),
Additions: int64(*week.Additions), Additions: uint64(*week.Additions),
Deletions: int64(*week.Deletions), Deletions: uint64(*week.Deletions),
} }
contrib.Save() contrib.Save()
} }