gitea源码

default_branch.go 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package private
  4. import (
  5. "fmt"
  6. "net/http"
  7. repo_model "code.gitea.io/gitea/models/repo"
  8. "code.gitea.io/gitea/modules/gitrepo"
  9. "code.gitea.io/gitea/modules/private"
  10. gitea_context "code.gitea.io/gitea/services/context"
  11. repo_service "code.gitea.io/gitea/services/repository"
  12. )
  13. // SetDefaultBranch updates the default branch
  14. func SetDefaultBranch(ctx *gitea_context.PrivateContext) {
  15. ownerName := ctx.PathParam("owner")
  16. repoName := ctx.PathParam("repo")
  17. branch := ctx.PathParam("branch")
  18. ctx.Repo.Repository.DefaultBranch = branch
  19. if err := gitrepo.SetDefaultBranch(ctx, ctx.Repo.Repository, ctx.Repo.Repository.DefaultBranch); err != nil {
  20. ctx.JSON(http.StatusInternalServerError, private.Response{
  21. Err: fmt.Sprintf("Unable to set default branch on repository: %s/%s Error: %v", ownerName, repoName, err),
  22. })
  23. return
  24. }
  25. if err := repo_model.UpdateDefaultBranch(ctx, ctx.Repo.Repository); err != nil {
  26. ctx.JSON(http.StatusInternalServerError, private.Response{
  27. Err: fmt.Sprintf("Unable to set default branch on repository: %s/%s Error: %v", ownerName, repoName, err),
  28. })
  29. return
  30. }
  31. if err := repo_service.AddRepoToLicenseUpdaterQueue(&repo_service.LicenseUpdaterOptions{
  32. RepoID: ctx.Repo.Repository.ID,
  33. }); err != nil {
  34. ctx.JSON(http.StatusInternalServerError, private.Response{
  35. Err: fmt.Sprintf("Unable to set default branch on repository: %s/%s Error: %v", ownerName, repoName, err),
  36. })
  37. return
  38. }
  39. ctx.PlainText(http.StatusOK, "success")
  40. }