]> source.dussan.org Git - gitea.git/commitdiff
Add migrate repo API
authorUnknwon <joe2010xtmf@163.com>
Fri, 29 Aug 2014 09:31:53 +0000 (17:31 +0800)
committerUnknwon <joe2010xtmf@163.com>
Fri, 29 Aug 2014 09:31:53 +0000 (17:31 +0800)
cmd/web.go
routers/api/v1/repos.go
routers/repo/repo.go

index 8e471eb61f6d5efbc4fee9329bf8150ea5f2de06..de1aa03cc209e02b1503602490c20332485da447 100644 (file)
@@ -138,10 +138,15 @@ func runWeb(*cli.Context) {
                        r.Post("/markdown/raw", v1.MarkdownRaw)
 
                        // Users.
-                       r.Get("/users/search", v1.SearchUsers)
+                       m.Group("/users", func(r *macaron.Router) {
+                               r.Get("/search", v1.SearchUsers)
+                       })
 
                        // Repositories.
-                       r.Get("/repos/search", v1.SearchRepos)
+                       m.Group("/repos", func(r *macaron.Router) {
+                               r.Get("/search", v1.SearchRepos)
+                               r.Post("/migrate", bindIgnErr(auth.MigrateRepoForm{}), v1.Migrate)
+                       })
 
                        r.Any("/*", func(ctx *middleware.Context) {
                                ctx.JSON(404, &base.ApiJsonErr{"Not Found", v1.DOC_URL})
index e343cfce7c09fc24fd6ede074593682be5fbfaf7..37a3e47a63093d5404395b8245f3606f01a20c5b 100644 (file)
@@ -61,42 +61,50 @@ func SearchRepos(ctx *middleware.Context) {
 }
 
 func Migrate(ctx *middleware.Context, form auth.MigrateRepoForm) {
-       ctxUser := ctx.User
+       u, err := models.GetUserByName(ctx.Query("username"))
+       if err != nil {
+               ctx.JSON(500, map[string]interface{}{
+                       "ok":    false,
+                       "error": err.Error(),
+               })
+               return
+       }
+       if !u.ValidtePassword(ctx.Query("password")) {
+               ctx.JSON(500, map[string]interface{}{
+                       "ok":    false,
+                       "error": "username or password is not correct",
+               })
+               return
+       }
+
+       ctxUser := u
        // Not equal means current user is an organization.
-       if form.Uid != ctx.User.Id {
+       if form.Uid != u.Id {
                org, err := models.GetUserById(form.Uid)
-               if err != nil && err != models.ErrUserNotExist {
+               if err != nil {
                        ctx.JSON(500, map[string]interface{}{
-                               "ok":   false,
-                               "data": err.Error(),
+                               "ok":    false,
+                               "error": 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(),
+                       "ok":    false,
+                       "error": ctx.GetErrMsg(),
                })
                return
        }
 
        if ctxUser.IsOrganization() {
                // Check ownership of organization.
-               if !ctxUser.IsOrgOwner(ctx.User.Id) {
+               if !ctxUser.IsOrgOwner(u.Id) {
                        ctx.JSON(403, map[string]interface{}{
-                               "ok":   false,
-                               "data": "Not allowed",
+                               "ok":    false,
+                               "error": "given user is not owner of organization",
                        })
                        return
                }
@@ -109,23 +117,9 @@ func Migrate(ctx *middleware.Context, form auth.MigrateRepoForm) {
                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(),
+               ctx.JSON(200, map[string]interface{}{
+                       "ok":   true,
+                       "data": "/" + ctxUser.Name + "/" + form.RepoName,
                })
                return
        }
@@ -137,7 +131,7 @@ func Migrate(ctx *middleware.Context, form auth.MigrateRepoForm) {
        }
 
        ctx.JSON(500, map[string]interface{}{
-               "ok":   false,
-               "data": err.Error(),
+               "ok":    false,
+               "error": err.Error(),
        })
 }
index bbc7cf850fa8c6c850a5f6631720c86274acd92e..17f20a0a71866d3e775665760615737bb8ce0413 100644 (file)
@@ -145,7 +145,7 @@ func MigratePost(ctx *middleware.Context, form auth.MigrateRepoForm) {
        // 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 {
+               if err != nil {
                        ctx.Handle(500, "GetUserById", err)
                        return
                }