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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repo
  4. import (
  5. "fmt"
  6. "net/http"
  7. "net/url"
  8. "strings"
  9. "time"
  10. git_model "code.gitea.io/gitea/models/git"
  11. "code.gitea.io/gitea/models/organization"
  12. "code.gitea.io/gitea/models/perm"
  13. access_model "code.gitea.io/gitea/models/perm/access"
  14. repo_model "code.gitea.io/gitea/models/repo"
  15. "code.gitea.io/gitea/modules/base"
  16. "code.gitea.io/gitea/modules/context"
  17. "code.gitea.io/gitea/modules/git"
  18. "code.gitea.io/gitea/modules/log"
  19. "code.gitea.io/gitea/modules/setting"
  20. "code.gitea.io/gitea/modules/web"
  21. "code.gitea.io/gitea/services/forms"
  22. pull_service "code.gitea.io/gitea/services/pull"
  23. "code.gitea.io/gitea/services/repository"
  24. "github.com/gobwas/glob"
  25. )
  26. const (
  27. tplProtectedBranch base.TplName = "repo/settings/protected_branch"
  28. )
  29. // ProtectedBranchRules render the page to protect the repository
  30. func ProtectedBranchRules(ctx *context.Context) {
  31. ctx.Data["Title"] = ctx.Tr("repo.settings.branches")
  32. ctx.Data["PageIsSettingsBranches"] = true
  33. rules, err := git_model.FindRepoProtectedBranchRules(ctx, ctx.Repo.Repository.ID)
  34. if err != nil {
  35. ctx.ServerError("GetProtectedBranches", err)
  36. return
  37. }
  38. ctx.Data["ProtectedBranches"] = rules
  39. ctx.HTML(http.StatusOK, tplBranches)
  40. }
  41. // SetDefaultBranchPost set default branch
  42. func SetDefaultBranchPost(ctx *context.Context) {
  43. ctx.Data["Title"] = ctx.Tr("repo.settings.branches.update_default_branch")
  44. ctx.Data["PageIsSettingsBranches"] = true
  45. repo := ctx.Repo.Repository
  46. switch ctx.FormString("action") {
  47. case "default_branch":
  48. if ctx.HasError() {
  49. ctx.HTML(http.StatusOK, tplBranches)
  50. return
  51. }
  52. branch := ctx.FormString("branch")
  53. if !ctx.Repo.GitRepo.IsBranchExist(branch) {
  54. ctx.Status(http.StatusNotFound)
  55. return
  56. } else if repo.DefaultBranch != branch {
  57. repo.DefaultBranch = branch
  58. if err := ctx.Repo.GitRepo.SetDefaultBranch(branch); err != nil {
  59. if !git.IsErrUnsupportedVersion(err) {
  60. ctx.ServerError("SetDefaultBranch", err)
  61. return
  62. }
  63. }
  64. if err := repo_model.UpdateDefaultBranch(repo); err != nil {
  65. ctx.ServerError("SetDefaultBranch", err)
  66. return
  67. }
  68. }
  69. log.Trace("Repository basic settings updated: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  70. ctx.Flash.Success(ctx.Tr("repo.settings.update_settings_success"))
  71. ctx.Redirect(setting.AppSubURL + ctx.Req.URL.EscapedPath())
  72. default:
  73. ctx.NotFound("", nil)
  74. }
  75. }
  76. // SettingsProtectedBranch renders the protected branch setting page
  77. func SettingsProtectedBranch(c *context.Context) {
  78. ruleName := c.FormString("rule_name")
  79. var rule *git_model.ProtectedBranch
  80. if ruleName != "" {
  81. var err error
  82. rule, err = git_model.GetProtectedBranchRuleByName(c, c.Repo.Repository.ID, ruleName)
  83. if err != nil {
  84. c.ServerError("GetProtectBranchOfRepoByName", err)
  85. return
  86. }
  87. }
  88. if rule == nil {
  89. // No options found, create defaults.
  90. rule = &git_model.ProtectedBranch{}
  91. }
  92. c.Data["PageIsSettingsBranches"] = true
  93. c.Data["Title"] = c.Tr("repo.settings.protected_branch") + " - " + rule.RuleName
  94. users, err := access_model.GetRepoReaders(c.Repo.Repository)
  95. if err != nil {
  96. c.ServerError("Repo.Repository.GetReaders", err)
  97. return
  98. }
  99. c.Data["Users"] = users
  100. c.Data["whitelist_users"] = strings.Join(base.Int64sToStrings(rule.WhitelistUserIDs), ",")
  101. c.Data["merge_whitelist_users"] = strings.Join(base.Int64sToStrings(rule.MergeWhitelistUserIDs), ",")
  102. c.Data["approvals_whitelist_users"] = strings.Join(base.Int64sToStrings(rule.ApprovalsWhitelistUserIDs), ",")
  103. c.Data["status_check_contexts"] = strings.Join(rule.StatusCheckContexts, "\n")
  104. contexts, _ := git_model.FindRepoRecentCommitStatusContexts(c, c.Repo.Repository.ID, 7*24*time.Hour) // Find last week status check contexts
  105. c.Data["recent_status_checks"] = contexts
  106. if c.Repo.Owner.IsOrganization() {
  107. teams, err := organization.OrgFromUser(c.Repo.Owner).TeamsWithAccessToRepo(c.Repo.Repository.ID, perm.AccessModeRead)
  108. if err != nil {
  109. c.ServerError("Repo.Owner.TeamsWithAccessToRepo", err)
  110. return
  111. }
  112. c.Data["Teams"] = teams
  113. c.Data["whitelist_teams"] = strings.Join(base.Int64sToStrings(rule.WhitelistTeamIDs), ",")
  114. c.Data["merge_whitelist_teams"] = strings.Join(base.Int64sToStrings(rule.MergeWhitelistTeamIDs), ",")
  115. c.Data["approvals_whitelist_teams"] = strings.Join(base.Int64sToStrings(rule.ApprovalsWhitelistTeamIDs), ",")
  116. }
  117. c.Data["Rule"] = rule
  118. c.HTML(http.StatusOK, tplProtectedBranch)
  119. }
  120. // SettingsProtectedBranchPost updates the protected branch settings
  121. func SettingsProtectedBranchPost(ctx *context.Context) {
  122. f := web.GetForm(ctx).(*forms.ProtectBranchForm)
  123. var protectBranch *git_model.ProtectedBranch
  124. if f.RuleName == "" {
  125. ctx.Flash.Error(ctx.Tr("repo.settings.protected_branch_required_rule_name"))
  126. ctx.Redirect(fmt.Sprintf("%s/settings/branches/edit", ctx.Repo.RepoLink))
  127. return
  128. }
  129. var err error
  130. if f.RuleID > 0 {
  131. // If the RuleID isn't 0, it must be an edit operation. So we get rule by id.
  132. protectBranch, err = git_model.GetProtectedBranchRuleByID(ctx, ctx.Repo.Repository.ID, f.RuleID)
  133. if err != nil {
  134. ctx.ServerError("GetProtectBranchOfRepoByID", err)
  135. return
  136. }
  137. if protectBranch != nil && protectBranch.RuleName != f.RuleName {
  138. // RuleName changed. We need to check if there is a rule with the same name.
  139. // If a rule with the same name exists, an error should be returned.
  140. sameNameProtectBranch, err := git_model.GetProtectedBranchRuleByName(ctx, ctx.Repo.Repository.ID, f.RuleName)
  141. if err != nil {
  142. ctx.ServerError("GetProtectBranchOfRepoByName", err)
  143. return
  144. }
  145. if sameNameProtectBranch != nil {
  146. ctx.Flash.Error(ctx.Tr("repo.settings.protected_branch_duplicate_rule_name"))
  147. ctx.Redirect(fmt.Sprintf("%s/settings/branches/edit?rule_name=%s", ctx.Repo.RepoLink, protectBranch.RuleName))
  148. return
  149. }
  150. }
  151. } else {
  152. // FIXME: If a new ProtectBranch has a duplicate RuleName, an error should be returned.
  153. // Currently, if a new ProtectBranch with a duplicate RuleName is created, the existing ProtectBranch will be updated.
  154. // But we cannot modify this logic now because many unit tests rely on it.
  155. protectBranch, err = git_model.GetProtectedBranchRuleByName(ctx, ctx.Repo.Repository.ID, f.RuleName)
  156. if err != nil {
  157. ctx.ServerError("GetProtectBranchOfRepoByName", err)
  158. return
  159. }
  160. }
  161. if protectBranch == nil {
  162. // No options found, create defaults.
  163. protectBranch = &git_model.ProtectedBranch{
  164. RepoID: ctx.Repo.Repository.ID,
  165. RuleName: f.RuleName,
  166. }
  167. }
  168. var whitelistUsers, whitelistTeams, mergeWhitelistUsers, mergeWhitelistTeams, approvalsWhitelistUsers, approvalsWhitelistTeams []int64
  169. protectBranch.RuleName = f.RuleName
  170. if f.RequiredApprovals < 0 {
  171. ctx.Flash.Error(ctx.Tr("repo.settings.protected_branch_required_approvals_min"))
  172. ctx.Redirect(fmt.Sprintf("%s/settings/branches/edit?rule_name=%s", ctx.Repo.RepoLink, f.RuleName))
  173. return
  174. }
  175. switch f.EnablePush {
  176. case "all":
  177. protectBranch.CanPush = true
  178. protectBranch.EnableWhitelist = false
  179. protectBranch.WhitelistDeployKeys = false
  180. case "whitelist":
  181. protectBranch.CanPush = true
  182. protectBranch.EnableWhitelist = true
  183. protectBranch.WhitelistDeployKeys = f.WhitelistDeployKeys
  184. if strings.TrimSpace(f.WhitelistUsers) != "" {
  185. whitelistUsers, _ = base.StringsToInt64s(strings.Split(f.WhitelistUsers, ","))
  186. }
  187. if strings.TrimSpace(f.WhitelistTeams) != "" {
  188. whitelistTeams, _ = base.StringsToInt64s(strings.Split(f.WhitelistTeams, ","))
  189. }
  190. default:
  191. protectBranch.CanPush = false
  192. protectBranch.EnableWhitelist = false
  193. protectBranch.WhitelistDeployKeys = false
  194. }
  195. protectBranch.EnableMergeWhitelist = f.EnableMergeWhitelist
  196. if f.EnableMergeWhitelist {
  197. if strings.TrimSpace(f.MergeWhitelistUsers) != "" {
  198. mergeWhitelistUsers, _ = base.StringsToInt64s(strings.Split(f.MergeWhitelistUsers, ","))
  199. }
  200. if strings.TrimSpace(f.MergeWhitelistTeams) != "" {
  201. mergeWhitelistTeams, _ = base.StringsToInt64s(strings.Split(f.MergeWhitelistTeams, ","))
  202. }
  203. }
  204. protectBranch.EnableStatusCheck = f.EnableStatusCheck
  205. if f.EnableStatusCheck {
  206. patterns := strings.Split(strings.ReplaceAll(f.StatusCheckContexts, "\r", "\n"), "\n")
  207. validPatterns := make([]string, 0, len(patterns))
  208. for _, pattern := range patterns {
  209. trimmed := strings.TrimSpace(pattern)
  210. if trimmed == "" {
  211. continue
  212. }
  213. if _, err := glob.Compile(trimmed); err != nil {
  214. ctx.Flash.Error(ctx.Tr("repo.settings.protect_invalid_status_check_pattern", pattern))
  215. ctx.Redirect(fmt.Sprintf("%s/settings/branches/edit?rule_name=%s", ctx.Repo.RepoLink, url.QueryEscape(protectBranch.RuleName)))
  216. return
  217. }
  218. validPatterns = append(validPatterns, trimmed)
  219. }
  220. if len(validPatterns) == 0 {
  221. // if status check is enabled, patterns slice is not allowed to be empty
  222. ctx.Flash.Error(ctx.Tr("repo.settings.protect_no_valid_status_check_patterns"))
  223. ctx.Redirect(fmt.Sprintf("%s/settings/branches/edit?rule_name=%s", ctx.Repo.RepoLink, url.QueryEscape(protectBranch.RuleName)))
  224. return
  225. }
  226. protectBranch.StatusCheckContexts = validPatterns
  227. } else {
  228. protectBranch.StatusCheckContexts = nil
  229. }
  230. protectBranch.RequiredApprovals = f.RequiredApprovals
  231. protectBranch.EnableApprovalsWhitelist = f.EnableApprovalsWhitelist
  232. if f.EnableApprovalsWhitelist {
  233. if strings.TrimSpace(f.ApprovalsWhitelistUsers) != "" {
  234. approvalsWhitelistUsers, _ = base.StringsToInt64s(strings.Split(f.ApprovalsWhitelistUsers, ","))
  235. }
  236. if strings.TrimSpace(f.ApprovalsWhitelistTeams) != "" {
  237. approvalsWhitelistTeams, _ = base.StringsToInt64s(strings.Split(f.ApprovalsWhitelistTeams, ","))
  238. }
  239. }
  240. protectBranch.BlockOnRejectedReviews = f.BlockOnRejectedReviews
  241. protectBranch.BlockOnOfficialReviewRequests = f.BlockOnOfficialReviewRequests
  242. protectBranch.DismissStaleApprovals = f.DismissStaleApprovals
  243. protectBranch.RequireSignedCommits = f.RequireSignedCommits
  244. protectBranch.ProtectedFilePatterns = f.ProtectedFilePatterns
  245. protectBranch.UnprotectedFilePatterns = f.UnprotectedFilePatterns
  246. protectBranch.BlockOnOutdatedBranch = f.BlockOnOutdatedBranch
  247. err = git_model.UpdateProtectBranch(ctx, ctx.Repo.Repository, protectBranch, git_model.WhitelistOptions{
  248. UserIDs: whitelistUsers,
  249. TeamIDs: whitelistTeams,
  250. MergeUserIDs: mergeWhitelistUsers,
  251. MergeTeamIDs: mergeWhitelistTeams,
  252. ApprovalsUserIDs: approvalsWhitelistUsers,
  253. ApprovalsTeamIDs: approvalsWhitelistTeams,
  254. })
  255. if err != nil {
  256. ctx.ServerError("UpdateProtectBranch", err)
  257. return
  258. }
  259. // FIXME: since we only need to recheck files protected rules, we could improve this
  260. matchedBranches, err := git_model.FindAllMatchedBranches(ctx, ctx.Repo.GitRepo, protectBranch.RuleName)
  261. if err != nil {
  262. ctx.ServerError("FindAllMatchedBranches", err)
  263. return
  264. }
  265. for _, branchName := range matchedBranches {
  266. if err = pull_service.CheckPRsForBaseBranch(ctx.Repo.Repository, branchName); err != nil {
  267. ctx.ServerError("CheckPRsForBaseBranch", err)
  268. return
  269. }
  270. }
  271. ctx.Flash.Success(ctx.Tr("repo.settings.update_protect_branch_success", protectBranch.RuleName))
  272. ctx.Redirect(fmt.Sprintf("%s/settings/branches?rule_name=%s", ctx.Repo.RepoLink, protectBranch.RuleName))
  273. }
  274. // DeleteProtectedBranchRulePost delete protected branch rule by id
  275. func DeleteProtectedBranchRulePost(ctx *context.Context) {
  276. ruleID := ctx.ParamsInt64("id")
  277. if ruleID <= 0 {
  278. ctx.Flash.Error(ctx.Tr("repo.settings.remove_protected_branch_failed", fmt.Sprintf("%d", ruleID)))
  279. ctx.JSON(http.StatusOK, map[string]any{
  280. "redirect": fmt.Sprintf("%s/settings/branches", ctx.Repo.RepoLink),
  281. })
  282. return
  283. }
  284. rule, err := git_model.GetProtectedBranchRuleByID(ctx, ctx.Repo.Repository.ID, ruleID)
  285. if err != nil {
  286. ctx.Flash.Error(ctx.Tr("repo.settings.remove_protected_branch_failed", fmt.Sprintf("%d", ruleID)))
  287. ctx.JSON(http.StatusOK, map[string]any{
  288. "redirect": fmt.Sprintf("%s/settings/branches", ctx.Repo.RepoLink),
  289. })
  290. return
  291. }
  292. if rule == nil {
  293. ctx.Flash.Error(ctx.Tr("repo.settings.remove_protected_branch_failed", fmt.Sprintf("%d", ruleID)))
  294. ctx.JSON(http.StatusOK, map[string]any{
  295. "redirect": fmt.Sprintf("%s/settings/branches", ctx.Repo.RepoLink),
  296. })
  297. return
  298. }
  299. if err := git_model.DeleteProtectedBranch(ctx, ctx.Repo.Repository.ID, ruleID); err != nil {
  300. ctx.Flash.Error(ctx.Tr("repo.settings.remove_protected_branch_failed", rule.RuleName))
  301. ctx.JSON(http.StatusOK, map[string]any{
  302. "redirect": fmt.Sprintf("%s/settings/branches", ctx.Repo.RepoLink),
  303. })
  304. return
  305. }
  306. ctx.Flash.Success(ctx.Tr("repo.settings.remove_protected_branch_success", rule.RuleName))
  307. ctx.JSON(http.StatusOK, map[string]any{
  308. "redirect": fmt.Sprintf("%s/settings/branches", ctx.Repo.RepoLink),
  309. })
  310. }
  311. // RenameBranchPost responses for rename a branch
  312. func RenameBranchPost(ctx *context.Context) {
  313. form := web.GetForm(ctx).(*forms.RenameBranchForm)
  314. if !ctx.Repo.CanCreateBranch() {
  315. ctx.NotFound("RenameBranch", nil)
  316. return
  317. }
  318. if ctx.HasError() {
  319. ctx.Flash.Error(ctx.GetErrMsg())
  320. ctx.Redirect(fmt.Sprintf("%s/branches", ctx.Repo.RepoLink))
  321. return
  322. }
  323. msg, err := repository.RenameBranch(ctx, ctx.Repo.Repository, ctx.Doer, ctx.Repo.GitRepo, form.From, form.To)
  324. if err != nil {
  325. ctx.ServerError("RenameBranch", err)
  326. return
  327. }
  328. if msg == "target_exist" {
  329. ctx.Flash.Error(ctx.Tr("repo.settings.rename_branch_failed_exist", form.To))
  330. ctx.Redirect(fmt.Sprintf("%s/branches", ctx.Repo.RepoLink))
  331. return
  332. }
  333. if msg == "from_not_exist" {
  334. ctx.Flash.Error(ctx.Tr("repo.settings.rename_branch_failed_not_exist", form.From))
  335. ctx.Redirect(fmt.Sprintf("%s/branches", ctx.Repo.RepoLink))
  336. return
  337. }
  338. ctx.Flash.Success(ctx.Tr("repo.settings.rename_branch_success", form.From, form.To))
  339. ctx.Redirect(fmt.Sprintf("%s/branches", ctx.Repo.RepoLink))
  340. }