harness-drone/server/repo.go

313 lines
6.4 KiB
Go
Raw Normal View History

2016-05-02 19:21:25 +00:00
package server
2015-09-30 01:21:17 +00:00
import (
"encoding/base32"
2015-09-30 01:21:17 +00:00
"fmt"
"net/http"
2017-07-14 19:58:38 +00:00
"strconv"
2015-09-30 01:21:17 +00:00
"github.com/gin-gonic/gin"
"github.com/gorilla/securecookie"
2015-09-30 01:21:17 +00:00
"github.com/drone/drone/model"
"github.com/drone/drone/remote"
2015-09-30 01:21:17 +00:00
"github.com/drone/drone/router/middleware/session"
"github.com/drone/drone/shared/httputil"
"github.com/drone/drone/shared/token"
"github.com/drone/drone/store"
2015-09-30 01:21:17 +00:00
)
func PostRepo(c *gin.Context) {
remote := remote.FromContext(c)
2015-09-30 01:21:17 +00:00
user := session.User(c)
2017-07-14 19:58:38 +00:00
repo := session.Repo(c)
2015-09-30 01:21:17 +00:00
2017-07-14 19:58:38 +00:00
if repo.IsActive {
c.String(409, "Repository is already active.")
2015-09-30 01:21:17 +00:00
return
}
if err := Config.Services.Limiter.LimitRepo(user, repo); err != nil {
c.String(403, "Repository activation blocked by limiter")
return
}
2017-07-14 19:58:38 +00:00
repo.IsActive = true
repo.UserID = user.ID
if !repo.AllowPush && !repo.AllowPull && !repo.AllowDeploy && !repo.AllowTag {
repo.AllowPush = true
repo.AllowPull = true
}
if repo.Visibility == "" {
repo.Visibility = model.VisibilityPublic
if repo.IsPrivate {
repo.Visibility = model.VisibilityPrivate
}
2015-09-30 01:21:17 +00:00
}
2017-07-14 19:58:38 +00:00
if repo.Config == "" {
repo.Config = Config.Server.RepoConfig
2015-09-30 01:21:17 +00:00
}
2017-07-14 19:58:38 +00:00
if repo.Timeout == 0 {
repo.Timeout = 60 // 1 hour default build time
2015-09-30 01:21:17 +00:00
}
2017-07-14 19:58:38 +00:00
if repo.Hash == "" {
repo.Hash = base32.StdEncoding.EncodeToString(
securecookie.GenerateRandomKey(32),
)
2015-09-30 01:21:17 +00:00
}
2017-07-14 19:58:38 +00:00
// creates the jwt token used to verify the repository
t := token.New(token.HookToken, repo.FullName)
sig, err := t.Sign(repo.Hash)
2015-09-30 01:21:17 +00:00
if err != nil {
2015-10-29 20:47:46 +00:00
c.String(500, err.Error())
2015-09-30 01:21:17 +00:00
return
}
link := fmt.Sprintf(
"%s/hook?access_token=%s",
httputil.GetURL(c.Request),
sig,
)
2017-07-14 19:58:38 +00:00
err = remote.Activate(user, repo, link)
2015-09-30 01:21:17 +00:00
if err != nil {
2015-10-29 20:47:46 +00:00
c.String(500, err.Error())
2015-09-30 01:21:17 +00:00
return
}
2017-07-21 15:53:11 +00:00
from, err := remote.Repo(user, repo.Owner, repo.Name)
if err == nil {
repo.Update(from)
}
2017-07-14 19:58:38 +00:00
err = store.UpdateRepo(c, repo)
2015-09-30 01:21:17 +00:00
if err != nil {
2015-10-29 20:47:46 +00:00
c.String(500, err.Error())
2015-09-30 01:21:17 +00:00
return
}
2017-07-14 19:58:38 +00:00
c.JSON(200, repo)
2015-09-30 01:21:17 +00:00
}
func PatchRepo(c *gin.Context) {
repo := session.Repo(c)
user := session.User(c)
in := new(model.RepoPatch)
2015-09-30 01:21:17 +00:00
if err := c.Bind(in); err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
if (in.IsTrusted != nil || in.Timeout != nil || in.BuildCounter != nil) && !user.Admin {
c.String(403, "Insufficient privileges")
return
}
2015-09-30 01:21:17 +00: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
}
if in.IsGated != nil {
repo.IsGated = *in.IsGated
}
if in.IsTrusted != nil {
2015-09-30 01:21:17 +00:00
repo.IsTrusted = *in.IsTrusted
}
if in.Timeout != nil {
2015-09-30 01:21:17 +00:00
repo.Timeout = *in.Timeout
}
if in.Config != nil {
repo.Config = *in.Config
}
2017-05-22 22:44:58 +00:00
if in.Visibility != nil {
switch *in.Visibility {
2017-05-22 22:54:04 +00:00
case model.VisibilityInternal, model.VisibilityPrivate, model.VisibilityPublic:
repo.Visibility = *in.Visibility
2017-05-22 22:44:58 +00:00
default:
c.String(400, "Invalid visibility type")
return
}
}
if in.BuildCounter != nil {
repo.Counter = *in.BuildCounter
}
2015-09-30 01:21:17 +00:00
err := store.UpdateRepo(c, repo)
2015-09-30 01:21:17 +00:00
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
c.JSON(http.StatusOK, repo)
2015-09-30 01:21:17 +00:00
}
2016-06-14 21:05:53 +00: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 01:21:17 +00:00
func GetRepo(c *gin.Context) {
c.JSON(http.StatusOK, session.Repo(c))
2015-09-30 01:21:17 +00:00
}
func DeleteRepo(c *gin.Context) {
2017-07-14 19:58:38 +00:00
remove, _ := strconv.ParseBool(c.Query("remove"))
remote := remote.FromContext(c)
2015-09-30 01:21:17 +00:00
repo := session.Repo(c)
2015-10-05 01:34:06 +00:00
user := session.User(c)
2015-09-30 01:21:17 +00:00
2017-07-14 19:58:38 +00:00
repo.IsActive = false
repo.UserID = 0
err := store.UpdateRepo(c, repo)
2015-09-30 01:21:17 +00:00
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
2015-10-05 01:34:06 +00:00
return
2015-09-30 01:21:17 +00:00
}
2015-10-05 01:34:06 +00:00
2017-07-14 19:58:38 +00:00
if remove {
err := store.DeleteRepo(c, repo)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
}
2015-10-05 01:34:06 +00:00
remote.Deactivate(user, repo, httputil.GetURL(c.Request))
2017-08-25 00:03:11 +00:00
c.JSON(200, repo)
2015-09-30 01:21:17 +00:00
}
2017-04-12 13:32:44 +00:00
func RepairRepo(c *gin.Context) {
remote := remote.FromContext(c)
repo := session.Repo(c)
user := session.User(c)
// creates the jwt token used to verify the repository
2017-04-12 13:32:44 +00:00
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
}
from, err := remote.Repo(user, repo.Owner, repo.Name)
if err == nil {
repo.Name = from.Name
repo.Owner = from.Owner
repo.FullName = from.FullName
repo.Avatar = from.Avatar
repo.Link = from.Link
repo.Clone = from.Clone
repo.IsPrivate = from.IsPrivate
if repo.IsPrivate != from.IsPrivate {
repo.ResetVisibility()
}
store.UpdateRepo(c, repo)
}
2017-04-12 13:32:44 +00:00
c.Writer.WriteHeader(http.StatusOK)
}
func MoveRepo(c *gin.Context) {
remote := remote.FromContext(c)
repo := session.Repo(c)
user := session.User(c)
to, exists := c.GetQuery("to")
if !exists {
err := fmt.Errorf("Missing required to query value")
c.AbortWithError(http.StatusInternalServerError, err)
return
}
owner, name, errParse := model.ParseRepo(to)
if errParse != nil {
c.AbortWithError(http.StatusInternalServerError, errParse)
return
}
from, err := remote.Repo(user, owner, name)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
if !from.Perm.Admin {
c.AbortWithStatus(http.StatusUnauthorized)
return
}
repo.Name = from.Name
repo.Owner = from.Owner
repo.FullName = from.FullName
repo.Avatar = from.Avatar
repo.Link = from.Link
repo.Clone = from.Clone
repo.IsPrivate = from.IsPrivate
if repo.IsPrivate != from.IsPrivate {
repo.ResetVisibility()
}
errStore := store.UpdateRepo(c, repo)
if errStore != nil {
c.AbortWithError(http.StatusInternalServerError, errStore)
return
}
// creates 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)
}