From 74309f6e1707899da4fb61cf7d1f2e55337cd37c Mon Sep 17 00:00:00 2001 From: Kim Lindhardt Madsen Date: Thu, 26 Nov 2015 10:49:25 +0100 Subject: [PATCH] fork repo options --- cmd/web.go | 3 ++- routers/api/v1/repo.go | 31 +++++++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/cmd/web.go b/cmd/web.go index 159356a55..f6bfaeeaf 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -241,7 +241,8 @@ func runWeb(ctx *cli.Context) { m.Patch("/hooks/:id:int", bind(api.EditHookOption{}), v1.EditRepoHook) m.Get("/raw/*", middleware.RepoRef(), v1.GetRepoRawFile) m.Get("/archive/*", v1.GetRepoArchive) - m.Post("/forks", v1.ForkRepo) + + m.Post("/forks", bind(api.ForkRepoOption{}), v1.ForkRepo) m.Get("/commits/:commitid", middleware.RepoRef(), v1.CommitByID) m.Get("/commits/head", middleware.RepoRef(), v1.HEADCommit) diff --git a/routers/api/v1/repo.go b/routers/api/v1/repo.go index 380062fc4..37674f52b 100644 --- a/routers/api/v1/repo.go +++ b/routers/api/v1/repo.go @@ -187,8 +187,34 @@ func CreateOrgRepo(ctx *middleware.Context, opt api.CreateRepoOption) { createRepo(ctx, org, opt) } -func ForkRepo(ctx *middleware.Context) { - forkedRepo, err := models.ForkRepository(ctx.User, ctx.Repo.Repository, ctx.Repo.Repository.Name, ctx.Repo.Repository.Description) +func ForkRepo(ctx *middleware.Context, opt api.ForkRepoOption) { + if opt.TargetUser != "" { + if ctx.User.Name == opt.TargetUser { + forkRepoTo(ctx, ctx.User, opt.Name, opt.Description) + } else { + if ctx.User.IsAdmin { + targetUser, err :=models.GetUserByName(opt.TargetUser) + if err != nil { + if models.IsErrUserNotExist(err) { + ctx.APIError(422, "", err) + } else { + ctx.APIError(500, "GetUserByName", err) + } + return + } + forkRepoTo(ctx, targetUser, opt.Name, opt.Description) + } else { + ctx.APIError(403, "", "You do not have access to " + opt.TargetUser + ".") + return + } + } + } else { + forkRepoTo(ctx, ctx.User, opt.Name, opt.Description) + } +} + +func forkRepoTo(ctx *middleware.Context, targetUser *models.User, name string, description string) { + forkedRepo, err := models.ForkRepository(targetUser, ctx.Repo.Repository, name, description) if err != nil { if models.IsErrRepoAlreadyExist(err) || models.IsErrNameReserved(err) || @@ -209,6 +235,7 @@ func ForkRepo(ctx *middleware.Context) { ctx.JSON(201, ToApiRepository(ctx.User, forkedRepo, api.Permission{true, true, true})) } + func MigrateRepo(ctx *middleware.Context, form auth.MigrateRepoForm) { ctxUser := ctx.User // Not equal means current user is an organization.