Skip to content

Commit 865dd24

Browse files
committed
Fix branches/_new endpoints, and update integration test
1 parent 7794a97 commit 865dd24

File tree

3 files changed

+64
-60
lines changed

3 files changed

+64
-60
lines changed

integrations/repo_branch_test.go

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ import (
1414
"github.com/stretchr/testify/assert"
1515
)
1616

17-
func testCreateBranch(t *testing.T, session *TestSession, user, repo, oldRefName, newBranchName string, expectedStatus int) string {
17+
func testCreateBranch(t *testing.T, session *TestSession, user, repo, oldRefSubURL, newBranchName string, expectedStatus int) string {
1818
var csrf string
1919
if expectedStatus == http.StatusNotFound {
20-
csrf = GetCSRF(t, session, path.Join(user, repo, "src/master"))
20+
csrf = GetCSRF(t, session, path.Join(user, repo, "src/branch/master"))
2121
} else {
22-
csrf = GetCSRF(t, session, path.Join(user, repo, "src", oldRefName))
22+
csrf = GetCSRF(t, session, path.Join(user, repo, "src", oldRefSubURL))
2323
}
24-
req := NewRequestWithValues(t, "POST", path.Join(user, repo, "branches/_new", oldRefName), map[string]string{
24+
req := NewRequestWithValues(t, "POST", path.Join(user, repo, "branches/_new", oldRefSubURL), map[string]string{
2525
"_csrf": csrf,
2626
"new_branch_name": newBranchName,
2727
})
@@ -34,72 +34,72 @@ func testCreateBranch(t *testing.T, session *TestSession, user, repo, oldRefName
3434

3535
func TestCreateBranch(t *testing.T) {
3636
tests := []struct {
37-
OldBranchOrCommit string
38-
NewBranch string
39-
CreateRelease string
40-
FlashMessage string
41-
ExpectedStatus int
37+
OldRefSubURL string
38+
NewBranch string
39+
CreateRelease string
40+
FlashMessage string
41+
ExpectedStatus int
4242
}{
4343
{
44-
OldBranchOrCommit: "master",
45-
NewBranch: "feature/test1",
46-
ExpectedStatus: http.StatusFound,
47-
FlashMessage: i18n.Tr("en", "repo.branch.create_success", "feature/test1"),
44+
OldRefSubURL: "branch/master",
45+
NewBranch: "feature/test1",
46+
ExpectedStatus: http.StatusFound,
47+
FlashMessage: i18n.Tr("en", "repo.branch.create_success", "feature/test1"),
4848
},
4949
{
50-
OldBranchOrCommit: "master",
51-
NewBranch: "",
52-
ExpectedStatus: http.StatusFound,
53-
FlashMessage: i18n.Tr("en", "form.NewBranchName") + i18n.Tr("en", "form.require_error"),
50+
OldRefSubURL: "branch/master",
51+
NewBranch: "",
52+
ExpectedStatus: http.StatusFound,
53+
FlashMessage: i18n.Tr("en", "form.NewBranchName") + i18n.Tr("en", "form.require_error"),
5454
},
5555
{
56-
OldBranchOrCommit: "master",
57-
NewBranch: "feature=test1",
58-
ExpectedStatus: http.StatusFound,
59-
FlashMessage: i18n.Tr("en", "form.NewBranchName") + i18n.Tr("en", "form.git_ref_name_error"),
56+
OldRefSubURL: "branch/master",
57+
NewBranch: "feature=test1",
58+
ExpectedStatus: http.StatusFound,
59+
FlashMessage: i18n.Tr("en", "form.NewBranchName") + i18n.Tr("en", "form.git_ref_name_error"),
6060
},
6161
{
62-
OldBranchOrCommit: "master",
63-
NewBranch: strings.Repeat("b", 101),
64-
ExpectedStatus: http.StatusFound,
65-
FlashMessage: i18n.Tr("en", "form.NewBranchName") + i18n.Tr("en", "form.max_size_error", "100"),
62+
OldRefSubURL: "branch/master",
63+
NewBranch: strings.Repeat("b", 101),
64+
ExpectedStatus: http.StatusFound,
65+
FlashMessage: i18n.Tr("en", "form.NewBranchName") + i18n.Tr("en", "form.max_size_error", "100"),
6666
},
6767
{
68-
OldBranchOrCommit: "master",
69-
NewBranch: "master",
70-
ExpectedStatus: http.StatusFound,
71-
FlashMessage: i18n.Tr("en", "repo.branch.branch_already_exists", "master"),
68+
OldRefSubURL: "branch/master",
69+
NewBranch: "master",
70+
ExpectedStatus: http.StatusFound,
71+
FlashMessage: i18n.Tr("en", "repo.branch.branch_already_exists", "master"),
7272
},
7373
{
74-
OldBranchOrCommit: "master",
75-
NewBranch: "master/test",
76-
ExpectedStatus: http.StatusFound,
77-
FlashMessage: i18n.Tr("en", "repo.branch.branch_name_conflict", "master/test", "master"),
74+
OldRefSubURL: "branch/master",
75+
NewBranch: "master/test",
76+
ExpectedStatus: http.StatusFound,
77+
FlashMessage: i18n.Tr("en", "repo.branch.branch_name_conflict", "master/test", "master"),
7878
},
7979
{
80-
OldBranchOrCommit: "acd1d892867872cb47f3993468605b8aa59aa2e0",
81-
NewBranch: "feature/test2",
82-
ExpectedStatus: http.StatusNotFound,
80+
OldRefSubURL: "commit/acd1d892867872cb47f3993468605b8aa59aa2e0",
81+
NewBranch: "feature/test2",
82+
ExpectedStatus: http.StatusNotFound,
8383
},
8484
{
85-
OldBranchOrCommit: "65f1bf27bc3bf70f64657658635e66094edbcb4d",
86-
NewBranch: "feature/test3",
87-
ExpectedStatus: http.StatusFound,
88-
FlashMessage: i18n.Tr("en", "repo.branch.create_success", "feature/test3"),
85+
OldRefSubURL: "commit/65f1bf27bc3bf70f64657658635e66094edbcb4d",
86+
NewBranch: "feature/test3",
87+
ExpectedStatus: http.StatusFound,
88+
FlashMessage: i18n.Tr("en", "repo.branch.create_success", "feature/test3"),
8989
},
9090
{
91-
OldBranchOrCommit: "master",
92-
NewBranch: "v1.0.0",
93-
CreateRelease: "v1.0.0",
94-
ExpectedStatus: http.StatusFound,
95-
FlashMessage: i18n.Tr("en", "repo.branch.tag_collision", "v1.0.0"),
91+
OldRefSubURL: "branch/master",
92+
NewBranch: "v1.0.0",
93+
CreateRelease: "v1.0.0",
94+
ExpectedStatus: http.StatusFound,
95+
FlashMessage: i18n.Tr("en", "repo.branch.tag_collision", "v1.0.0"),
9696
},
9797
{
98-
OldBranchOrCommit: "v1.0.0",
99-
NewBranch: "feature/test4",
100-
CreateRelease: "v1.0.0",
101-
ExpectedStatus: http.StatusFound,
102-
FlashMessage: i18n.Tr("en", "repo.branch.create_success", "feature/test4"),
98+
OldRefSubURL: "tag/v1.0.0",
99+
NewBranch: "feature/test4",
100+
CreateRelease: "v1.0.0",
101+
ExpectedStatus: http.StatusFound,
102+
FlashMessage: i18n.Tr("en", "repo.branch.create_success", "feature/test4"),
103103
},
104104
}
105105
for _, test := range tests {
@@ -108,7 +108,7 @@ func TestCreateBranch(t *testing.T) {
108108
if test.CreateRelease != "" {
109109
createNewRelease(t, session, "/user2/repo1", test.CreateRelease, test.CreateRelease, false, false)
110110
}
111-
redirectURL := testCreateBranch(t, session, "user2", "repo1", test.OldBranchOrCommit, test.NewBranch, test.ExpectedStatus)
111+
redirectURL := testCreateBranch(t, session, "user2", "repo1", test.OldRefSubURL, test.NewBranch, test.ExpectedStatus)
112112
if test.ExpectedStatus == http.StatusFound {
113113
req := NewRequest(t, "GET", redirectURL)
114114
resp := session.MakeRequest(t, req, http.StatusOK)
@@ -124,7 +124,7 @@ func TestCreateBranch(t *testing.T) {
124124
func TestCreateBranchInvalidCSRF(t *testing.T) {
125125
prepareTestEnv(t)
126126
session := loginUser(t, "user2")
127-
req := NewRequestWithValues(t, "POST", "user2/repo1/branches/_new/master", map[string]string{
127+
req := NewRequestWithValues(t, "POST", "user2/repo1/branches/_new/branch/master", map[string]string{
128128
"_csrf": "fake_csrf",
129129
"new_branch_name": "test",
130130
})

modules/context/repo.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -463,9 +463,9 @@ func RepoAssignment() macaron.Handler {
463463
type RepoRefType int
464464

465465
const (
466-
// RepoRefUnknown unknown type, make educated guess and redirect.
466+
// RepoRefLegacy unknown type, make educated guess and redirect.
467467
// for backward compatibility with previous URL scheme
468-
RepoRefUnknown RepoRefType = iota
468+
RepoRefLegacy RepoRefType = iota
469469
// RepoRefBranch branch
470470
RepoRefBranch
471471
// RepoRefTag tag
@@ -497,7 +497,7 @@ func getRefNameFromPath(ctx *Context, path string, isExist func(string) bool) st
497497
func getRefName(ctx *Context, pathType RepoRefType) string {
498498
path := ctx.Params("*")
499499
switch pathType {
500-
case RepoRefUnknown:
500+
case RepoRefLegacy:
501501
if refName := getRefName(ctx, RepoRefBranch); len(refName) > 0 {
502502
return refName
503503
}
@@ -615,7 +615,7 @@ func RepoRefByType(refType RepoRefType) macaron.Handler {
615615
return
616616
}
617617

618-
if refType == RepoRefUnknown {
618+
if refType == RepoRefLegacy {
619619
// redirect from old URL scheme to new URL scheme
620620
ctx.Redirect(repoRefRedirect(ctx))
621621
return

routers/routes/routes.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,11 @@ func RegisterRoutes(m *macaron.Macaron) {
541541
}, repo.MustBeNotBare, reqRepoWriter)
542542

543543
m.Group("/branches", func() {
544-
m.Post("/_new/*", context.RepoRef(), bindIgnErr(auth.NewBranchForm{}), repo.CreateBranch)
544+
m.Group("/_new/", func() {
545+
m.Post("/branch/*", context.RepoRefByType(context.RepoRefBranch), repo.CreateBranch)
546+
m.Post("/tag/*", context.RepoRefByType(context.RepoRefTag), repo.CreateBranch)
547+
m.Post("/commit/*", context.RepoRefByType(context.RepoRefCommit), repo.CreateBranch)
548+
}, bindIgnErr(auth.NewBranchForm{}))
545549
m.Post("/delete", repo.DeleteBranchPost)
546550
m.Post("/restore", repo.RestoreBranchPost)
547551
}, reqRepoWriter, repo.MustBeNotBare, context.CheckUnit(models.UnitTypeCode))
@@ -626,15 +630,15 @@ func RegisterRoutes(m *macaron.Macaron) {
626630
m.Get("/tag/*", context.RepoRefByType(context.RepoRefTag), repo.SingleDownload)
627631
m.Get("/commit/*", context.RepoRefByType(context.RepoRefCommit), repo.SingleDownload)
628632
// "/*" route is deprecated, and kept for backward compatibility
629-
m.Get("/*", context.RepoRefByType(context.RepoRefUnknown), repo.SingleDownload)
633+
m.Get("/*", context.RepoRefByType(context.RepoRefLegacy), repo.SingleDownload)
630634
}, repo.MustBeNotBare, context.CheckUnit(models.UnitTypeCode))
631635

632636
m.Group("/commits", func() {
633637
m.Get("/branch/*", context.RepoRefByType(context.RepoRefBranch), repo.RefCommits)
634638
m.Get("/tag/*", context.RepoRefByType(context.RepoRefTag), repo.RefCommits)
635639
m.Get("/commit/*", context.RepoRefByType(context.RepoRefCommit), repo.RefCommits)
636640
// "/*" route is deprecated, and kept for backward compatibility
637-
m.Get("/*", context.RepoRefByType(context.RepoRefUnknown), repo.RefCommits)
641+
m.Get("/*", context.RepoRefByType(context.RepoRefLegacy), repo.RefCommits)
638642
}, repo.MustBeNotBare, context.CheckUnit(models.UnitTypeCode))
639643

640644
m.Group("", func() {
@@ -647,7 +651,7 @@ func RegisterRoutes(m *macaron.Macaron) {
647651
m.Get("/tag/*", context.RepoRefByType(context.RepoRefTag), repo.Home)
648652
m.Get("/commit/*", context.RepoRefByType(context.RepoRefCommit), repo.Home)
649653
// "/*" route is deprecated, and kept for backward compatibility
650-
m.Get("/*", context.RepoRefByType(context.RepoRefUnknown), repo.Home)
654+
m.Get("/*", context.RepoRefByType(context.RepoRefLegacy), repo.Home)
651655
}, repo.SetEditorconfigIfExists)
652656

653657
m.Group("", func() {

0 commit comments

Comments
 (0)