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",
"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",

View File

@ -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 = `

View File

@ -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"`
}

View File

@ -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
}

View File

@ -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"`
}

View File

@ -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"`
}

View File

@ -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"`
}

View File

@ -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)
}

View File

@ -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)

View File

@ -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) {

View File

@ -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()

View File

@ -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()
}