From 184e47f6ce5587dfb13b3b8aaf49f566d778d896 Mon Sep 17 00:00:00 2001 From: David Svantesson Date: Sun, 17 Nov 2019 07:30:01 +0100 Subject: [PATCH] Handle setting default branch via API (#9030) --- routers/api/v1/api.go | 2 +- routers/api/v1/repo/repo.go | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 6fdd0d074..ec6ec191d 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -606,7 +606,7 @@ func RegisterRoutes(m *macaron.Macaron) { m.Group("/:username/:reponame", func() { m.Combo("").Get(reqAnyRepoReader(), repo.Get). Delete(reqToken(), reqOwner(), repo.Delete). - Patch(reqToken(), reqAdmin(), bind(api.EditRepoOption{}), repo.Edit) + Patch(reqToken(), reqAdmin(), bind(api.EditRepoOption{}), context.RepoRef(), repo.Edit) m.Group("/hooks", func() { m.Combo("").Get(repo.ListHooks). Post(bind(api.CreateHookOption{}), repo.CreateHook) diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index 151475a8e..05ab9cb38 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -17,6 +17,7 @@ import ( "code.gitea.io/gitea/modules/auth" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" + "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/migrations" "code.gitea.io/gitea/modules/notification" @@ -687,6 +688,17 @@ func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) err repo.IsTemplate = *opts.Template } + // Default branch only updated if changed and exist + if opts.DefaultBranch != nil && repo.DefaultBranch != *opts.DefaultBranch && ctx.Repo.GitRepo.IsBranchExist(*opts.DefaultBranch) { + if err := ctx.Repo.GitRepo.SetDefaultBranch(*opts.DefaultBranch); err != nil { + if !git.IsErrUnsupportedVersion(err) { + ctx.Error(http.StatusInternalServerError, "SetDefaultBranch", err) + return err + } + } + repo.DefaultBranch = *opts.DefaultBranch + } + if err := models.UpdateRepository(repo, visibilityChanged); err != nil { ctx.Error(http.StatusInternalServerError, "UpdateRepository", err) return err