You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
5.2 KiB

  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repo
  5. import (
  6. "fmt"
  7. "strings"
  8. "code.gitea.io/git"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/auth"
  11. "code.gitea.io/gitea/modules/base"
  12. "code.gitea.io/gitea/modules/context"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/setting"
  15. )
  16. // ProtectedBranch render the page to protect the repository
  17. func ProtectedBranch(ctx *context.Context) {
  18. ctx.Data["Title"] = ctx.Tr("repo.settings")
  19. ctx.Data["PageIsSettingsBranches"] = true
  20. protectedBranches, err := ctx.Repo.Repository.GetProtectedBranches()
  21. if err != nil {
  22. ctx.ServerError("GetProtectedBranches", err)
  23. return
  24. }
  25. ctx.Data["ProtectedBranches"] = protectedBranches
  26. branches := ctx.Data["Branches"].([]string)
  27. leftBranches := make([]string, 0, len(branches)-len(protectedBranches))
  28. for _, b := range branches {
  29. var protected bool
  30. for _, pb := range protectedBranches {
  31. if b == pb.BranchName {
  32. protected = true
  33. break
  34. }
  35. }
  36. if !protected {
  37. leftBranches = append(leftBranches, b)
  38. }
  39. }
  40. ctx.Data["LeftBranches"] = leftBranches
  41. ctx.HTML(200, tplBranches)
  42. }
  43. // ProtectedBranchPost response for protect for a branch of a repository
  44. func ProtectedBranchPost(ctx *context.Context) {
  45. ctx.Data["Title"] = ctx.Tr("repo.settings")
  46. ctx.Data["PageIsSettingsBranches"] = true
  47. repo := ctx.Repo.Repository
  48. switch ctx.Query("action") {
  49. case "default_branch":
  50. if ctx.HasError() {
  51. ctx.HTML(200, tplBranches)
  52. return
  53. }
  54. branch := ctx.Query("branch")
  55. if !ctx.Repo.GitRepo.IsBranchExist(branch) {
  56. ctx.Status(404)
  57. return
  58. } else if repo.DefaultBranch != branch {
  59. repo.DefaultBranch = branch
  60. if err := ctx.Repo.GitRepo.SetDefaultBranch(branch); err != nil {
  61. if !git.IsErrUnsupportedVersion(err) {
  62. ctx.ServerError("SetDefaultBranch", err)
  63. return
  64. }
  65. }
  66. if err := repo.UpdateDefaultBranch(); err != nil {
  67. ctx.ServerError("SetDefaultBranch", err)
  68. return
  69. }
  70. }
  71. log.Trace("Repository basic settings updated: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  72. ctx.Flash.Success(ctx.Tr("repo.settings.update_settings_success"))
  73. ctx.Redirect(setting.AppSubURL + ctx.Req.URL.Path)
  74. default:
  75. ctx.NotFound("", nil)
  76. }
  77. }
  78. // SettingsProtectedBranch renders the protected branch setting page
  79. func SettingsProtectedBranch(c *context.Context) {
  80. branch := c.Params("*")
  81. if !c.Repo.GitRepo.IsBranchExist(branch) {
  82. c.NotFound("IsBranchExist", nil)
  83. return
  84. }
  85. c.Data["Title"] = c.Tr("repo.settings.protected_branches") + " - " + branch
  86. c.Data["PageIsSettingsBranches"] = true
  87. protectBranch, err := models.GetProtectedBranchBy(c.Repo.Repository.ID, branch)
  88. if err != nil {
  89. if !models.IsErrBranchNotExist(err) {
  90. c.ServerError("GetProtectBranchOfRepoByName", err)
  91. return
  92. }
  93. }
  94. if protectBranch == nil {
  95. // No options found, create defaults.
  96. protectBranch = &models.ProtectedBranch{
  97. BranchName: branch,
  98. }
  99. }
  100. users, err := c.Repo.Repository.GetWriters()
  101. if err != nil {
  102. c.ServerError("Repo.Repository.GetWriters", err)
  103. return
  104. }
  105. c.Data["Users"] = users
  106. c.Data["whitelist_users"] = strings.Join(base.Int64sToStrings(protectBranch.WhitelistUserIDs), ",")
  107. if c.Repo.Owner.IsOrganization() {
  108. teams, err := c.Repo.Owner.TeamsWithAccessToRepo(c.Repo.Repository.ID, models.AccessModeWrite)
  109. if err != nil {
  110. c.ServerError("Repo.Owner.TeamsWithAccessToRepo", err)
  111. return
  112. }
  113. c.Data["Teams"] = teams
  114. c.Data["whitelist_teams"] = strings.Join(base.Int64sToStrings(protectBranch.WhitelistTeamIDs), ",")
  115. }
  116. c.Data["Branch"] = protectBranch
  117. c.HTML(200, tplProtectedBranch)
  118. }
  119. // SettingsProtectedBranchPost updates the protected branch settings
  120. func SettingsProtectedBranchPost(ctx *context.Context, f auth.ProtectBranchForm) {
  121. branch := ctx.Params("*")
  122. if !ctx.Repo.GitRepo.IsBranchExist(branch) {
  123. ctx.NotFound("IsBranchExist", nil)
  124. return
  125. }
  126. protectBranch, err := models.GetProtectedBranchBy(ctx.Repo.Repository.ID, branch)
  127. if err != nil {
  128. if !models.IsErrBranchNotExist(err) {
  129. ctx.ServerError("GetProtectBranchOfRepoByName", err)
  130. return
  131. }
  132. }
  133. if f.Protected {
  134. if protectBranch == nil {
  135. // No options found, create defaults.
  136. protectBranch = &models.ProtectedBranch{
  137. RepoID: ctx.Repo.Repository.ID,
  138. BranchName: branch,
  139. }
  140. }
  141. protectBranch.EnableWhitelist = f.EnableWhitelist
  142. whitelistUsers, _ := base.StringsToInt64s(strings.Split(f.WhitelistUsers, ","))
  143. whitelistTeams, _ := base.StringsToInt64s(strings.Split(f.WhitelistTeams, ","))
  144. err = models.UpdateProtectBranch(ctx.Repo.Repository, protectBranch, whitelistUsers, whitelistTeams)
  145. if err != nil {
  146. ctx.ServerError("UpdateProtectBranch", err)
  147. return
  148. }
  149. ctx.Flash.Success(ctx.Tr("repo.settings.update_protect_branch_success", branch))
  150. ctx.Redirect(fmt.Sprintf("%s/settings/branches/%s", ctx.Repo.RepoLink, branch))
  151. } else {
  152. if protectBranch != nil {
  153. if err := ctx.Repo.Repository.DeleteProtectedBranch(protectBranch.ID); err != nil {
  154. ctx.ServerError("DeleteProtectedBranch", err)
  155. return
  156. }
  157. }
  158. ctx.Flash.Success(ctx.Tr("repo.settings.remove_protected_branch_success", branch))
  159. ctx.Redirect(fmt.Sprintf("%s/settings/branches", ctx.Repo.RepoLink))
  160. }
  161. }