From cee90e93aac627fcfd61cc2d660be42a75205e13 Mon Sep 17 00:00:00 2001 From: Joachim Hill-Grannec Date: Mon, 21 Aug 2017 17:56:37 -0400 Subject: [PATCH] Update to create move endpoint to allow changing the repo name --- model/repo.go | 2 -- router/router.go | 1 + server/repo.go | 71 ++++++++++++++++++++++++++++++++++++------------ 3 files changed, 54 insertions(+), 20 deletions(-) diff --git a/model/repo.go b/model/repo.go index ca6fd0177..094969625 100644 --- a/model/repo.go +++ b/model/repo.go @@ -67,6 +67,4 @@ type RepoPatch struct { AllowDeploy *bool `json:"allow_deploy,omitempty"` AllowTag *bool `json:"allow_tag,omitempty"` BuildCounter *int `json:"build_counter,omitempty"` - Name *string `json:"name,omitempty"` - Owner *string `json:"owner,omitempty"` } diff --git a/router/router.go b/router/router.go index a05b65beb..2503300d7 100644 --- a/router/router.go +++ b/router/router.go @@ -104,6 +104,7 @@ func Load(mux *httptreemux.ContextMux, middleware ...gin.HandlerFunc) http.Handl repo.DELETE("", session.MustRepoAdmin(), server.DeleteRepo) repo.POST("/chown", session.MustRepoAdmin(), server.ChownRepo) repo.POST("/repair", session.MustRepoAdmin(), server.RepairRepo) + repo.POST("/move", session.MustRepoAdmin(), server.MoveRepo) repo.POST("/builds/:number", session.MustPush, server.PostBuild) repo.DELETE("/builds/:number", session.MustAdmin(), server.ZombieKill) diff --git a/server/repo.go b/server/repo.go index 38b15c443..ba00a54b5 100644 --- a/server/repo.go +++ b/server/repo.go @@ -15,6 +15,7 @@ import ( "github.com/drone/drone/shared/httputil" "github.com/drone/drone/shared/token" "github.com/drone/drone/store" + "strings" ) func PostRepo(c *gin.Context) { @@ -88,7 +89,6 @@ func PostRepo(c *gin.Context) { func PatchRepo(c *gin.Context) { repo := session.Repo(c) user := session.User(c) - remote := remote.FromContext(c) in := new(model.RepoPatch) if err := c.Bind(in); err != nil { @@ -96,7 +96,7 @@ func PatchRepo(c *gin.Context) { return } - if (in.IsTrusted != nil || in.Timeout != nil || in.BuildCounter != nil || in.Owner != nil || in.Name != nil) && !user.Admin { + if (in.IsTrusted != nil || in.Timeout != nil || in.BuildCounter != nil) && !user.Admin { c.String(403, "Insufficient privileges") return } @@ -138,21 +138,6 @@ func PatchRepo(c *gin.Context) { repo.Counter = *in.BuildCounter } - if in.Name != nil && in.Owner != nil { - from, err := remote.Repo(user, *in.Owner, *in.Name) - if err != nil { - c.AbortWithError(http.StatusInternalServerError, err) - } - 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 - repo.Visibility = from.Visibility - } - err := store.UpdateRepo(c, repo) if err != nil { c.AbortWithError(http.StatusInternalServerError, err) @@ -211,7 +196,7 @@ func RepairRepo(c *gin.Context) { repo := session.Repo(c) user := session.User(c) - // crates the jwt token used to verify the repository + // 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 { @@ -235,3 +220,53 @@ func RepairRepo(c *gin.Context) { } 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) + } + + owner, name, errParse := ParseRepo(to) + if errParse != nil { + c.AbortWithError(http.StatusInternalServerError, errParse) + } + + from, err := remote.Repo(user, owner, name) + if err != nil { + c.AbortWithError(http.StatusInternalServerError, err) + } + 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 + repo.Visibility = from.Visibility + + errStore := store.UpdateRepo(c, repo) + if errStore != nil { + c.AbortWithError(http.StatusInternalServerError, errStore) + return + } + + RepairRepo(c) +} + +// ParseRepo parses the repository owner and name from a string. +func ParseRepo(str string) (user, repo string, err error) { + var parts = strings.Split(str, "/") + if len(parts) != 2 { + err = fmt.Errorf("Error: Invalid or missing repository. eg octocat/hello-world.") + return + } + user = parts[0] + repo = parts[1] + return +}