2017-09-14 08:16:22 +00:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-09-14 08:16:22 +00:00
|
|
|
|
2023-07-02 00:59:32 +00:00
|
|
|
package setting
|
2017-09-14 08:16:22 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-04-05 15:30:52 +00:00
|
|
|
"net/http"
|
2023-05-17 08:11:13 +00:00
|
|
|
"net/url"
|
2017-09-14 08:16:22 +00:00
|
|
|
"strings"
|
2019-09-18 05:39:45 +00:00
|
|
|
"time"
|
2017-09-14 08:16:22 +00:00
|
|
|
|
2022-06-12 15:51:54 +00:00
|
|
|
git_model "code.gitea.io/gitea/models/git"
|
2022-03-29 06:29:02 +00:00
|
|
|
"code.gitea.io/gitea/models/organization"
|
2021-11-28 11:58:28 +00:00
|
|
|
"code.gitea.io/gitea/models/perm"
|
2022-05-11 10:09:36 +00:00
|
|
|
access_model "code.gitea.io/gitea/models/perm/access"
|
2017-09-14 08:16:22 +00:00
|
|
|
"code.gitea.io/gitea/modules/base"
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
2021-01-26 15:36:53 +00:00
|
|
|
"code.gitea.io/gitea/modules/web"
|
2023-07-21 11:20:04 +00:00
|
|
|
"code.gitea.io/gitea/routers/web/repo"
|
2021-04-06 19:44:05 +00:00
|
|
|
"code.gitea.io/gitea/services/forms"
|
2020-10-13 18:50:57 +00:00
|
|
|
pull_service "code.gitea.io/gitea/services/pull"
|
2021-10-08 17:03:04 +00:00
|
|
|
"code.gitea.io/gitea/services/repository"
|
2023-05-17 08:11:13 +00:00
|
|
|
|
|
|
|
"github.com/gobwas/glob"
|
2017-09-14 08:16:22 +00:00
|
|
|
)
|
|
|
|
|
2023-01-16 08:00:22 +00: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 22:56:10 +00:00
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings.branches")
|
2017-09-14 08:16:22 +00:00
|
|
|
ctx.Data["PageIsSettingsBranches"] = true
|
|
|
|
|
2023-01-16 08:00:22 +00:00
|
|
|
rules, err := git_model.FindRepoProtectedBranchRules(ctx, ctx.Repo.Repository.ID)
|
2017-09-14 08:16:22 +00:00
|
|
|
if err != nil {
|
2018-01-10 21:34:17 +00:00
|
|
|
ctx.ServerError("GetProtectedBranches", err)
|
2017-09-14 08:16:22 +00:00
|
|
|
return
|
|
|
|
}
|
2023-01-16 08:00:22 +00:00
|
|
|
ctx.Data["ProtectedBranches"] = rules
|
2017-09-14 08:16:22 +00:00
|
|
|
|
2023-07-21 11:20:04 +00:00
|
|
|
repo.PrepareBranchList(ctx)
|
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-05 15:30:52 +00:00
|
|
|
ctx.HTML(http.StatusOK, tplBranches)
|
2017-09-14 08:16:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SettingsProtectedBranch renders the protected branch setting page
|
|
|
|
func SettingsProtectedBranch(c *context.Context) {
|
2023-01-16 08:00:22 +00: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 21:34:17 +00:00
|
|
|
c.ServerError("GetProtectBranchOfRepoByName", err)
|
2017-09-14 08:16:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-16 08:00:22 +00:00
|
|
|
if rule == nil {
|
2017-09-14 08:16:22 +00:00
|
|
|
// No options found, create defaults.
|
2023-01-16 08:00:22 +00:00
|
|
|
rule = &git_model.ProtectedBranch{}
|
2017-09-14 08:16:22 +00:00
|
|
|
}
|
|
|
|
|
2023-01-16 08:00:22 +00:00
|
|
|
c.Data["PageIsSettingsBranches"] = true
|
2024-02-14 21:48:45 +00:00
|
|
|
c.Data["Title"] = c.Locale.TrString("repo.settings.protected_branch") + " - " + rule.RuleName
|
2023-01-16 08:00:22 +00:00
|
|
|
|
2023-10-03 10:30:41 +00:00
|
|
|
users, err := access_model.GetRepoReaders(c, c.Repo.Repository)
|
2017-09-14 08:16:22 +00:00
|
|
|
if err != nil {
|
2019-10-08 19:18:17 +00:00
|
|
|
c.ServerError("Repo.Repository.GetReaders", err)
|
2017-09-14 08:16:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Data["Users"] = users
|
2023-01-16 08:00:22 +00: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 08:11:13 +00:00
|
|
|
c.Data["status_check_contexts"] = strings.Join(rule.StatusCheckContexts, "\n")
|
2023-01-09 03:50:54 +00:00
|
|
|
contexts, _ := git_model.FindRepoRecentCommitStatusContexts(c, c.Repo.Repository.ID, 7*24*time.Hour) // Find last week status check contexts
|
2023-05-17 08:11:13 +00:00
|
|
|
c.Data["recent_status_checks"] = contexts
|
2019-09-18 05:39:45 +00:00
|
|
|
|
2017-09-14 08:16:22 +00:00
|
|
|
if c.Repo.Owner.IsOrganization() {
|
2023-10-03 10:30:41 +00:00
|
|
|
teams, err := organization.OrgFromUser(c.Repo.Owner).TeamsWithAccessToRepo(c, c.Repo.Repository.ID, perm.AccessModeRead)
|
2017-09-14 08:16:22 +00:00
|
|
|
if err != nil {
|
2018-01-10 21:34:17 +00:00
|
|
|
c.ServerError("Repo.Owner.TeamsWithAccessToRepo", err)
|
2017-09-14 08:16:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Data["Teams"] = teams
|
2023-01-16 08:00:22 +00: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 08:16:22 +00:00
|
|
|
}
|
|
|
|
|
2023-01-16 08:00:22 +00:00
|
|
|
c.Data["Rule"] = rule
|
2021-04-05 15:30:52 +00:00
|
|
|
c.HTML(http.StatusOK, tplProtectedBranch)
|
2017-09-14 08:16:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SettingsProtectedBranchPost updates the protected branch settings
|
2021-01-26 15:36:53 +00:00
|
|
|
func SettingsProtectedBranchPost(ctx *context.Context) {
|
2021-04-06 19:44:05 +00:00
|
|
|
f := web.GetForm(ctx).(*forms.ProtectBranchForm)
|
2023-01-16 08:00:22 +00: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 08:16:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-16 08:00:22 +00:00
|
|
|
var err error
|
2023-02-20 11:30:41 +00: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 18:50:46 +00:00
|
|
|
// Check if a rule already exists with this rulename, if so redirect to it.
|
2023-02-20 11:30:41 +00:00
|
|
|
protectBranch, err = git_model.GetProtectedBranchRuleByName(ctx, ctx.Repo.Repository.ID, f.RuleName)
|
|
|
|
if err != nil {
|
2024-02-25 18:50:46 +00: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 11:30:41 +00:00
|
|
|
return
|
|
|
|
}
|
2023-01-16 08:00:22 +00:00
|
|
|
}
|
|
|
|
if protectBranch == nil {
|
|
|
|
// No options found, create defaults.
|
|
|
|
protectBranch = &git_model.ProtectedBranch{
|
|
|
|
RepoID: ctx.Repo.Repository.ID,
|
|
|
|
RuleName: f.RuleName,
|
2017-09-14 08:16:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-16 08:00:22 +00: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 08:16:22 +00:00
|
|
|
}
|
2023-01-16 08:00:22 +00:00
|
|
|
if strings.TrimSpace(f.WhitelistTeams) != "" {
|
|
|
|
whitelistTeams, _ = base.StringsToInt64s(strings.Split(f.WhitelistTeams, ","))
|
2018-12-11 11:28:37 +00:00
|
|
|
}
|
2023-01-16 08:00:22 +00:00
|
|
|
default:
|
|
|
|
protectBranch.CanPush = false
|
|
|
|
protectBranch.EnableWhitelist = false
|
|
|
|
protectBranch.WhitelistDeployKeys = false
|
|
|
|
}
|
2017-09-14 08:16:22 +00:00
|
|
|
|
2023-01-16 08:00:22 +00:00
|
|
|
protectBranch.EnableMergeWhitelist = f.EnableMergeWhitelist
|
|
|
|
if f.EnableMergeWhitelist {
|
|
|
|
if strings.TrimSpace(f.MergeWhitelistUsers) != "" {
|
|
|
|
mergeWhitelistUsers, _ = base.StringsToInt64s(strings.Split(f.MergeWhitelistUsers, ","))
|
2018-11-28 11:26:14 +00:00
|
|
|
}
|
2023-01-16 08:00:22 +00:00
|
|
|
if strings.TrimSpace(f.MergeWhitelistTeams) != "" {
|
|
|
|
mergeWhitelistTeams, _ = base.StringsToInt64s(strings.Split(f.MergeWhitelistTeams, ","))
|
2018-11-28 11:26:14 +00:00
|
|
|
}
|
2023-01-16 08:00:22 +00:00
|
|
|
}
|
2019-09-18 05:39:45 +00:00
|
|
|
|
2023-01-16 08:00:22 +00:00
|
|
|
protectBranch.EnableStatusCheck = f.EnableStatusCheck
|
|
|
|
if f.EnableStatusCheck {
|
2023-05-17 08:11:13 +00: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 08:00:22 +00:00
|
|
|
} else {
|
|
|
|
protectBranch.StatusCheckContexts = nil
|
|
|
|
}
|
2019-09-18 05:39:45 +00:00
|
|
|
|
2023-01-16 08:00:22 +00: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 11:28:37 +00:00
|
|
|
}
|
2023-01-16 08:00:22 +00:00
|
|
|
if strings.TrimSpace(f.ApprovalsWhitelistTeams) != "" {
|
|
|
|
approvalsWhitelistTeams, _ = base.StringsToInt64s(strings.Split(f.ApprovalsWhitelistTeams, ","))
|
2017-09-14 08:16:22 +00:00
|
|
|
}
|
2023-01-16 08:00:22 +00:00
|
|
|
}
|
|
|
|
protectBranch.BlockOnRejectedReviews = f.BlockOnRejectedReviews
|
|
|
|
protectBranch.BlockOnOfficialReviewRequests = f.BlockOnOfficialReviewRequests
|
|
|
|
protectBranch.DismissStaleApprovals = f.DismissStaleApprovals
|
2024-01-15 07:20:01 +00:00
|
|
|
protectBranch.IgnoreStaleApprovals = f.IgnoreStaleApprovals
|
2023-01-16 08:00:22 +00:00
|
|
|
protectBranch.RequireSignedCommits = f.RequireSignedCommits
|
|
|
|
protectBranch.ProtectedFilePatterns = f.ProtectedFilePatterns
|
|
|
|
protectBranch.UnprotectedFilePatterns = f.UnprotectedFilePatterns
|
|
|
|
protectBranch.BlockOnOutdatedBranch = f.BlockOnOutdatedBranch
|
|
|
|
|
|
|
|
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 10:03:20 +00:00
|
|
|
matchedBranches, err := git_model.FindAllMatchedBranches(ctx, ctx.Repo.Repository.ID, protectBranch.RuleName)
|
2023-01-16 08:00:22 +00:00
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("FindAllMatchedBranches", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, branchName := range matchedBranches {
|
2023-07-22 14:14:27 +00:00
|
|
|
if err = pull_service.CheckPRsForBaseBranch(ctx, ctx.Repo.Repository, branchName); err != nil {
|
2023-01-16 08:00:22 +00:00
|
|
|
ctx.ServerError("CheckPRsForBaseBranch", err)
|
2020-10-13 18:50:57 +00:00
|
|
|
return
|
|
|
|
}
|
2017-09-14 08:16:22 +00:00
|
|
|
}
|
2023-01-16 08:00:22 +00: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 06:04:01 +00:00
|
|
|
ctx.JSONRedirect(fmt.Sprintf("%s/settings/branches", ctx.Repo.RepoLink))
|
2023-01-16 08:00:22 +00: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 06:04:01 +00:00
|
|
|
ctx.JSONRedirect(fmt.Sprintf("%s/settings/branches", ctx.Repo.RepoLink))
|
2023-01-16 08:00:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if rule == nil {
|
|
|
|
ctx.Flash.Error(ctx.Tr("repo.settings.remove_protected_branch_failed", fmt.Sprintf("%d", ruleID)))
|
2023-07-26 06:04:01 +00:00
|
|
|
ctx.JSONRedirect(fmt.Sprintf("%s/settings/branches", ctx.Repo.RepoLink))
|
2023-01-16 08:00:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-21 23:43:29 +00:00
|
|
|
if err := git_model.DeleteProtectedBranch(ctx, ctx.Repo.Repository, ruleID); err != nil {
|
2023-01-16 08:00:22 +00:00
|
|
|
ctx.Flash.Error(ctx.Tr("repo.settings.remove_protected_branch_failed", rule.RuleName))
|
2023-07-26 06:04:01 +00:00
|
|
|
ctx.JSONRedirect(fmt.Sprintf("%s/settings/branches", ctx.Repo.RepoLink))
|
2023-01-16 08:00:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.remove_protected_branch_success", rule.RuleName))
|
2023-07-26 06:04:01 +00:00
|
|
|
ctx.JSONRedirect(fmt.Sprintf("%s/settings/branches", ctx.Repo.RepoLink))
|
2017-09-14 08:16:22 +00:00
|
|
|
}
|
2021-10-08 17:03:04 +00: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 15:08:51 +00:00
|
|
|
ctx.Redirect(fmt.Sprintf("%s/branches", ctx.Repo.RepoLink))
|
2021-10-08 17:03:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-28 22:17:51 +00:00
|
|
|
msg, err := repository.RenameBranch(ctx, ctx.Repo.Repository, ctx.Doer, ctx.Repo.GitRepo, form.From, form.To)
|
2021-10-08 17:03:04 +00:00
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("RenameBranch", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg == "target_exist" {
|
|
|
|
ctx.Flash.Error(ctx.Tr("repo.settings.rename_branch_failed_exist", form.To))
|
2023-04-30 15:08:51 +00:00
|
|
|
ctx.Redirect(fmt.Sprintf("%s/branches", ctx.Repo.RepoLink))
|
2021-10-08 17:03:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg == "from_not_exist" {
|
|
|
|
ctx.Flash.Error(ctx.Tr("repo.settings.rename_branch_failed_not_exist", form.From))
|
2023-04-30 15:08:51 +00:00
|
|
|
ctx.Redirect(fmt.Sprintf("%s/branches", ctx.Repo.RepoLink))
|
2021-10-08 17:03:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.rename_branch_success", form.From, form.To))
|
2023-04-30 15:08:51 +00:00
|
|
|
ctx.Redirect(fmt.Sprintf("%s/branches", ctx.Repo.RepoLink))
|
2021-10-08 17:03:04 +00:00
|
|
|
}
|