From d66995548389e2c891787a2a85216d5c45d8ba9e Mon Sep 17 00:00:00 2001 From: Kim Lindhardt Madsen Date: Wed, 9 Sep 2015 16:18:41 +0200 Subject: [PATCH] simple api for forking --- routers/api/v1/repo.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/routers/api/v1/repo.go b/routers/api/v1/repo.go index d38a0b0ff..1f21732d1 100644 --- a/routers/api/v1/repo.go +++ b/routers/api/v1/repo.go @@ -191,6 +191,32 @@ func CreateOrgRepo(ctx *middleware.Context, opt api.CreateRepoOption) { createRepo(ctx, org, opt) } +func ForkRepo(ctx *middleware.Context) { + forkRepo(ctx, ctx.User, ctx.Repo.Repository) +} + +func forkRepo(ctx *middleware.Context, owner *models.User, repo *models.Repository) { + forkedRepo, err := models.ForkRepository(owner, repo, repo.Name, repo.Description) + if err != nil { + if models.IsErrRepoAlreadyExist(err) || + models.IsErrNameReserved(err) || + models.IsErrNamePatternNotAllowed(err) { + ctx.JSON(422, &base.ApiJsonErr{err.Error(), base.DOC_URL}) + } else { + log.Error(4, "ForkRepository: %v", err) + if forkedRepo != nil { + if err = models.DeleteRepository(ctx.User.Id, forkedRepo.ID); err != nil { + log.Error(4, "DeleteRepository: %v", err) + } + } + ctx.Error(500) + } + return + } + + ctx.JSON(201, ToApiRepository(owner, 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.