2017-09-14 04:16:22 -04:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-09-14 04:16:22 -04:00
|
|
|
|
2023-07-01 20:59:32 -04:00
|
|
|
package setting
|
2017-09-14 04:16:22 -04:00
|
|
|
|
|
|
|
import (
|
2024-03-25 20:39:15 -04:00
|
|
|
"errors"
|
2017-09-14 04:16:22 -04:00
|
|
|
"fmt"
|
2021-04-05 11:30:52 -04:00
|
|
|
"net/http"
|
2023-05-17 04:11:13 -04:00
|
|
|
"net/url"
|
2017-09-14 04:16:22 -04:00
|
|
|
"strings"
|
2019-09-18 01:39:45 -04:00
|
|
|
"time"
|
2017-09-14 04:16:22 -04:00
|
|
|
|
2022-06-12 11:51:54 -04:00
|
|
|
git_model "code.gitea.io/gitea/models/git"
|
2022-03-29 02:29:02 -04:00
|
|
|
"code.gitea.io/gitea/models/organization"
|
2021-11-28 06:58:28 -05:00
|
|
|
"code.gitea.io/gitea/models/perm"
|
2022-05-11 06:09:36 -04:00
|
|
|
access_model "code.gitea.io/gitea/models/perm/access"
|
2017-09-14 04:16:22 -04:00
|
|
|
"code.gitea.io/gitea/modules/base"
|
2021-01-26 10:36:53 -05:00
|
|
|
"code.gitea.io/gitea/modules/web"
|
2023-07-21 07:20:04 -04:00
|
|
|
"code.gitea.io/gitea/routers/web/repo"
|
2024-02-27 02:12:22 -05:00
|
|
|
"code.gitea.io/gitea/services/context"
|
2021-04-06 15:44:05 -04:00
|
|
|
"code.gitea.io/gitea/services/forms"
|
2020-10-13 14:50:57 -04:00
|
|
|
pull_service "code.gitea.io/gitea/services/pull"
|
2021-10-08 13:03:04 -04:00
|
|
|
"code.gitea.io/gitea/services/repository"
|
2023-05-17 04:11:13 -04:00
|
|
|
|
|
|
|
"github.com/gobwas/glob"
|
2017-09-14 04:16:22 -04:00
|
|
|
)
|
|
|
|
|
2023-01-16 03:00:22 -05:00
|
|
|
const (
|
|
|
|
tplProtectedBranch base.TplName = "repo/settings/protected_branch"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ProtectedBranchRules render the page to protect the repository
|
|
|
|
func ProtectedBranchRules(ctx *context.Context) {
|
2023-02-01 17:56:10 -05:00
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings.branches")
|
2017-09-14 04:16:22 -04:00
|
|
|
ctx.Data["PageIsSettingsBranches"] = true
|
|
|
|
|
2023-01-16 03:00:22 -05:00
|
|
|
rules, err := git_model.FindRepoProtectedBranchRules(ctx, ctx.Repo.Repository.ID)
|
2017-09-14 04:16:22 -04:00
|
|
|
if err != nil {
|
2018-01-10 16:34:17 -05:00
|
|
|
ctx.ServerError("GetProtectedBranches", err)
|
2017-09-14 04:16:22 -04:00
|
|
|
return
|
|
|
|
}
|
2023-01-16 03:00:22 -05:00
|
|
|
ctx.Data["ProtectedBranches"] = rules
|
2017-09-14 04:16:22 -04:00
|
|
|
|
2023-07-21 07:20:04 -04:00
|
|
|
repo.PrepareBranchList(ctx)
|
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-05 11:30:52 -04:00
|
|
|
ctx.HTML(http.StatusOK, tplBranches)
|
2017-09-14 04:16:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// SettingsProtectedBranch renders the protected branch setting page
|
|
|
|
func SettingsProtectedBranch(c *context.Context) {
|
2023-01-16 03:00:22 -05:00
|
|
|
ruleName := c.FormString("rule_name")
|
|
|
|
var rule *git_model.ProtectedBranch
|
|
|
|
if ruleName != "" {
|
|
|
|
var err error
|
|
|
|
rule, err = git_model.GetProtectedBranchRuleByName(c, c.Repo.Repository.ID, ruleName)
|
|
|
|
if err != nil {
|
2018-01-10 16:34:17 -05:00
|
|
|
c.ServerError("GetProtectBranchOfRepoByName", err)
|
2017-09-14 04:16:22 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-16 03:00:22 -05:00
|
|
|
if rule == nil {
|
2017-09-14 04:16:22 -04:00
|
|
|
// No options found, create defaults.
|
2023-01-16 03:00:22 -05:00
|
|
|
rule = &git_model.ProtectedBranch{}
|
2017-09-14 04:16:22 -04:00
|
|
|
}
|
|
|
|
|
2023-01-16 03:00:22 -05:00
|
|
|
c.Data["PageIsSettingsBranches"] = true
|
2024-02-14 16:48:45 -05:00
|
|
|
c.Data["Title"] = c.Locale.TrString("repo.settings.protected_branch") + " - " + rule.RuleName
|
2023-01-16 03:00:22 -05:00
|
|
|
|
2023-10-03 06:30:41 -04:00
|
|
|
users, err := access_model.GetRepoReaders(c, c.Repo.Repository)
|
2017-09-14 04:16:22 -04:00
|
|
|
if err != nil {
|
2019-10-08 15:18:17 -04:00
|
|
|
c.ServerError("Repo.Repository.GetReaders", err)
|
2017-09-14 04:16:22 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Data["Users"] = users
|
2023-01-16 03:00:22 -05:00
|
|
|
c.Data["whitelist_users"] = strings.Join(base.Int64sToStrings(rule.WhitelistUserIDs), ",")
|
|
|
|
c.Data["merge_whitelist_users"] = strings.Join(base.Int64sToStrings(rule.MergeWhitelistUserIDs), ",")
|
|
|
|
c.Data["approvals_whitelist_users"] = strings.Join(base.Int64sToStrings(rule.ApprovalsWhitelistUserIDs), ",")
|
2023-05-17 04:11:13 -04:00
|
|
|
c.Data["status_check_contexts"] = strings.Join(rule.StatusCheckContexts, "\n")
|
2023-01-08 22:50:54 -05:00
|
|
|
contexts, _ := git_model.FindRepoRecentCommitStatusContexts(c, c.Repo.Repository.ID, 7*24*time.Hour) // Find last week status check contexts
|
2023-05-17 04:11:13 -04:00
|
|
|
c.Data["recent_status_checks"] = contexts
|
2019-09-18 01:39:45 -04:00
|
|
|
|
2017-09-14 04:16:22 -04:00
|
|
|
if c.Repo.Owner.IsOrganization() {
|
2023-10-03 06:30:41 -04:00
|
|
|
teams, err := organization.OrgFromUser(c.Repo.Owner).TeamsWithAccessToRepo(c, c.Repo.Repository.ID, perm.AccessModeRead)
|
2017-09-14 04:16:22 -04:00
|
|
|
if err != nil {
|
2018-01-10 16:34:17 -05:00
|
|
|
c.ServerError("Repo.Owner.TeamsWithAccessToRepo", err)
|
2017-09-14 04:16:22 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Data["Teams"] = teams
|
2023-01-16 03:00:22 -05:00
|
|
|
c.Data["whitelist_teams"] = strings.Join(base.Int64sToStrings(rule.WhitelistTeamIDs), ",")
|
|
|
|
c.Data["merge_whitelist_teams"] = strings.Join(base.Int64sToStrings(rule.MergeWhitelistTeamIDs), ",")
|
|
|
|
c.Data["approvals_whitelist_teams"] = strings.Join(base.Int64sToStrings(rule.ApprovalsWhitelistTeamIDs), ",")
|
2017-09-14 04:16:22 -04:00
|
|
|
}
|
|
|
|
|
2023-01-16 03:00:22 -05:00
|
|
|
c.Data["Rule"] = rule
|
2021-04-05 11:30:52 -04:00
|
|
|
c.HTML(http.StatusOK, tplProtectedBranch)
|
2017-09-14 04:16:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// SettingsProtectedBranchPost updates the protected branch settings
|
2021-01-26 10:36:53 -05:00
|
|
|
func SettingsProtectedBranchPost(ctx *context.Context) {
|
2021-04-06 15:44:05 -04:00
|
|
|
f := web.GetForm(ctx).(*forms.ProtectBranchForm)
|
2023-01-16 03:00:22 -05:00
|
|
|
var protectBranch *git_model.ProtectedBranch
|
|
|
|
if f.RuleName == "" {
|
|
|
|
ctx.Flash.Error(ctx.Tr("repo.settings.protected_branch_required_rule_name"))
|
|
|
|
ctx.Redirect(fmt.Sprintf("%s/settings/branches/edit", ctx.Repo.RepoLink))
|
2017-09-14 04:16:22 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-16 03:00:22 -05:00
|
|
|
var err error
|
2023-02-20 06:30:41 -05:00
|
|
|
if f.RuleID > 0 {
|
|
|
|
// If the RuleID isn't 0, it must be an edit operation. So we get rule by id.
|
|
|
|
protectBranch, err = git_model.GetProtectedBranchRuleByID(ctx, ctx.Repo.Repository.ID, f.RuleID)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetProtectBranchOfRepoByID", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if protectBranch != nil && protectBranch.RuleName != f.RuleName {
|
|
|
|
// RuleName changed. We need to check if there is a rule with the same name.
|
|
|
|
// If a rule with the same name exists, an error should be returned.
|
|
|
|
sameNameProtectBranch, err := git_model.GetProtectedBranchRuleByName(ctx, ctx.Repo.Repository.ID, f.RuleName)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetProtectBranchOfRepoByName", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if sameNameProtectBranch != nil {
|
|
|
|
ctx.Flash.Error(ctx.Tr("repo.settings.protected_branch_duplicate_rule_name"))
|
|
|
|
ctx.Redirect(fmt.Sprintf("%s/settings/branches/edit?rule_name=%s", ctx.Repo.RepoLink, protectBranch.RuleName))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2024-02-25 13:50:46 -05:00
|
|
|
// Check if a rule already exists with this rulename, if so redirect to it.
|
2023-02-20 06:30:41 -05:00
|
|
|
protectBranch, err = git_model.GetProtectedBranchRuleByName(ctx, ctx.Repo.Repository.ID, f.RuleName)
|
|
|
|
if err != nil {
|
2024-02-25 13:50:46 -05:00
|
|
|
ctx.ServerError("GetProtectedBranchRuleByName", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if protectBranch != nil {
|
|
|
|
ctx.Flash.Error(ctx.Tr("repo.settings.protected_branch_duplicate_rule_name"))
|
|
|
|
ctx.Redirect(fmt.Sprintf("%s/settings/branches/edit?rule_name=%s", ctx.Repo.RepoLink, protectBranch.RuleName))
|
2023-02-20 06:30:41 -05:00
|
|
|
return
|
|
|
|
}
|
2023-01-16 03:00:22 -05:00
|
|
|
}
|
|
|
|
if protectBranch == nil {
|
|
|
|
// No options found, create defaults.
|
|
|
|
protectBranch = &git_model.ProtectedBranch{
|
|
|
|
RepoID: ctx.Repo.Repository.ID,
|
|
|
|
RuleName: f.RuleName,
|
2017-09-14 04:16:22 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-16 03:00:22 -05:00
|
|
|
var whitelistUsers, whitelistTeams, mergeWhitelistUsers, mergeWhitelistTeams, approvalsWhitelistUsers, approvalsWhitelistTeams []int64
|
|
|
|
protectBranch.RuleName = f.RuleName
|
|
|
|
if f.RequiredApprovals < 0 {
|
|
|
|
ctx.Flash.Error(ctx.Tr("repo.settings.protected_branch_required_approvals_min"))
|
|
|
|
ctx.Redirect(fmt.Sprintf("%s/settings/branches/edit?rule_name=%s", ctx.Repo.RepoLink, f.RuleName))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch f.EnablePush {
|
|
|
|
case "all":
|
|
|
|
protectBranch.CanPush = true
|
|
|
|
protectBranch.EnableWhitelist = false
|
|
|
|
protectBranch.WhitelistDeployKeys = false
|
|
|
|
case "whitelist":
|
|
|
|
protectBranch.CanPush = true
|
|
|
|
protectBranch.EnableWhitelist = true
|
|
|
|
protectBranch.WhitelistDeployKeys = f.WhitelistDeployKeys
|
|
|
|
if strings.TrimSpace(f.WhitelistUsers) != "" {
|
|
|
|
whitelistUsers, _ = base.StringsToInt64s(strings.Split(f.WhitelistUsers, ","))
|
2017-09-14 04:16:22 -04:00
|
|
|
}
|
2023-01-16 03:00:22 -05:00
|
|
|
if strings.TrimSpace(f.WhitelistTeams) != "" {
|
|
|
|
whitelistTeams, _ = base.StringsToInt64s(strings.Split(f.WhitelistTeams, ","))
|
2018-12-11 06:28:37 -05:00
|
|
|
}
|
2023-01-16 03:00:22 -05:00
|
|
|
default:
|
|
|
|
protectBranch.CanPush = false
|
|
|
|
protectBranch.EnableWhitelist = false
|
|
|
|
protectBranch.WhitelistDeployKeys = false
|
|
|
|
}
|
2017-09-14 04:16:22 -04:00
|
|
|
|
2023-01-16 03:00:22 -05:00
|
|
|
protectBranch.EnableMergeWhitelist = f.EnableMergeWhitelist
|
|
|
|
if f.EnableMergeWhitelist {
|
|
|
|
if strings.TrimSpace(f.MergeWhitelistUsers) != "" {
|
|
|
|
mergeWhitelistUsers, _ = base.StringsToInt64s(strings.Split(f.MergeWhitelistUsers, ","))
|
2018-11-28 06:26:14 -05:00
|
|
|
}
|
2023-01-16 03:00:22 -05:00
|
|
|
if strings.TrimSpace(f.MergeWhitelistTeams) != "" {
|
|
|
|
mergeWhitelistTeams, _ = base.StringsToInt64s(strings.Split(f.MergeWhitelistTeams, ","))
|
2018-11-28 06:26:14 -05:00
|
|
|
}
|
2023-01-16 03:00:22 -05:00
|
|
|
}
|
2019-09-18 01:39:45 -04:00
|
|
|
|
2023-01-16 03:00:22 -05:00
|
|
|
protectBranch.EnableStatusCheck = f.EnableStatusCheck
|
|
|
|
if f.EnableStatusCheck {
|
2023-05-17 04:11:13 -04:00
|
|
|
patterns := strings.Split(strings.ReplaceAll(f.StatusCheckContexts, "\r", "\n"), "\n")
|
|
|
|
validPatterns := make([]string, 0, len(patterns))
|
|
|
|
for _, pattern := range patterns {
|
|
|
|
trimmed := strings.TrimSpace(pattern)
|
|
|
|
if trimmed == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, err := glob.Compile(trimmed); err != nil {
|
|
|
|
ctx.Flash.Error(ctx.Tr("repo.settings.protect_invalid_status_check_pattern", pattern))
|
|
|
|
ctx.Redirect(fmt.Sprintf("%s/settings/branches/edit?rule_name=%s", ctx.Repo.RepoLink, url.QueryEscape(protectBranch.RuleName)))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
validPatterns = append(validPatterns, trimmed)
|
|
|
|
}
|
|
|
|
if len(validPatterns) == 0 {
|
|
|
|
// if status check is enabled, patterns slice is not allowed to be empty
|
|
|
|
ctx.Flash.Error(ctx.Tr("repo.settings.protect_no_valid_status_check_patterns"))
|
|
|
|
ctx.Redirect(fmt.Sprintf("%s/settings/branches/edit?rule_name=%s", ctx.Repo.RepoLink, url.QueryEscape(protectBranch.RuleName)))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
protectBranch.StatusCheckContexts = validPatterns
|
2023-01-16 03:00:22 -05:00
|
|
|
} else {
|
|
|
|
protectBranch.StatusCheckContexts = nil
|
|
|
|
}
|
2019-09-18 01:39:45 -04:00
|
|
|
|
2023-01-16 03:00:22 -05:00
|
|
|
protectBranch.RequiredApprovals = f.RequiredApprovals
|
|
|
|
protectBranch.EnableApprovalsWhitelist = f.EnableApprovalsWhitelist
|
|
|
|
if f.EnableApprovalsWhitelist {
|
|
|
|
if strings.TrimSpace(f.ApprovalsWhitelistUsers) != "" {
|
|
|
|
approvalsWhitelistUsers, _ = base.StringsToInt64s(strings.Split(f.ApprovalsWhitelistUsers, ","))
|
2018-12-11 06:28:37 -05:00
|
|
|
}
|
2023-01-16 03:00:22 -05:00
|
|
|
if strings.TrimSpace(f.ApprovalsWhitelistTeams) != "" {
|
|
|
|
approvalsWhitelistTeams, _ = base.StringsToInt64s(strings.Split(f.ApprovalsWhitelistTeams, ","))
|
2017-09-14 04:16:22 -04:00
|
|
|
}
|
2023-01-16 03:00:22 -05:00
|
|
|
}
|
|
|
|
protectBranch.BlockOnRejectedReviews = f.BlockOnRejectedReviews
|
|
|
|
protectBranch.BlockOnOfficialReviewRequests = f.BlockOnOfficialReviewRequests
|
|
|
|
protectBranch.DismissStaleApprovals = f.DismissStaleApprovals
|
2024-01-15 02:20:01 -05:00
|
|
|
protectBranch.IgnoreStaleApprovals = f.IgnoreStaleApprovals
|
2023-01-16 03:00:22 -05:00
|
|
|
protectBranch.RequireSignedCommits = f.RequireSignedCommits
|
|
|
|
protectBranch.ProtectedFilePatterns = f.ProtectedFilePatterns
|
|
|
|
protectBranch.UnprotectedFilePatterns = f.UnprotectedFilePatterns
|
|
|
|
protectBranch.BlockOnOutdatedBranch = f.BlockOnOutdatedBranch
|
2024-03-28 16:41:52 -04:00
|
|
|
protectBranch.ApplyToAdmins = f.ApplyToAdmins
|
2023-01-16 03:00:22 -05:00
|
|
|
|
|
|
|
err = git_model.UpdateProtectBranch(ctx, ctx.Repo.Repository, protectBranch, git_model.WhitelistOptions{
|
|
|
|
UserIDs: whitelistUsers,
|
|
|
|
TeamIDs: whitelistTeams,
|
|
|
|
MergeUserIDs: mergeWhitelistUsers,
|
|
|
|
MergeTeamIDs: mergeWhitelistTeams,
|
|
|
|
ApprovalsUserIDs: approvalsWhitelistUsers,
|
|
|
|
ApprovalsTeamIDs: approvalsWhitelistTeams,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("UpdateProtectBranch", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: since we only need to recheck files protected rules, we could improve this
|
2023-06-29 06:03:20 -04:00
|
|
|
matchedBranches, err := git_model.FindAllMatchedBranches(ctx, ctx.Repo.Repository.ID, protectBranch.RuleName)
|
2023-01-16 03:00:22 -05:00
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("FindAllMatchedBranches", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, branchName := range matchedBranches {
|
2023-07-22 10:14:27 -04:00
|
|
|
if err = pull_service.CheckPRsForBaseBranch(ctx, ctx.Repo.Repository, branchName); err != nil {
|
2023-01-16 03:00:22 -05:00
|
|
|
ctx.ServerError("CheckPRsForBaseBranch", err)
|
2020-10-13 14:50:57 -04:00
|
|
|
return
|
|
|
|
}
|
2017-09-14 04:16:22 -04:00
|
|
|
}
|
2023-01-16 03:00:22 -05:00
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.update_protect_branch_success", protectBranch.RuleName))
|
|
|
|
ctx.Redirect(fmt.Sprintf("%s/settings/branches?rule_name=%s", ctx.Repo.RepoLink, protectBranch.RuleName))
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteProtectedBranchRulePost delete protected branch rule by id
|
|
|
|
func DeleteProtectedBranchRulePost(ctx *context.Context) {
|
|
|
|
ruleID := ctx.ParamsInt64("id")
|
|
|
|
if ruleID <= 0 {
|
|
|
|
ctx.Flash.Error(ctx.Tr("repo.settings.remove_protected_branch_failed", fmt.Sprintf("%d", ruleID)))
|
2023-07-26 02:04:01 -04:00
|
|
|
ctx.JSONRedirect(fmt.Sprintf("%s/settings/branches", ctx.Repo.RepoLink))
|
2023-01-16 03:00:22 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rule, err := git_model.GetProtectedBranchRuleByID(ctx, ctx.Repo.Repository.ID, ruleID)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Flash.Error(ctx.Tr("repo.settings.remove_protected_branch_failed", fmt.Sprintf("%d", ruleID)))
|
2023-07-26 02:04:01 -04:00
|
|
|
ctx.JSONRedirect(fmt.Sprintf("%s/settings/branches", ctx.Repo.RepoLink))
|
2023-01-16 03:00:22 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if rule == nil {
|
|
|
|
ctx.Flash.Error(ctx.Tr("repo.settings.remove_protected_branch_failed", fmt.Sprintf("%d", ruleID)))
|
2023-07-26 02:04:01 -04:00
|
|
|
ctx.JSONRedirect(fmt.Sprintf("%s/settings/branches", ctx.Repo.RepoLink))
|
2023-01-16 03:00:22 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-21 19:43:29 -04:00
|
|
|
if err := git_model.DeleteProtectedBranch(ctx, ctx.Repo.Repository, ruleID); err != nil {
|
2023-01-16 03:00:22 -05:00
|
|
|
ctx.Flash.Error(ctx.Tr("repo.settings.remove_protected_branch_failed", rule.RuleName))
|
2023-07-26 02:04:01 -04:00
|
|
|
ctx.JSONRedirect(fmt.Sprintf("%s/settings/branches", ctx.Repo.RepoLink))
|
2023-01-16 03:00:22 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.remove_protected_branch_success", rule.RuleName))
|
2023-07-26 02:04:01 -04:00
|
|
|
ctx.JSONRedirect(fmt.Sprintf("%s/settings/branches", ctx.Repo.RepoLink))
|
2017-09-14 04:16:22 -04:00
|
|
|
}
|
2021-10-08 13:03:04 -04:00
|
|
|
|
|
|
|
// RenameBranchPost responses for rename a branch
|
|
|
|
func RenameBranchPost(ctx *context.Context) {
|
|
|
|
form := web.GetForm(ctx).(*forms.RenameBranchForm)
|
|
|
|
|
|
|
|
if !ctx.Repo.CanCreateBranch() {
|
|
|
|
ctx.NotFound("RenameBranch", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
|
|
|
ctx.Flash.Error(ctx.GetErrMsg())
|
2023-04-30 11:08:51 -04:00
|
|
|
ctx.Redirect(fmt.Sprintf("%s/branches", ctx.Repo.RepoLink))
|
2021-10-08 13:03:04 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-28 17:17:51 -05:00
|
|
|
msg, err := repository.RenameBranch(ctx, ctx.Repo.Repository, ctx.Doer, ctx.Repo.GitRepo, form.From, form.To)
|
2021-10-08 13:03:04 -04:00
|
|
|
if err != nil {
|
2024-03-25 20:39:15 -04:00
|
|
|
if errors.Is(err, git_model.ErrBranchIsProtected) {
|
|
|
|
ctx.Flash.Error(ctx.Tr("repo.settings.rename_branch_failed_protected", form.To))
|
|
|
|
ctx.Redirect(fmt.Sprintf("%s/branches", ctx.Repo.RepoLink))
|
2024-04-15 10:03:09 -04:00
|
|
|
} else if git_model.IsErrBranchAlreadyExists(err) {
|
|
|
|
ctx.Flash.Error(ctx.Tr("repo.branch.branch_already_exists", form.To))
|
|
|
|
ctx.Redirect(fmt.Sprintf("%s/branches", ctx.Repo.RepoLink))
|
2024-03-25 20:39:15 -04:00
|
|
|
} else {
|
|
|
|
ctx.ServerError("RenameBranch", err)
|
|
|
|
}
|
2021-10-08 13:03:04 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg == "target_exist" {
|
|
|
|
ctx.Flash.Error(ctx.Tr("repo.settings.rename_branch_failed_exist", form.To))
|
2023-04-30 11:08:51 -04:00
|
|
|
ctx.Redirect(fmt.Sprintf("%s/branches", ctx.Repo.RepoLink))
|
2021-10-08 13:03:04 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg == "from_not_exist" {
|
|
|
|
ctx.Flash.Error(ctx.Tr("repo.settings.rename_branch_failed_not_exist", form.From))
|
2023-04-30 11:08:51 -04:00
|
|
|
ctx.Redirect(fmt.Sprintf("%s/branches", ctx.Repo.RepoLink))
|
2021-10-08 13:03:04 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.rename_branch_success", form.From, form.To))
|
2023-04-30 11:08:51 -04:00
|
|
|
ctx.Redirect(fmt.Sprintf("%s/branches", ctx.Repo.RepoLink))
|
2021-10-08 13:03:04 -04:00
|
|
|
}
|