diff --git a/integrations/change_default_branch_test.go b/integrations/change_default_branch_test.go new file mode 100644 index 00000000000..03ece46a58f --- /dev/null +++ b/integrations/change_default_branch_test.go @@ -0,0 +1,58 @@ +// Copyright 2017 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package integrations + +import ( + "bytes" + "fmt" + "net/http" + "net/url" + "testing" + + "code.gitea.io/gitea/models" + + "github.com/stretchr/testify/assert" +) + +func TestChangeDefaultBranch(t *testing.T) { + prepareTestEnv(t) + repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) + + session := loginUser(t, owner.Name, "password") + branchesURL := fmt.Sprintf("/%s/%s/settings/branches", owner.Name, repo.Name) + + req := NewRequest(t, "GET", branchesURL) + resp := session.MakeRequest(t, req) + assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + doc, err := NewHtmlParser(resp.Body) + assert.NoError(t, err) + + req = NewRequestBody(t, "POST", branchesURL, + bytes.NewBufferString(url.Values{ + "_csrf": []string{doc.GetInputValueByName("_csrf")}, + "action": []string{"default_branch"}, + "branch": []string{"DefaultBranch"}, + }.Encode())) + req.Header.Add("Content-Type", "application/x-www-form-urlencoded") + resp = session.MakeRequest(t, req) + assert.EqualValues(t, http.StatusFound, resp.HeaderCode) + + req = NewRequest(t, "GET", branchesURL) + resp = session.MakeRequest(t, req) + assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + doc, err = NewHtmlParser(resp.Body) + assert.NoError(t, err) + + req = NewRequestBody(t, "POST", branchesURL, + bytes.NewBufferString(url.Values{ + "_csrf": []string{doc.GetInputValueByName("_csrf")}, + "action": []string{"default_branch"}, + "branch": []string{"does_not_exist"}, + }.Encode())) + req.Header.Add("Content-Type", "application/x-www-form-urlencoded") + resp = session.MakeRequest(t, req) + assert.EqualValues(t, http.StatusNotFound, resp.HeaderCode) +} diff --git a/integrations/gitea-integration-meta/gitea-repositories/user2/repo1.git/refs/heads/DefaultBranch b/integrations/gitea-integration-meta/gitea-repositories/user2/repo1.git/refs/heads/DefaultBranch new file mode 100644 index 00000000000..f98a263be62 --- /dev/null +++ b/integrations/gitea-integration-meta/gitea-repositories/user2/repo1.git/refs/heads/DefaultBranch @@ -0,0 +1 @@ +65f1bf27bc3bf70f64657658635e66094edbcb4d diff --git a/integrations/gitea-integration-meta/gitea-repositories/user2/repo1.git/refs/heads/develop b/integrations/gitea-integration-meta/gitea-repositories/user2/repo1.git/refs/heads/develop new file mode 100644 index 00000000000..f98a263be62 --- /dev/null +++ b/integrations/gitea-integration-meta/gitea-repositories/user2/repo1.git/refs/heads/develop @@ -0,0 +1 @@ +65f1bf27bc3bf70f64657658635e66094edbcb4d diff --git a/routers/repo/setting.go b/routers/repo/setting.go index b2cb73cf98f..a50c6a6e227 100644 --- a/routers/repo/setting.go +++ b/routers/repo/setting.go @@ -480,9 +480,11 @@ func ProtectedBranchPost(ctx *context.Context) { return } - branch := strings.ToLower(ctx.Query("branch")) - if ctx.Repo.GitRepo.IsBranchExist(branch) && - repo.DefaultBranch != branch { + branch := ctx.Query("branch") + if !ctx.Repo.GitRepo.IsBranchExist(branch) { + ctx.Status(404) + return + } else if repo.DefaultBranch != branch { repo.DefaultBranch = branch if err := ctx.Repo.GitRepo.SetDefaultBranch(branch); err != nil { if !git.IsErrUnsupportedVersion(err) {