2016-05-02 21:21:25 +02:00
|
|
|
package server
|
2015-09-30 03:21:17 +02:00
|
|
|
|
|
|
|
import (
|
2016-05-03 02:47:58 +02:00
|
|
|
"encoding/base32"
|
2015-09-30 03:21:17 +02:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2016-05-03 02:47:58 +02:00
|
|
|
"github.com/gorilla/securecookie"
|
2015-09-30 03:21:17 +02:00
|
|
|
|
2016-03-05 07:15:50 +02:00
|
|
|
"github.com/drone/drone/cache"
|
2017-04-12 14:12:21 +02:00
|
|
|
"github.com/drone/drone/model"
|
2015-10-22 01:14:02 +02:00
|
|
|
"github.com/drone/drone/remote"
|
2015-09-30 03:21:17 +02:00
|
|
|
"github.com/drone/drone/router/middleware/session"
|
|
|
|
"github.com/drone/drone/shared/httputil"
|
|
|
|
"github.com/drone/drone/shared/token"
|
2015-10-22 01:14:02 +02:00
|
|
|
"github.com/drone/drone/store"
|
2015-09-30 03:21:17 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func PostRepo(c *gin.Context) {
|
2015-10-22 01:14:02 +02:00
|
|
|
remote := remote.FromContext(c)
|
2015-09-30 03:21:17 +02:00
|
|
|
user := session.User(c)
|
|
|
|
owner := c.Param("owner")
|
|
|
|
name := c.Param("name")
|
|
|
|
|
|
|
|
if user == nil {
|
|
|
|
c.AbortWithStatus(403)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := remote.Repo(user, owner, name)
|
|
|
|
if err != nil {
|
|
|
|
c.String(404, err.Error())
|
|
|
|
return
|
|
|
|
}
|
2016-03-05 07:15:50 +02:00
|
|
|
m, err := cache.GetPerms(c, user, owner, name)
|
2015-09-30 03:21:17 +02:00
|
|
|
if err != nil {
|
|
|
|
c.String(404, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !m.Admin {
|
|
|
|
c.String(403, "Administrative access is required.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// error if the repository already exists
|
2015-10-22 01:14:02 +02:00
|
|
|
_, err = store.GetRepoOwnerName(c, owner, name)
|
2015-09-30 03:21:17 +02:00
|
|
|
if err == nil {
|
|
|
|
c.String(409, "Repository already exists.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the repository owner to the
|
|
|
|
// currently authenticated user.
|
|
|
|
r.UserID = user.ID
|
|
|
|
r.AllowPush = true
|
|
|
|
r.AllowPull = true
|
2017-03-19 10:44:57 +02:00
|
|
|
r.Config = ".drone.yml"
|
2015-09-30 03:21:17 +02:00
|
|
|
r.Timeout = 60 // 1 hour default build time
|
2016-05-03 02:47:58 +02:00
|
|
|
r.Hash = base32.StdEncoding.EncodeToString(
|
|
|
|
securecookie.GenerateRandomKey(32),
|
|
|
|
)
|
2015-09-30 03:21:17 +02:00
|
|
|
|
|
|
|
// crates the jwt token used to verify the repository
|
|
|
|
t := token.New(token.HookToken, r.FullName)
|
|
|
|
sig, err := t.Sign(r.Hash)
|
|
|
|
if err != nil {
|
2015-10-29 22:47:46 +02:00
|
|
|
c.String(500, err.Error())
|
2015-09-30 03:21:17 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
link := fmt.Sprintf(
|
|
|
|
"%s/hook?access_token=%s",
|
|
|
|
httputil.GetURL(c.Request),
|
|
|
|
sig,
|
|
|
|
)
|
|
|
|
|
|
|
|
// activate the repository before we make any
|
|
|
|
// local changes to the database.
|
2016-05-02 01:30:00 +02:00
|
|
|
err = remote.Activate(user, r, link)
|
2015-09-30 03:21:17 +02:00
|
|
|
if err != nil {
|
2015-10-29 22:47:46 +02:00
|
|
|
c.String(500, err.Error())
|
2015-09-30 03:21:17 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// persist the repository
|
2015-10-22 01:14:02 +02:00
|
|
|
err = store.CreateRepo(c, r)
|
2015-09-30 03:21:17 +02:00
|
|
|
if err != nil {
|
2015-10-29 22:47:46 +02:00
|
|
|
c.String(500, err.Error())
|
2015-09-30 03:21:17 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(200, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func PatchRepo(c *gin.Context) {
|
|
|
|
repo := session.Repo(c)
|
|
|
|
user := session.User(c)
|
|
|
|
|
2017-04-12 14:12:21 +02:00
|
|
|
in := new(model.RepoPatch)
|
2015-09-30 03:21:17 +02:00
|
|
|
if err := c.Bind(in); err != nil {
|
|
|
|
c.AbortWithError(http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-11 23:51:33 +02:00
|
|
|
if (in.IsTrusted != nil || in.Timeout != nil) && !user.Admin {
|
2017-04-11 19:06:45 +02:00
|
|
|
c.String(403, "Insufficient privileges")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-30 03:21:17 +02:00
|
|
|
if in.AllowPush != nil {
|
|
|
|
repo.AllowPush = *in.AllowPush
|
|
|
|
}
|
|
|
|
if in.AllowPull != nil {
|
|
|
|
repo.AllowPull = *in.AllowPull
|
|
|
|
}
|
|
|
|
if in.AllowDeploy != nil {
|
|
|
|
repo.AllowDeploy = *in.AllowDeploy
|
|
|
|
}
|
|
|
|
if in.AllowTag != nil {
|
|
|
|
repo.AllowTag = *in.AllowTag
|
|
|
|
}
|
2017-04-11 19:06:45 +02:00
|
|
|
if in.IsGated != nil {
|
|
|
|
repo.IsGated = *in.IsGated
|
|
|
|
}
|
|
|
|
if in.IsTrusted != nil {
|
2015-09-30 03:21:17 +02:00
|
|
|
repo.IsTrusted = *in.IsTrusted
|
|
|
|
}
|
2017-04-11 19:06:45 +02:00
|
|
|
if in.Timeout != nil {
|
2015-09-30 03:21:17 +02:00
|
|
|
repo.Timeout = *in.Timeout
|
|
|
|
}
|
2017-04-12 14:12:21 +02:00
|
|
|
if in.Config != nil {
|
|
|
|
repo.Config = *in.Config
|
|
|
|
}
|
2015-09-30 03:21:17 +02:00
|
|
|
|
2015-10-22 01:14:02 +02:00
|
|
|
err := store.UpdateRepo(c, repo)
|
2015-09-30 03:21:17 +02:00
|
|
|
if err != nil {
|
|
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-30 22:15:28 +02:00
|
|
|
c.JSON(http.StatusOK, repo)
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
|
|
|
|
2016-06-14 23:05:53 +02:00
|
|
|
func ChownRepo(c *gin.Context) {
|
|
|
|
repo := session.Repo(c)
|
|
|
|
user := session.User(c)
|
|
|
|
repo.UserID = user.ID
|
|
|
|
|
|
|
|
err := store.UpdateRepo(c, repo)
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, repo)
|
|
|
|
}
|
|
|
|
|
2015-09-30 03:21:17 +02:00
|
|
|
func GetRepo(c *gin.Context) {
|
2016-03-30 22:15:28 +02:00
|
|
|
c.JSON(http.StatusOK, session.Repo(c))
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteRepo(c *gin.Context) {
|
2015-10-22 01:14:02 +02:00
|
|
|
remote := remote.FromContext(c)
|
2015-09-30 03:21:17 +02:00
|
|
|
repo := session.Repo(c)
|
2015-10-05 03:34:06 +02:00
|
|
|
user := session.User(c)
|
2015-09-30 03:21:17 +02:00
|
|
|
|
2015-10-22 01:14:02 +02:00
|
|
|
err := store.DeleteRepo(c, repo)
|
2015-09-30 03:21:17 +02:00
|
|
|
if err != nil {
|
|
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
2015-10-05 03:34:06 +02:00
|
|
|
return
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
2015-10-05 03:34:06 +02:00
|
|
|
|
|
|
|
remote.Deactivate(user, repo, httputil.GetURL(c.Request))
|
|
|
|
c.Writer.WriteHeader(http.StatusOK)
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
2017-04-12 15:32:44 +02:00
|
|
|
|
|
|
|
func RepairRepo(c *gin.Context) {
|
|
|
|
remote := remote.FromContext(c)
|
|
|
|
repo := session.Repo(c)
|
|
|
|
user := session.User(c)
|
|
|
|
|
|
|
|
// crates the jwt token used to verify the repository
|
|
|
|
t := token.New(token.HookToken, repo.FullName)
|
|
|
|
sig, err := t.Sign(repo.Hash)
|
|
|
|
if err != nil {
|
|
|
|
c.String(500, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// reconstruct the link
|
|
|
|
host := httputil.GetURL(c.Request)
|
|
|
|
link := fmt.Sprintf(
|
|
|
|
"%s/hook?access_token=%s",
|
|
|
|
host,
|
|
|
|
sig,
|
|
|
|
)
|
|
|
|
|
|
|
|
remote.Deactivate(user, repo, host)
|
|
|
|
err = remote.Activate(user, repo, link)
|
|
|
|
if err != nil {
|
|
|
|
c.String(500, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Writer.WriteHeader(http.StatusOK)
|
|
|
|
}
|