|
|
@ -5,7 +5,7 @@ |
|
|
|
package v1 |
|
|
|
package v1 |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"fmt" |
|
|
|
"net/url" |
|
|
|
"path" |
|
|
|
"path" |
|
|
|
"strings" |
|
|
|
"strings" |
|
|
|
|
|
|
|
|
|
|
@ -156,17 +156,15 @@ func CreateOrgRepo(ctx *middleware.Context, opt api.CreateRepoOption) { |
|
|
|
func MigrateRepo(ctx *middleware.Context, form auth.MigrateRepoForm) { |
|
|
|
func MigrateRepo(ctx *middleware.Context, form auth.MigrateRepoForm) { |
|
|
|
u, err := models.GetUserByName(ctx.Query("username")) |
|
|
|
u, err := models.GetUserByName(ctx.Query("username")) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
ctx.JSON(500, map[string]interface{}{ |
|
|
|
if err == models.ErrUserNotExist { |
|
|
|
"ok": false, |
|
|
|
ctx.HandleAPI(422, err) |
|
|
|
"error": err.Error(), |
|
|
|
} else { |
|
|
|
}) |
|
|
|
ctx.HandleAPI(500, err) |
|
|
|
|
|
|
|
} |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
if !u.ValidtePassword(ctx.Query("password")) { |
|
|
|
if !u.ValidtePassword(ctx.Query("password")) { |
|
|
|
ctx.JSON(500, map[string]interface{}{ |
|
|
|
ctx.HandleAPI(422, "Username or password is not correct.") |
|
|
|
"ok": false, |
|
|
|
|
|
|
|
"error": "username or password is not correct", |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -175,56 +173,59 @@ func MigrateRepo(ctx *middleware.Context, form auth.MigrateRepoForm) { |
|
|
|
if form.Uid != u.Id { |
|
|
|
if form.Uid != u.Id { |
|
|
|
org, err := models.GetUserById(form.Uid) |
|
|
|
org, err := models.GetUserById(form.Uid) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
log.Error(4, "GetUserById: %v", err) |
|
|
|
if err == models.ErrUserNotExist { |
|
|
|
ctx.Error(500) |
|
|
|
ctx.HandleAPI(422, err) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
ctx.HandleAPI(500, err) |
|
|
|
|
|
|
|
} |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
ctxUser = org |
|
|
|
ctxUser = org |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if ctx.HasError() { |
|
|
|
if ctx.HasError() { |
|
|
|
ctx.JSON(422, map[string]interface{}{ |
|
|
|
ctx.HandleAPI(422, ctx.GetErrMsg()) |
|
|
|
"ok": false, |
|
|
|
|
|
|
|
"error": ctx.GetErrMsg(), |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if ctxUser.IsOrganization() { |
|
|
|
if ctxUser.IsOrganization() { |
|
|
|
// Check ownership of organization.
|
|
|
|
// Check ownership of organization.
|
|
|
|
if !ctxUser.IsOwnedBy(u.Id) { |
|
|
|
if !ctxUser.IsOwnedBy(u.Id) { |
|
|
|
ctx.JSON(403, map[string]interface{}{ |
|
|
|
ctx.HandleAPI(403, "Given user is not owner of organization.") |
|
|
|
"ok": false, |
|
|
|
|
|
|
|
"error": "given user is not owner of organization", |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
authStr := strings.Replace(fmt.Sprintf("://%s:%s", |
|
|
|
// Remote address can be HTTPS URL or local path.
|
|
|
|
form.AuthUserName, form.AuthPasswd), "@", "%40", -1) |
|
|
|
remoteAddr := form.CloneAddr |
|
|
|
url := strings.Replace(form.HttpsUrl, "://", authStr+"@", 1) |
|
|
|
if strings.HasPrefix(form.CloneAddr, "http") { |
|
|
|
repo, err := models.MigrateRepository(ctxUser, form.RepoName, form.Description, form.Private, |
|
|
|
u, err := url.Parse(form.CloneAddr) |
|
|
|
form.Mirror, url) |
|
|
|
if err != nil { |
|
|
|
if err == nil { |
|
|
|
ctx.HandleAPI(422, err) |
|
|
|
log.Trace("Repository migrated: %s/%s", ctxUser.Name, form.RepoName) |
|
|
|
return |
|
|
|
ctx.JSON(200, map[string]interface{}{ |
|
|
|
} |
|
|
|
"ok": true, |
|
|
|
if len(form.AuthUserName) > 0 || len(form.AuthPasswd) > 0 { |
|
|
|
"data": "/" + ctxUser.Name + "/" + form.RepoName, |
|
|
|
u.User = url.UserPassword(form.AuthUserName, form.AuthPasswd) |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
remoteAddr = u.String() |
|
|
|
|
|
|
|
} else if !com.IsDir(remoteAddr) { |
|
|
|
|
|
|
|
ctx.HandleAPI(422, "Invalid local path, it does not exist or not a directory.") |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
repo, err := models.MigrateRepository(ctxUser, form.RepoName, form.Description, form.Private, form.Mirror, remoteAddr) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
if repo != nil { |
|
|
|
if repo != nil { |
|
|
|
if errDelete := models.DeleteRepository(ctxUser.Id, repo.Id, ctxUser.Name); errDelete != nil { |
|
|
|
if errDelete := models.DeleteRepository(ctxUser.Id, repo.Id, ctxUser.Name); errDelete != nil { |
|
|
|
log.Error(4, "DeleteRepository: %v", errDelete) |
|
|
|
log.Error(4, "DeleteRepository: %v", errDelete) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
ctx.HandleAPI(500, err) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
ctx.JSON(500, map[string]interface{}{ |
|
|
|
log.Trace("Repository migrated: %s/%s", ctxUser.Name, form.RepoName) |
|
|
|
"ok": false, |
|
|
|
ctx.WriteHeader(200) |
|
|
|
"error": err.Error(), |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// GET /user/repos
|
|
|
|
// GET /user/repos
|
|
|
|