add migrate api

This commit is contained in:
lunnyxiao 2014-08-29 11:24:37 +08:00
parent 81287ba022
commit c40e815763
2 changed files with 87 additions and 0 deletions

View File

@ -127,6 +127,7 @@ func SetEngine() (err error) {
x.ShowSQL = true
x.ShowDebug = true
x.ShowErr = true
x.ShowWarn = true
return nil
}

View File

@ -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(),
})
}