2014-06-05 00:25:38 +03:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
2014-10-28 17:16:50 +02:00
|
|
|
"github.com/drone/drone/plugin/remote"
|
2014-09-29 04:36:24 +03:00
|
|
|
"github.com/drone/drone/server/datastore"
|
2014-10-28 17:16:50 +02:00
|
|
|
"github.com/drone/drone/server/sync"
|
2014-06-13 02:41:04 +03:00
|
|
|
"github.com/drone/drone/shared/model"
|
2014-09-29 04:36:24 +03:00
|
|
|
"github.com/goji/context"
|
|
|
|
"github.com/zenazn/goji/web"
|
2014-06-05 00:25:38 +03:00
|
|
|
)
|
|
|
|
|
2014-09-29 04:36:24 +03:00
|
|
|
// GetUserCurrent accepts a request to retrieve the
|
|
|
|
// currently authenticated user from the datastore
|
|
|
|
// and return in JSON format.
|
|
|
|
//
|
|
|
|
// GET /api/user
|
|
|
|
//
|
|
|
|
func GetUserCurrent(c web.C, w http.ResponseWriter, r *http.Request) {
|
|
|
|
var user = ToUser(c)
|
|
|
|
if user == nil {
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
return
|
2014-06-05 00:25:38 +03:00
|
|
|
}
|
2014-09-29 04:36:24 +03:00
|
|
|
// return private data for the currently authenticated
|
|
|
|
// user, specifically, their auth token.
|
2014-06-05 00:25:38 +03:00
|
|
|
data := struct {
|
2014-06-13 02:41:04 +03:00
|
|
|
*model.User
|
2014-06-05 00:25:38 +03:00
|
|
|
Token string `json:"token"`
|
2014-09-29 04:36:24 +03:00
|
|
|
}{user, user.Token}
|
|
|
|
json.NewEncoder(w).Encode(&data)
|
2014-06-05 00:25:38 +03:00
|
|
|
}
|
|
|
|
|
2014-09-29 04:36:24 +03:00
|
|
|
// PutUser accepts a request to update the currently
|
|
|
|
// authenticated User profile.
|
|
|
|
//
|
|
|
|
// PUT /api/user
|
|
|
|
//
|
|
|
|
func PutUser(c web.C, w http.ResponseWriter, r *http.Request) {
|
|
|
|
var ctx = context.FromC(c)
|
|
|
|
var user = ToUser(c)
|
|
|
|
if user == nil {
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
return
|
2014-06-05 00:25:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// unmarshal the repository from the payload
|
|
|
|
defer r.Body.Close()
|
2014-06-13 02:41:04 +03:00
|
|
|
in := model.User{}
|
2014-06-05 00:25:38 +03:00
|
|
|
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
|
2014-09-29 04:36:24 +03:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
2014-06-05 00:25:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// update the user email
|
|
|
|
if len(in.Email) != 0 {
|
2014-09-29 04:36:24 +03:00
|
|
|
user.SetEmail(in.Email)
|
2014-06-05 00:25:38 +03:00
|
|
|
}
|
|
|
|
// update the user full name
|
|
|
|
if len(in.Name) != 0 {
|
2014-09-29 04:36:24 +03:00
|
|
|
user.Name = in.Name
|
2014-06-05 00:25:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// update the database
|
2014-09-29 04:36:24 +03:00
|
|
|
if err := datastore.PutUser(ctx, user); err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
2014-06-05 00:25:38 +03:00
|
|
|
}
|
|
|
|
|
2014-09-29 04:36:24 +03:00
|
|
|
json.NewEncoder(w).Encode(user)
|
2014-06-05 00:25:38 +03:00
|
|
|
}
|
|
|
|
|
2014-09-29 04:36:24 +03:00
|
|
|
// GetRepos accepts a request to get the currently
|
|
|
|
// authenticated user's repository list from the datastore,
|
|
|
|
// encoded and returned in JSON format.
|
|
|
|
//
|
|
|
|
// GET /api/user/repos
|
|
|
|
//
|
|
|
|
func GetUserRepos(c web.C, w http.ResponseWriter, r *http.Request) {
|
|
|
|
var ctx = context.FromC(c)
|
|
|
|
var user = ToUser(c)
|
|
|
|
if user == nil {
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
return
|
2014-06-05 00:25:38 +03:00
|
|
|
}
|
2014-09-29 04:36:24 +03:00
|
|
|
repos, err := datastore.GetRepoList(ctx, user)
|
2014-06-05 00:25:38 +03:00
|
|
|
if err != nil {
|
2014-09-29 04:36:24 +03:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
2014-06-05 00:25:38 +03:00
|
|
|
}
|
2014-09-29 04:36:24 +03:00
|
|
|
json.NewEncoder(w).Encode(&repos)
|
2014-06-05 00:25:38 +03:00
|
|
|
}
|
|
|
|
|
2014-09-29 04:36:24 +03:00
|
|
|
// GetUserFeed accepts a request to get the user's latest
|
|
|
|
// build feed, across all repositories, from the datastore.
|
|
|
|
// The results are encoded and returned in JSON format.
|
|
|
|
//
|
|
|
|
// GET /api/user/feed
|
|
|
|
//
|
|
|
|
func GetUserFeed(c web.C, w http.ResponseWriter, r *http.Request) {
|
|
|
|
var ctx = context.FromC(c)
|
|
|
|
var user = ToUser(c)
|
|
|
|
if user == nil {
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
return
|
2014-06-05 00:25:38 +03:00
|
|
|
}
|
2014-09-29 04:36:24 +03:00
|
|
|
repos, err := datastore.GetCommitListUser(ctx, user)
|
2014-06-05 00:25:38 +03:00
|
|
|
if err != nil {
|
2014-09-29 04:36:24 +03:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
2014-06-05 00:25:38 +03:00
|
|
|
}
|
2014-09-29 04:36:24 +03:00
|
|
|
json.NewEncoder(w).Encode(&repos)
|
2014-06-05 00:25:38 +03:00
|
|
|
}
|
2014-10-28 17:16:50 +02:00
|
|
|
|
|
|
|
// PostUserSync accepts a request to post user sync
|
|
|
|
//
|
|
|
|
// POST /api/user/sync
|
|
|
|
//
|
|
|
|
func PostUserSync(c web.C, w http.ResponseWriter, r *http.Request) {
|
|
|
|
var ctx = context.FromC(c)
|
|
|
|
var user = ToUser(c)
|
|
|
|
if user == nil {
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var remote = remote.Lookup(user.Remote)
|
|
|
|
if remote == nil {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if user.Syncing {
|
|
|
|
w.WriteHeader(http.StatusConflict)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user.Syncing = true
|
|
|
|
if err := datastore.PutUser(ctx, user); err != nil {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
go sync.SyncUser(ctx, user, remote)
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
return
|
|
|
|
}
|