2014-06-04 21:25:38 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2015-02-12 07:41:04 +00:00
|
|
|
"log"
|
2014-06-04 21:25:38 +00:00
|
|
|
"net/http"
|
|
|
|
|
2014-07-13 02:01:58 +00:00
|
|
|
"github.com/drone/drone/plugin/remote"
|
2014-09-29 01:36:24 +00:00
|
|
|
"github.com/drone/drone/server/datastore"
|
2014-06-12 22:28:05 +00:00
|
|
|
"github.com/drone/drone/shared/httputil"
|
2014-07-10 00:26:21 +00:00
|
|
|
"github.com/drone/drone/shared/model"
|
2014-06-12 22:28:05 +00:00
|
|
|
"github.com/drone/drone/shared/sshutil"
|
2014-09-29 01:36:24 +00:00
|
|
|
"github.com/goji/context"
|
|
|
|
"github.com/zenazn/goji/web"
|
2014-06-04 21:25:38 +00:00
|
|
|
)
|
|
|
|
|
2014-09-29 01:36:24 +00:00
|
|
|
// GetRepo accepts a request to retrieve a commit
|
|
|
|
// from the datastore for the given repository, branch and
|
|
|
|
// commit hash.
|
|
|
|
//
|
|
|
|
// GET /api/repos/:host/:owner/:name
|
|
|
|
//
|
|
|
|
func GetRepo(c web.C, w http.ResponseWriter, r *http.Request) {
|
|
|
|
var (
|
2014-10-09 04:28:06 +00:00
|
|
|
role = ToRole(c)
|
|
|
|
repo = ToRepo(c)
|
2014-09-29 01:36:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// if the user is not requesting (or cannot access)
|
|
|
|
// admin data then we just return the repo as-is
|
2014-10-09 04:28:06 +00:00
|
|
|
if role.Admin == false {
|
2015-02-25 14:38:22 +00:00
|
|
|
json.NewEncoder(w).Encode(struct {
|
|
|
|
*model.Repo
|
2015-02-26 08:22:06 +00:00
|
|
|
Perm *model.Perm `json:"role"`
|
2015-02-25 14:38:22 +00:00
|
|
|
}{repo, role})
|
2014-09-29 01:36:24 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// else we should return restricted fields
|
|
|
|
json.NewEncoder(w).Encode(struct {
|
2014-07-10 00:26:21 +00:00
|
|
|
*model.Repo
|
2014-10-09 04:28:06 +00:00
|
|
|
PublicKey string `json:"public_key"`
|
|
|
|
Params string `json:"params"`
|
|
|
|
Perm *model.Perm `json:"role"`
|
|
|
|
}{repo, repo.PublicKey, repo.Params, role})
|
2014-06-04 21:25:38 +00:00
|
|
|
}
|
|
|
|
|
2015-02-05 14:27:26 +00:00
|
|
|
// DelRepo accepts a request to delete the named
|
|
|
|
// repository.
|
2014-09-29 01:36:24 +00:00
|
|
|
//
|
|
|
|
// DEL /api/repos/:host/:owner/:name
|
|
|
|
//
|
|
|
|
func DelRepo(c web.C, w http.ResponseWriter, r *http.Request) {
|
|
|
|
var ctx = context.FromC(c)
|
|
|
|
var repo = ToRepo(c)
|
|
|
|
|
2015-02-04 13:42:24 +00:00
|
|
|
// completely remove the repository from the database
|
2015-02-05 14:27:26 +00:00
|
|
|
var user = ToUser(c)
|
|
|
|
var remote = remote.Lookup(repo.Host)
|
|
|
|
if remote == nil {
|
2015-02-12 07:41:04 +00:00
|
|
|
log.Printf("[ERROR] no remote for host '%s' found", repo.Host)
|
2015-02-05 14:27:26 +00:00
|
|
|
} else {
|
|
|
|
// Request a new token and update
|
|
|
|
user_token, err := remote.GetToken(user)
|
|
|
|
if err != nil {
|
2015-02-12 07:41:04 +00:00
|
|
|
log.Printf("[ERROR] no token for user '%s' on remote '%s' ", user.Email, repo.Host)
|
2015-02-05 12:13:17 +00:00
|
|
|
} else {
|
2015-02-05 14:27:26 +00:00
|
|
|
if user_token != nil {
|
|
|
|
user.Access = user_token.AccessToken
|
|
|
|
user.Secret = user_token.RefreshToken
|
|
|
|
user.TokenExpiry = user_token.Expiry
|
|
|
|
datastore.PutUser(ctx, user)
|
|
|
|
}
|
|
|
|
// setup the post-commit hook with the remote system and
|
|
|
|
// and deactiveate this hook/user on the remote system
|
|
|
|
var hook = fmt.Sprintf("%s/api/hook/%s/%s", httputil.GetURL(r), repo.Remote, repo.Token)
|
|
|
|
if err := remote.Deactivate(user, repo, hook); err != nil {
|
2015-02-12 07:41:04 +00:00
|
|
|
log.Printf("[ERROR] deactivate on remote '%s' failed: %s", repo.Host, err)
|
2015-02-05 12:13:17 +00:00
|
|
|
}
|
2015-02-04 13:42:24 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-05 14:27:26 +00:00
|
|
|
// fail through: if any of the actions on the remote failed
|
|
|
|
// we try to delete the repo in our datastore anyway
|
|
|
|
if err := datastore.DelRepo(ctx, repo); err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-11 06:56:22 +00:00
|
|
|
// DeactivateRepo accepts a request to deactivate the named
|
2015-02-05 14:27:26 +00:00
|
|
|
// repository. This will disable all builds in the system
|
|
|
|
// for this repository.
|
|
|
|
//
|
2015-02-11 06:56:22 +00:00
|
|
|
// POST /api/repos/:host/:owner/:name/deactivate
|
2015-02-05 14:27:26 +00:00
|
|
|
//
|
2015-02-11 06:56:22 +00:00
|
|
|
func DeactivateRepo(c web.C, w http.ResponseWriter, r *http.Request) {
|
2015-02-05 14:27:26 +00:00
|
|
|
var ctx = context.FromC(c)
|
|
|
|
var repo = ToRepo(c)
|
2015-02-04 13:42:24 +00:00
|
|
|
|
2014-09-29 01:36:24 +00:00
|
|
|
// disable everything
|
|
|
|
repo.Active = false
|
|
|
|
repo.PullRequest = false
|
|
|
|
repo.PostCommit = false
|
2014-10-22 08:02:14 +00:00
|
|
|
repo.UserID = 0
|
2014-06-04 21:25:38 +00:00
|
|
|
|
2014-09-29 01:36:24 +00:00
|
|
|
if err := datastore.PutRepo(ctx, repo); err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
2014-06-04 21:25:38 +00:00
|
|
|
}
|
2015-02-11 06:56:22 +00:00
|
|
|
json.NewEncoder(w).Encode(repo)
|
2014-09-29 01:36:24 +00:00
|
|
|
}
|
2014-06-04 21:25:38 +00:00
|
|
|
|
2014-09-29 01:36:24 +00:00
|
|
|
// PostRepo accapets a request to activate the named repository
|
|
|
|
// in the datastore. It returns a 201 status created if successful
|
|
|
|
//
|
|
|
|
// POST /api/repos/:host/:owner/:name
|
|
|
|
//
|
|
|
|
func PostRepo(c web.C, w http.ResponseWriter, r *http.Request) {
|
|
|
|
var ctx = context.FromC(c)
|
|
|
|
var repo = ToRepo(c)
|
|
|
|
var user = ToUser(c)
|
2014-06-04 21:25:38 +00:00
|
|
|
|
|
|
|
// update the repo active flag and fields
|
|
|
|
repo.Active = true
|
|
|
|
repo.PullRequest = true
|
|
|
|
repo.PostCommit = true
|
|
|
|
repo.UserID = user.ID
|
2014-09-06 18:10:00 +00:00
|
|
|
repo.Timeout = 3600 // default to 1 hour
|
2014-10-22 08:02:14 +00:00
|
|
|
|
|
|
|
// generate a secret key for post-commit hooks
|
|
|
|
if len(repo.Token) == 0 {
|
|
|
|
repo.Token = model.GenerateToken()
|
|
|
|
}
|
2014-06-04 21:25:38 +00:00
|
|
|
|
2014-09-29 01:36:24 +00:00
|
|
|
// generates the rsa key
|
2014-10-22 08:02:14 +00:00
|
|
|
if len(repo.PublicKey) == 0 || len(repo.PrivateKey) == 0 {
|
|
|
|
key, err := sshutil.GeneratePrivateKey()
|
|
|
|
if err != nil {
|
2014-11-01 03:00:58 +00:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2014-10-22 08:02:14 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
repo.PublicKey = sshutil.MarshalPublicKey(&key.PublicKey)
|
|
|
|
repo.PrivateKey = sshutil.MarshalPrivateKey(key)
|
2014-06-04 21:25:38 +00:00
|
|
|
}
|
|
|
|
|
2014-09-29 01:36:24 +00:00
|
|
|
var remote = remote.Lookup(repo.Host)
|
2014-09-02 07:18:17 +00:00
|
|
|
if remote == nil {
|
2014-09-29 01:36:24 +00:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
2014-06-04 21:25:38 +00:00
|
|
|
}
|
|
|
|
|
2015-01-26 23:32:42 +00:00
|
|
|
// Request a new token and update
|
|
|
|
user_token, err := remote.GetToken(user)
|
|
|
|
if user_token != nil {
|
|
|
|
user.Access = user_token.AccessToken
|
|
|
|
user.Secret = user_token.RefreshToken
|
2015-01-27 22:42:20 +00:00
|
|
|
user.TokenExpiry = user_token.Expiry
|
2015-01-26 23:32:42 +00:00
|
|
|
datastore.PutUser(ctx, user)
|
|
|
|
} else if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-09-29 01:36:24 +00:00
|
|
|
// setup the post-commit hook with the remote system and
|
|
|
|
// if necessary, register the public key
|
2014-10-22 07:41:25 +00:00
|
|
|
var hook = fmt.Sprintf("%s/api/hook/%s/%s", httputil.GetURL(r), repo.Remote, repo.Token)
|
2014-09-02 07:18:17 +00:00
|
|
|
if err := remote.Activate(user, repo, hook); err != nil {
|
2014-11-01 03:00:58 +00:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2014-09-29 01:36:24 +00:00
|
|
|
return
|
2014-06-04 21:25:38 +00:00
|
|
|
}
|
|
|
|
|
2014-09-29 01:36:24 +00:00
|
|
|
if err := datastore.PutRepo(ctx, repo); err != nil {
|
2014-11-01 03:00:58 +00:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2014-09-29 01:36:24 +00:00
|
|
|
return
|
2014-06-04 21:25:38 +00:00
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
2014-09-29 01:36:24 +00:00
|
|
|
json.NewEncoder(w).Encode(repo)
|
2014-06-04 21:25:38 +00:00
|
|
|
}
|
|
|
|
|
2014-09-29 01:36:24 +00:00
|
|
|
// PutRepo accapets a request to update the named repository
|
|
|
|
// in the datastore. It expects a JSON input and returns the
|
|
|
|
// updated repository in JSON format if successful.
|
|
|
|
//
|
|
|
|
// PUT /api/repos/:host/:owner/:name
|
|
|
|
//
|
|
|
|
func PutRepo(c web.C, w http.ResponseWriter, r *http.Request) {
|
|
|
|
var ctx = context.FromC(c)
|
|
|
|
var repo = ToRepo(c)
|
|
|
|
var user = ToUser(c)
|
2014-06-04 21:25:38 +00:00
|
|
|
|
|
|
|
// unmarshal the repository from the payload
|
|
|
|
defer r.Body.Close()
|
|
|
|
in := struct {
|
|
|
|
PostCommit *bool `json:"post_commits"`
|
|
|
|
PullRequest *bool `json:"pull_requests"`
|
|
|
|
Privileged *bool `json:"privileged"`
|
|
|
|
Params *string `json:"params"`
|
|
|
|
Timeout *int64 `json:"timeout"`
|
2014-09-06 19:45:27 +00:00
|
|
|
PublicKey *string `json:"public_key"`
|
|
|
|
PrivateKey *string `json:"private_key"`
|
2014-06-04 21:25:38 +00:00
|
|
|
}{}
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
|
2014-11-01 03:00:58 +00:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2014-09-29 01:36:24 +00:00
|
|
|
return
|
2014-06-04 21:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if in.Params != nil {
|
|
|
|
repo.Params = *in.Params
|
2015-01-17 06:11:36 +00:00
|
|
|
if _, err := repo.ParamMap(); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2014-06-04 21:25:38 +00:00
|
|
|
}
|
|
|
|
if in.PostCommit != nil {
|
|
|
|
repo.PostCommit = *in.PostCommit
|
|
|
|
}
|
|
|
|
if in.PullRequest != nil {
|
|
|
|
repo.PullRequest = *in.PullRequest
|
|
|
|
}
|
|
|
|
if in.Privileged != nil && user.Admin {
|
|
|
|
repo.Privileged = *in.Privileged
|
|
|
|
}
|
|
|
|
if in.Timeout != nil && user.Admin {
|
|
|
|
repo.Timeout = *in.Timeout
|
|
|
|
}
|
2014-09-06 19:45:27 +00:00
|
|
|
if in.PrivateKey != nil && in.PublicKey != nil {
|
|
|
|
repo.PublicKey = *in.PublicKey
|
|
|
|
repo.PrivateKey = *in.PrivateKey
|
|
|
|
}
|
2014-09-29 01:36:24 +00:00
|
|
|
if err := datastore.PutRepo(ctx, repo); err != nil {
|
2014-11-01 03:00:58 +00:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2014-09-29 01:36:24 +00:00
|
|
|
return
|
2014-06-04 21:25:38 +00:00
|
|
|
}
|
2014-09-29 01:36:24 +00:00
|
|
|
json.NewEncoder(w).Encode(repo)
|
2014-06-04 21:25:38 +00:00
|
|
|
}
|