diff options
Diffstat (limited to 'routers')
-rw-r--r-- | routers/api/v1/repo.go | 2 | ||||
-rw-r--r-- | routers/repo/pull.go | 95 | ||||
-rw-r--r-- | routers/repo/repo.go | 113 | ||||
-rw-r--r-- | routers/repo/setting.go | 4 |
4 files changed, 100 insertions, 114 deletions
diff --git a/routers/api/v1/repo.go b/routers/api/v1/repo.go index d683eac3f9..fdf05c0529 100644 --- a/routers/api/v1/repo.go +++ b/routers/api/v1/repo.go @@ -104,7 +104,7 @@ func createRepo(ctx *middleware.Context, owner *models.User, opt api.CreateRepoO repo, err := models.CreateRepository(owner, opt.Name, opt.Description, opt.Gitignore, opt.License, opt.Private, false, opt.AutoInit) if err != nil { - if err == models.ErrRepoAlreadyExist || + if models.IsErrRepoAlreadyExist(err) || models.IsErrNameReserved(err) || models.IsErrNamePatternNotAllowed(err) { ctx.JSON(422, &base.ApiJsonErr{err.Error(), base.DOC_URL}) diff --git a/routers/repo/pull.go b/routers/repo/pull.go index d379a54eaa..cb5167033c 100644 --- a/routers/repo/pull.go +++ b/routers/repo/pull.go @@ -5,14 +5,109 @@ package repo import ( + "github.com/gogits/gogs/models" + "github.com/gogits/gogs/modules/auth" "github.com/gogits/gogs/modules/base" + "github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/middleware" + "github.com/gogits/gogs/modules/setting" ) const ( + FORK base.TplName = "repo/pulls/fork" PULLS base.TplName = "repo/pulls" ) +func getForkRepository(ctx *middleware.Context) *models.Repository { + forkRepo, err := models.GetRepositoryById(ctx.ParamsInt64(":repoid")) + if err != nil { + if models.IsErrRepoNotExist(err) { + ctx.Handle(404, "GetRepositoryById", nil) + } else { + ctx.Handle(500, "GetRepositoryById", err) + } + return nil + } + ctx.Data["repo_name"] = forkRepo.Name + ctx.Data["desc"] = forkRepo.Description + ctx.Data["IsPrivate"] = forkRepo.IsPrivate + + if err = forkRepo.GetOwner(); err != nil { + ctx.Handle(500, "GetOwner", err) + return nil + } + ctx.Data["ForkFrom"] = forkRepo.Owner.Name + "/" + forkRepo.Name + + if err := ctx.User.GetOrganizations(); err != nil { + ctx.Handle(500, "GetOrganizations", err) + return nil + } + ctx.Data["Orgs"] = ctx.User.Orgs + + return forkRepo +} + +func Fork(ctx *middleware.Context) { + ctx.Data["Title"] = ctx.Tr("new_fork") + + getForkRepository(ctx) + if ctx.Written() { + return + } + + ctx.Data["ContextUser"] = ctx.User + ctx.HTML(200, FORK) +} + +func ForkPost(ctx *middleware.Context, form auth.CreateRepoForm) { + ctx.Data["Title"] = ctx.Tr("new_fork") + + forkRepo := getForkRepository(ctx) + if ctx.Written() { + return + } + + ctxUser := checkContextUser(ctx, form.Uid) + if ctx.Written() { + return + } + ctx.Data["ContextUser"] = ctxUser + + if ctx.HasError() { + ctx.HTML(200, FORK) + return + } + + // Check ownership of organization. + if ctxUser.IsOrganization() { + if !ctxUser.IsOwnedBy(ctx.User.Id) { + ctx.Error(403) + return + } + } + + repo, err := models.ForkRepository(ctxUser, forkRepo, form.RepoName, form.Description) + if err != nil { + switch { + case models.IsErrRepoAlreadyExist(err): + ctx.Data["Err_RepoName"] = true + ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), FORK, &form) + case models.IsErrNameReserved(err): + ctx.Data["Err_RepoName"] = true + ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), FORK, &form) + case models.IsErrNamePatternNotAllowed(err): + ctx.Data["Err_RepoName"] = true + ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), FORK, &form) + default: + ctx.Handle(500, "ForkPost", err) + } + return + } + + log.Trace("Repository forked[%d]: %s/%s", forkRepo.Id, ctxUser.Name, repo.Name) + ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + repo.Name) +} + func Pulls(ctx *middleware.Context) { ctx.Data["IsRepoToolbarPulls"] = true ctx.HTML(200, PULLS) diff --git a/routers/repo/repo.go b/routers/repo/repo.go index f8df726cb6..dde8b58465 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -25,7 +25,6 @@ import ( const ( CREATE base.TplName = "repo/create" MIGRATE base.TplName = "repo/migrate" - FORK base.TplName = "repo/fork" ) func checkContextUser(ctx *middleware.Context, uid int64) *models.User { @@ -119,7 +118,7 @@ func CreatePost(ctx *middleware.Context, form auth.CreateRepoForm) { } switch { - case err == models.ErrRepoAlreadyExist: + case models.IsErrRepoAlreadyExist(err): ctx.Data["Err_RepoName"] = true ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), CREATE, &form) case models.IsErrNameReserved(err): @@ -222,7 +221,7 @@ func MigratePost(ctx *middleware.Context, form auth.MigrateRepoForm) { } switch { - case err == models.ErrRepoAlreadyExist: + case models.IsErrRepoAlreadyExist(err): ctx.Data["Err_RepoName"] = true ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), MIGRATE, &form) case models.IsErrNameReserved(err): @@ -236,114 +235,6 @@ func MigratePost(ctx *middleware.Context, form auth.MigrateRepoForm) { } } -func getForkRepository(ctx *middleware.Context) (*models.Repository, error) { - forkId := ctx.QueryInt64("fork_id") - ctx.Data["ForkId"] = forkId - - forkRepo, err := models.GetRepositoryById(forkId) - if err != nil { - return nil, fmt.Errorf("GetRepositoryById: %v", err) - } - ctx.Data["repo_name"] = forkRepo.Name - ctx.Data["desc"] = forkRepo.Description - - if err = forkRepo.GetOwner(); err != nil { - return nil, fmt.Errorf("GetOwner: %v", err) - } - ctx.Data["ForkFrom"] = forkRepo.Owner.Name + "/" + forkRepo.Name - return forkRepo, nil -} - -func Fork(ctx *middleware.Context) { - ctx.Data["Title"] = ctx.Tr("new_fork") - - if _, err := getForkRepository(ctx); err != nil { - if models.IsErrRepoNotExist(err) { - ctx.Redirect(setting.AppSubUrl + "/") - } else { - ctx.Handle(500, "getForkRepository", err) - } - return - } - - // FIXME: maybe sometime can directly fork to organization? - ctx.Data["ContextUser"] = ctx.User - if err := ctx.User.GetOrganizations(); err != nil { - ctx.Handle(500, "GetOrganizations", err) - return - } - ctx.Data["Orgs"] = ctx.User.Orgs - - ctx.HTML(200, FORK) -} - -func ForkPost(ctx *middleware.Context, form auth.CreateRepoForm) { - ctx.Data["Title"] = ctx.Tr("new_fork") - - forkRepo, err := getForkRepository(ctx) - if err != nil { - if models.IsErrRepoNotExist(err) { - ctx.Redirect(setting.AppSubUrl + "/") - } else { - ctx.Handle(500, "getForkRepository", err) - } - return - } - - ctxUser := checkContextUser(ctx, form.Uid) - if ctx.Written() { - return - } - ctx.Data["ContextUser"] = ctxUser - - if err := ctx.User.GetOrganizations(); err != nil { - ctx.Handle(500, "GetOrganizations", err) - return - } - ctx.Data["Orgs"] = ctx.User.Orgs - - if ctx.HasError() { - ctx.HTML(200, CREATE) - return - } - - if ctxUser.IsOrganization() { - // Check ownership of organization. - if !ctxUser.IsOwnedBy(ctx.User.Id) { - ctx.Error(403) - return - } - } - - repo, err := models.ForkRepository(ctxUser, forkRepo, form.RepoName, form.Description) - if err == nil { - log.Trace("Repository forked: %s/%s", ctxUser.Name, repo.Name) - ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + repo.Name) - return - } - - if repo != nil { - if errDelete := models.DeleteRepository(ctxUser.Id, repo.Id, ctxUser.Name); errDelete != nil { - log.Error(4, "DeleteRepository: %v", errDelete) - } - } - - // FIXME: merge this with other 2 error handling in to one. - switch { - case err == models.ErrRepoAlreadyExist: - ctx.Data["Err_RepoName"] = true - ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), FORK, &form) - case models.IsErrNameReserved(err): - ctx.Data["Err_RepoName"] = true - ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), FORK, &form) - case models.IsErrNamePatternNotAllowed(err): - ctx.Data["Err_RepoName"] = true - ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), FORK, &form) - default: - ctx.Handle(500, "ForkPost", err) - } -} - func Action(ctx *middleware.Context) { var err error switch ctx.Params(":action") { diff --git a/routers/repo/setting.go b/routers/repo/setting.go index 05881a67ef..1d5e0702ac 100644 --- a/routers/repo/setting.go +++ b/routers/repo/setting.go @@ -56,7 +56,7 @@ func SettingsPost(ctx *middleware.Context, form auth.RepoSettingForm) { if ctx.Repo.Repository.Name != newRepoName { if err := models.ChangeRepositoryName(ctx.Repo.Owner, ctx.Repo.Repository.Name, newRepoName); err != nil { switch { - case err == models.ErrRepoAlreadyExist: + case models.IsErrRepoAlreadyExist(err): ctx.Data["Err_RepoName"] = true ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), SETTINGS_OPTIONS, &form) case models.IsErrNameReserved(err): @@ -128,7 +128,7 @@ func SettingsPost(ctx *middleware.Context, form auth.RepoSettingForm) { } if err = models.TransferOwnership(ctx.User, newOwner, ctx.Repo.Repository); err != nil { - if err == models.ErrRepoAlreadyExist { + if models.IsErrRepoAlreadyExist(err) { ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), SETTINGS_OPTIONS, nil) } else { ctx.Handle(500, "TransferOwnership", err) |