Skip to content

Commit 2bb8b3a

Browse files
lunnyappleboy
authored andcommitted
fix bug when push a branch name with / & fix an integration test bug (#1689)
1 parent 7949404 commit 2bb8b3a

File tree

4 files changed

+53
-7
lines changed

4 files changed

+53
-7
lines changed

integrations/integration_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,12 @@ func initIntegrationTest() {
9494
if err != nil {
9595
log.Fatalf("db.Query: %v", err)
9696
}
97-
if rows.Next() {
98-
break // database already exists
99-
}
100-
if _, err = db.Exec("CREATE DATABASE testgitea"); err != nil {
101-
log.Fatalf("db.Exec: %v", err)
97+
defer rows.Close()
98+
99+
if !rows.Next() {
100+
if _, err = db.Exec("CREATE DATABASE testgitea"); err != nil {
101+
log.Fatalf("db.Exec: %v", err)
102+
}
102103
}
103104
}
104105
routers.GlobalInit()

integrations/internal_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2017 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package integrations
6+
7+
import (
8+
"encoding/json"
9+
"fmt"
10+
"net/http"
11+
"net/url"
12+
"testing"
13+
14+
"code.gitea.io/gitea/models"
15+
"code.gitea.io/gitea/modules/setting"
16+
17+
"github.com/stretchr/testify/assert"
18+
)
19+
20+
func assertProtectedBranch(t *testing.T, repoID int64, branchName string, isErr, canPush bool) {
21+
reqURL := fmt.Sprintf("/api/internal/branch/%d/%s", repoID, url.QueryEscape(branchName))
22+
req, err := http.NewRequest("GET", reqURL, nil)
23+
t.Log(reqURL)
24+
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", setting.InternalToken))
25+
26+
assert.NoError(t, err)
27+
resp := MakeRequest(req)
28+
if isErr {
29+
assert.EqualValues(t, 500, resp.HeaderCode)
30+
} else {
31+
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
32+
var branch models.ProtectedBranch
33+
t.Log(string(resp.Body))
34+
assert.NoError(t, json.Unmarshal(resp.Body, &branch))
35+
assert.Equal(t, canPush, branch.CanPush)
36+
}
37+
}
38+
39+
func TestInternal_GetProtectedBranch(t *testing.T) {
40+
prepareTestEnv(t)
41+
42+
assertProtectedBranch(t, 1, "master", false, true)
43+
assertProtectedBranch(t, 1, "dev", false, true)
44+
assertProtectedBranch(t, 1, "lunny/dev", false, true)
45+
}

routers/private/branch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
// GetProtectedBranchBy get protected branch information
1414
func GetProtectedBranchBy(ctx *macaron.Context) {
1515
repoID := ctx.ParamsInt64(":id")
16-
branchName := ctx.Params(":branch")
16+
branchName := ctx.Params("*")
1717
protectBranch, err := models.GetProtectedBranchBy(repoID, branchName)
1818
if err != nil {
1919
ctx.JSON(500, map[string]interface{}{

routers/private/internal.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@ func RegisterRoutes(m *macaron.Macaron) {
4242
m.Group("/", func() {
4343
m.Post("/ssh/:id/update", UpdatePublicKey)
4444
m.Post("/push/update", PushUpdate)
45-
m.Get("/branch/:id/:branch", GetProtectedBranchBy)
45+
m.Get("/branch/:id/*", GetProtectedBranchBy)
4646
}, CheckInternalToken)
4747
}

0 commit comments

Comments
 (0)