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.

setting_protected_branch.go 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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_branch") + " - " + 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. c.Data["merge_whitelist_users"] = strings.Join(base.Int64sToStrings(protectBranch.MergeWhitelistUserIDs), ",")
  108. if c.Repo.Owner.IsOrganization() {
  109. teams, err := c.Repo.Owner.TeamsWithAccessToRepo(c.Repo.Repository.ID, models.AccessModeWrite)
  110. if err != nil {
  111. c.ServerError("Repo.Owner.TeamsWithAccessToRepo", err)
  112. return
  113. }
  114. c.Data["Teams"] = teams
  115. c.Data["whitelist_teams"] = strings.Join(base.Int64sToStrings(protectBranch.WhitelistTeamIDs), ",")
  116. c.Data["merge_whitelist_teams"] = strings.Join(base.Int64sToStrings(protectBranch.MergeWhitelistTeamIDs), ",")
  117. }
  118. c.Data["Branch"] = protectBranch
  119. c.HTML(200, tplProtectedBranch)
  120. }
  121. // SettingsProtectedBranchPost updates the protected branch settings
  122. func SettingsProtectedBranchPost(ctx *context.Context, f auth.ProtectBranchForm) {
  123. branch := ctx.Params("*")
  124. if !ctx.Repo.GitRepo.IsBranchExist(branch) {
  125. ctx.NotFound("IsBranchExist", nil)
  126. return
  127. }
  128. protectBranch, err := models.GetProtectedBranchBy(ctx.Repo.Repository.ID, branch)
  129. if err != nil {
  130. if !models.IsErrBranchNotExist(err) {
  131. ctx.ServerError("GetProtectBranchOfRepoByName", err)
  132. return
  133. }
  134. }
  135. if f.Protected {
  136. if protectBranch == nil {
  137. // No options found, create defaults.
  138. protectBranch = &models.ProtectedBranch{
  139. RepoID: ctx.Repo.Repository.ID,
  140. BranchName: branch,
  141. }
  142. }
  143. protectBranch.EnableWhitelist = f.EnableWhitelist
  144. whitelistUsers, _ := base.StringsToInt64s(strings.Split(f.WhitelistUsers, ","))
  145. whitelistTeams, _ := base.StringsToInt64s(strings.Split(f.WhitelistTeams, ","))
  146. protectBranch.EnableMergeWhitelist = f.EnableMergeWhitelist
  147. mergeWhitelistUsers, _ := base.StringsToInt64s(strings.Split(f.MergeWhitelistUsers, ","))
  148. mergeWhitelistTeams, _ := base.StringsToInt64s(strings.Split(f.MergeWhitelistTeams, ","))
  149. err = models.UpdateProtectBranch(ctx.Repo.Repository, protectBranch, whitelistUsers, whitelistTeams, mergeWhitelistUsers, mergeWhitelistTeams)
  150. if err != nil {
  151. ctx.ServerError("UpdateProtectBranch", err)
  152. return
  153. }
  154. ctx.Flash.Success(ctx.Tr("repo.settings.update_protect_branch_success", branch))
  155. ctx.Redirect(fmt.Sprintf("%s/settings/branches/%s", ctx.Repo.RepoLink, branch))
  156. } else {
  157. if protectBranch != nil {
  158. if err := ctx.Repo.Repository.DeleteProtectedBranch(protectBranch.ID); err != nil {
  159. ctx.ServerError("DeleteProtectedBranch", err)
  160. return
  161. }
  162. }
  163. ctx.Flash.Success(ctx.Tr("repo.settings.remove_protected_branch_success", branch))
  164. ctx.Redirect(fmt.Sprintf("%s/settings/branches", ctx.Repo.RepoLink))
  165. }
  166. }