From c40e815763f8360b892cc5bbdd09725c1df6902f Mon Sep 17 00:00:00 2001 From: lunnyxiao Date: Fri, 29 Aug 2014 11:24:37 +0800 Subject: [PATCH] add migrate api --- models/models.go | 1 + routers/api/v1/repos.go | 86 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) diff --git a/models/models.go b/models/models.go index bf2586063b..186ddf41aa 100644 --- a/models/models.go +++ b/models/models.go @@ -127,6 +127,7 @@ func SetEngine() (err error) { x.ShowSQL = true x.ShowDebug = true x.ShowErr = true + x.ShowWarn = true return nil } diff --git a/routers/api/v1/repos.go b/routers/api/v1/repos.go index 67cf9e654c..e343cfce7c 100644 --- a/routers/api/v1/repos.go +++ b/routers/api/v1/repos.go @@ -5,11 +5,15 @@ package v1 import ( + "fmt" "path" + "strings" "github.com/Unknwon/com" "github.com/gogits/gogs/models" + "github.com/gogits/gogs/modules/auth" + "github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/middleware" ) @@ -55,3 +59,85 @@ func SearchRepos(ctx *middleware.Context) { "data": results, }) } + +func Migrate(ctx *middleware.Context, form auth.MigrateRepoForm) { + ctxUser := ctx.User + // Not equal means current user is an organization. + if form.Uid != ctx.User.Id { + org, err := models.GetUserById(form.Uid) + if err != nil && err != models.ErrUserNotExist { + ctx.JSON(500, map[string]interface{}{ + "ok": false, + "data": err.Error(), + }) + return + } + ctxUser = org + } + + if err := ctx.User.GetOrganizations(); err != nil { + ctx.JSON(500, map[string]interface{}{ + "ok": false, + "data": err.Error(), + }) + return + } + + if ctx.HasError() { + ctx.JSON(500, map[string]interface{}{ + "ok": false, + "data": ctx.GetErrMsg(), + }) + return + } + + if ctxUser.IsOrganization() { + // Check ownership of organization. + if !ctxUser.IsOrgOwner(ctx.User.Id) { + ctx.JSON(403, map[string]interface{}{ + "ok": false, + "data": "Not allowed", + }) + return + } + } + + authStr := strings.Replace(fmt.Sprintf("://%s:%s", + form.AuthUserName, form.AuthPasswd), "@", "%40", -1) + url := strings.Replace(form.HttpsUrl, "://", authStr+"@", 1) + repo, err := models.MigrateRepository(ctxUser, form.RepoName, form.Description, form.Private, + form.Mirror, url) + if err == nil { + log.Trace("Repository migrated: %s/%s", ctxUser.Name, form.RepoName) + ctx.JSON(200, + map[string]interface{}{ + "ok": true, + "data": "/" + ctxUser.Name + "/" + form.RepoName, + }) + return + } else if err == models.ErrRepoAlreadyExist { + ctx.JSON(500, + map[string]interface{}{ + "ok": false, + "data": err.Error(), + }) + return + } else if err == models.ErrRepoNameIllegal { + ctx.JSON(500, map[string]interface{}{ + "ok": false, + "data": err.Error(), + }) + return + } + + if repo != nil { + if errDelete := models.DeleteRepository(ctxUser.Id, repo.Id, ctxUser.Name); errDelete != nil { + log.Error(4, "DeleteRepository: %v", errDelete) + } + } + + ctx.JSON(500, map[string]interface{}{ + "ok": false, + "data": err.Error(), + }) +}