This commit is contained in:
Lunny Xiao 2022-09-06 20:12:36 +08:00
parent 9740a6dbbb
commit b12ac622a7
No known key found for this signature in database
GPG key ID: C3B7C91B632F738A
6 changed files with 35 additions and 17 deletions

View file

@ -30,8 +30,8 @@ const (
tplProtectedBranch base.TplName = "repo/settings/protected_branch"
)
// ProtectedBranch render the page to protect the repository
func ProtectedBranch(ctx *context.Context) {
// ProtectedBranchRules render the page to protect the repository
func ProtectedBranchRules(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("repo.settings")
ctx.Data["PageIsSettingsBranches"] = true
@ -45,8 +45,8 @@ func ProtectedBranch(ctx *context.Context) {
ctx.HTML(http.StatusOK, tplBranches)
}
// ProtectedBranchPost response for protect for a branch of a repository
func ProtectedBranchPost(ctx *context.Context) {
// SetDefaultBranchPost set default branch
func SetDefaultBranchPost(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("repo.settings")
ctx.Data["PageIsSettingsBranches"] = true
@ -159,19 +159,20 @@ func SettingsProtectedBranch(c *context.Context) {
// SettingsProtectedBranchPost updates the protected branch settings
func SettingsProtectedBranchPost(ctx *context.Context) {
f := web.GetForm(ctx).(*forms.ProtectBranchForm)
ruleID := ctx.ParamsInt64("id")
var protectBranch *git_model.ProtectedBranch
if f.RuleID > 0 {
if ruleID > 0 {
var err error
protectBranch, err = git_model.GetProtectedBranchRuleByID(ctx, ctx.Repo.Repository.ID, f.RuleID)
protectBranch, err = git_model.GetProtectedBranchRuleByID(ctx, ctx.Repo.Repository.ID, ruleID)
if err != nil {
ctx.ServerError("GetProtectBranchOfRepoByName", err)
return
}
} else {
// No options found, create defaults.
protectBranch = &git_model.ProtectedBranch{
RepoID: ctx.Repo.Repository.ID,
RepoID: ctx.Repo.Repository.ID,
BranchName: f.RuleName,
}
}
@ -179,7 +180,7 @@ func SettingsProtectedBranchPost(ctx *context.Context) {
protectBranch.BranchName = 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/%d", ctx.Repo.RepoLink, f.RuleID))
ctx.Redirect(fmt.Sprintf("%s/settings/branches/%d", ctx.Repo.RepoLink, ruleID))
return
}
@ -259,6 +260,7 @@ func SettingsProtectedBranchPost(ctx *context.Context) {
ctx.Redirect(fmt.Sprintf("%s/settings/branches/%d", ctx.Repo.RepoLink, protectBranch.ID))
}
// DeleteProtectedBranchRulePost delete protected branch rule by id
func DeleteProtectedBranchRulePost(ctx *context.Context) {
ruleID := ctx.ParamsInt64("id")
if ruleID <= 0 {

View file

@ -763,12 +763,18 @@ func RegisterRoutes(m *web.Route) {
})
m.Group("/branches", func() {
m.Combo("").Get(repo.ProtectedBranch).Post(repo.ProtectedBranchPost)
m.Post("/", repo.SetDefaultBranchPost)
}, repo.MustBeNotEmpty)
m.Group("/branches", func() {
m.Get("/", repo.ProtectedBranchRules)
m.Combo("/new").Get(repo.SettingsProtectedBranch).
Post(bindIgnErr(forms.ProtectBranchForm{}), context.RepoMustNotBeArchived(), repo.SettingsProtectedBranchPost)
m.Get("/{id}/delete", repo.DeleteProtectedBranchRulePost)
m.Combo("/{id}").Get(repo.SettingsProtectedBranch).
Post(bindIgnErr(forms.ProtectBranchForm{}), context.RepoMustNotBeArchived(), repo.SettingsProtectedBranchPost)
}, repo.MustBeNotEmpty)
m.Post("/rename_branch", bindIgnErr(forms.RenameBranchForm{}), context.RepoMustNotBeArchived(), repo.RenameBranchPost)
m.Group("/tags", func() {

View file

@ -186,7 +186,6 @@ func (f *RepoSettingForm) Validate(req *http.Request, errs binding.Errors) bindi
// ProtectBranchForm form for changing protected branch settings
type ProtectBranchForm struct {
RuleID int64
RuleName string `binding:"Required"`
EnablePush string
WhitelistUsers string

View file

@ -59,7 +59,10 @@
{{range .ProtectedBranches}}
<tr>
<td><div class="ui basic primary label">{{.BranchName}}</div></td>
<td class="right aligned"><a class="rm ui button" href="{{$.Repository.Link}}/settings/branches/{{.ID}}">{{$.locale.Tr "repo.settings.edit_protected_branch"}}</a></td>
<td class="right aligned">
<a class="rm ui button" href="{{$.Repository.Link}}/settings/branches/{{.ID}}">{{$.locale.Tr "repo.settings.edit_protected_branch"}}</a>
<a class="ui red button" href="{{$.Repository.Link}}/settings/branches/{{.ID}}/delete">{{$.locale.Tr "repo.settings.protected_branch.delete_rule"}}</a>
</td>
</tr>
{{else}}
<tr class="center aligned"><td>{{.locale.Tr "repo.settings.no_protected_branch"}}</td></tr>

View file

@ -257,9 +257,8 @@
<div class="ui divider"></div>
<div class="field">
<button class="ui gray button">{{$.locale.Tr "cancel"}}</button>
<button class="ui green button">{{$.locale.Tr "repo.settings.protected_branch.save_rule"}}</button>
<button class="ui red button">{{$.locale.Tr "repo.settings.protected_branch.delete_rule"}}</button>
<button class="ui gray button">{{$.locale.Tr "cancel"}}</button>
</div>
</div>
</form>

View file

@ -77,11 +77,20 @@ func TestCreateFileOnProtectedBranch(t *testing.T) {
// remove the protected branch
csrf = GetCSRF(t, session, "/user2/repo1/settings/branches")
// Change master branch to protected
req = NewRequestWithValues(t, "POST", "/user2/repo1/settings/branches/master", map[string]string{
"_csrf": csrf,
"protected": "off",
req = NewRequestWithValues(t, "POST", "/user2/repo1/settings/branches/new", map[string]string{
"_csrf": csrf,
"rule_name": "master",
"enable_push": "true",
})
session.MakeRequest(t, req, http.StatusOK)
// Change master branch to protected
req = NewRequestWithValues(t, "POST", "/user2/repo1/settings/branches/1/delete", map[string]string{
"_csrf": csrf,
})
session.MakeRequest(t, req, http.StatusSeeOther)
// Check if master branch has been locked successfully
flashCookie = session.GetCookie("macaron_flash")