Skip to content

Commit 13ffa28

Browse files
authored
Fix bug of branches API with tests(#25578) (#25579)
Backport #25578 This PR added a repository's check when creating/deleting branches via API. Mirror repository and archive repository cannot do that.
1 parent e5b684e commit 13ffa28

26 files changed

+268
-4
lines changed

models/fixtures/mirror.yml

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
-
2+
id: 1
3+
repo_id: 5
4+
interval: 3600
5+
enable_prune: false
6+
updated_unix: 0
7+
next_update_unix: 0
8+
lfs_enabled: false
9+
lfs_endpoint: ""
10+
11+
-
12+
id: 2
13+
repo_id: 25
14+
interval: 3600
15+
enable_prune: false
16+
updated_unix: 0
17+
next_update_unix: 0
18+
lfs_enabled: false
19+
lfs_endpoint: ""
20+
21+
-
22+
id: 3
23+
repo_id: 26
24+
interval: 3600
25+
enable_prune: false
26+
updated_unix: 0
27+
next_update_unix: 0
28+
lfs_enabled: false
29+
lfs_endpoint: ""
30+
31+
-
32+
id: 4
33+
repo_id: 27
34+
interval: 3600
35+
enable_prune: false
36+
updated_unix: 0
37+
next_update_unix: 0
38+
lfs_enabled: false
39+
lfs_endpoint: ""
40+
41+
-
42+
id: 5
43+
repo_id: 28
44+
interval: 3600
45+
enable_prune: false
46+
updated_unix: 0
47+
next_update_unix: 0
48+
lfs_enabled: false
49+
lfs_endpoint: ""

models/fixtures/repository.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@
141141
num_projects: 0
142142
num_closed_projects: 0
143143
is_private: true
144-
is_empty: true
144+
is_empty: false
145145
is_archived: false
146146
is_mirror: true
147147
status: 0

routers/api/v1/repo/branch.go

+35-2
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,21 @@ func DeleteBranch(ctx *context.APIContext) {
116116
// "404":
117117
// "$ref": "#/responses/notFound"
118118

119+
if ctx.Repo.Repository.IsEmpty {
120+
ctx.Error(http.StatusNotFound, "", "Git Repository is empty.")
121+
return
122+
}
123+
124+
if ctx.Repo.Repository.IsArchived {
125+
ctx.Error(http.StatusForbidden, "", "Git Repository is archived.")
126+
return
127+
}
128+
129+
if ctx.Repo.Repository.IsMirror {
130+
ctx.Error(http.StatusForbidden, "", "Git Repository is a mirror.")
131+
return
132+
}
133+
119134
branchName := ctx.Params("*")
120135

121136
if err := repo_service.DeleteBranch(ctx, ctx.Doer, ctx.Repo.Repository, ctx.Repo.GitRepo, branchName); err != nil {
@@ -162,17 +177,30 @@ func CreateBranch(ctx *context.APIContext) {
162177
// responses:
163178
// "201":
164179
// "$ref": "#/responses/Branch"
180+
// "403":
181+
// description: The branch is archived or a mirror.
165182
// "404":
166183
// description: The old branch does not exist.
167184
// "409":
168185
// description: The branch with the same name already exists.
169186

170-
opt := web.GetForm(ctx).(*api.CreateBranchRepoOption)
171187
if ctx.Repo.Repository.IsEmpty {
172188
ctx.Error(http.StatusNotFound, "", "Git Repository is empty.")
173189
return
174190
}
175191

192+
if ctx.Repo.Repository.IsArchived {
193+
ctx.Error(http.StatusForbidden, "", "Git Repository is archived.")
194+
return
195+
}
196+
197+
if ctx.Repo.Repository.IsMirror {
198+
ctx.Error(http.StatusForbidden, "", "Git Repository is a mirror.")
199+
return
200+
}
201+
202+
opt := web.GetForm(ctx).(*api.CreateBranchRepoOption)
203+
176204
var oldCommit *git.Commit
177205
var err error
178206

@@ -280,7 +308,12 @@ func ListBranches(ctx *context.APIContext) {
280308

281309
listOptions := utils.GetListOptions(ctx)
282310

283-
if !ctx.Repo.Repository.IsEmpty && ctx.Repo.GitRepo != nil {
311+
if !ctx.Repo.Repository.IsEmpty {
312+
if ctx.Repo.GitRepo == nil {
313+
ctx.Error(http.StatusInternalServerError, "Load git repository failed", nil)
314+
return
315+
}
316+
284317
rules, err := git_model.FindRepoProtectedBranchRules(ctx, ctx.Repo.Repository.ID)
285318
if err != nil {
286319
ctx.Error(http.StatusInternalServerError, "FindMatchedProtectedBranchRules", err)

templates/swagger/v1_json.tmpl

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ref: refs/heads/master
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[core]
2+
repositoryformatversion = 0
3+
filemode = true
4+
bare = true
5+
ignorecase = true
6+
precomposeunicode = true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Unnamed repository; edit this file 'description' to name the repository.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env bash
2+
ORI_DIR=`pwd`
3+
SHELL_FOLDER=$(cd "$(dirname "$0")";pwd)
4+
cd "$ORI_DIR"
5+
for i in `ls "$SHELL_FOLDER/post-receive.d"`; do
6+
sh "$SHELL_FOLDER/post-receive.d/$i"
7+
done
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env bash
2+
"$GITEA_ROOT/gitea" hook --config="$GITEA_ROOT/$GITEA_CONF" post-receive
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env bash
2+
ORI_DIR=`pwd`
3+
SHELL_FOLDER=$(cd "$(dirname "$0")";pwd)
4+
cd "$ORI_DIR"
5+
for i in `ls "$SHELL_FOLDER/pre-receive.d"`; do
6+
sh "$SHELL_FOLDER/pre-receive.d/$i"
7+
done
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env bash
2+
"$GITEA_ROOT/gitea" hook --config="$GITEA_ROOT/$GITEA_CONF" pre-receive
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env bash
2+
ORI_DIR=`pwd`
3+
SHELL_FOLDER=$(cd "$(dirname "$0")";pwd)
4+
cd "$ORI_DIR"
5+
for i in `ls "$SHELL_FOLDER/update.d"`; do
6+
sh "$SHELL_FOLDER/update.d/$i" $1 $2 $3
7+
done
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env bash
2+
"$GITEA_ROOT/gitea" hook --config="$GITEA_ROOT/$GITEA_CONF" update $1 $2 $3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# git ls-files --others --exclude-from=.git/info/exclude
2+
# Lines that start with '#' are comments.
3+
# For a project mostly in C, the following would be a good set of
4+
# exclude patterns (uncomment them if you want to use them):
5+
# *.[oa]
6+
# *~
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2a47ca4b614a9f5a43abbd5ad851a54a616ffee6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
d22b4d4daa5be07329fcef6ed458f00cf3392da0
+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package integration
5+
6+
import (
7+
"bytes"
8+
"fmt"
9+
"io"
10+
"net/http"
11+
"net/url"
12+
"sort"
13+
"testing"
14+
15+
auth_model "code.gitea.io/gitea/models/auth"
16+
repo_model "code.gitea.io/gitea/models/repo"
17+
"code.gitea.io/gitea/models/unittest"
18+
user_model "code.gitea.io/gitea/models/user"
19+
"code.gitea.io/gitea/modules/json"
20+
"code.gitea.io/gitea/modules/setting"
21+
api "code.gitea.io/gitea/modules/structs"
22+
"code.gitea.io/gitea/tests"
23+
24+
"github.com/stretchr/testify/assert"
25+
)
26+
27+
func TestAPIRepoBranchesPlain(t *testing.T) {
28+
onGiteaRun(t, func(*testing.T, *url.URL) {
29+
repo3 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3})
30+
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
31+
session := loginUser(t, user1.LowerName)
32+
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
33+
34+
link, _ := url.Parse(fmt.Sprintf("/api/v1/repos/user3/%s/branches", repo3.Name)) // a plain repo
35+
link.RawQuery = url.Values{"token": {token}}.Encode()
36+
resp := MakeRequest(t, NewRequest(t, "GET", link.String()), http.StatusOK)
37+
bs, err := io.ReadAll(resp.Body)
38+
assert.NoError(t, err)
39+
40+
var branches []*api.Branch
41+
assert.NoError(t, json.Unmarshal(bs, &branches))
42+
assert.Len(t, branches, 2)
43+
sort.Slice(branches, func(i, j int) bool {
44+
return branches[i].Name > branches[j].Name
45+
})
46+
assert.EqualValues(t, "test_branch", branches[0].Name)
47+
assert.EqualValues(t, "master", branches[1].Name)
48+
49+
link2, _ := url.Parse(fmt.Sprintf("/api/v1/repos/user3/%s/branches/test_branch", repo3.Name))
50+
link2.RawQuery = url.Values{"token": {token}}.Encode()
51+
resp = MakeRequest(t, NewRequest(t, "GET", link2.String()), http.StatusOK)
52+
bs, err = io.ReadAll(resp.Body)
53+
assert.NoError(t, err)
54+
var branch api.Branch
55+
assert.NoError(t, json.Unmarshal(bs, &branch))
56+
assert.EqualValues(t, "test_branch", branch.Name)
57+
58+
req := NewRequest(t, "POST", link.String())
59+
req.Header.Add("Content-Type", "application/json")
60+
req.Body = io.NopCloser(bytes.NewBufferString(`{"new_branch_name":"test_branch2", "old_branch_name": "test_branch", "old_ref_name":"refs/heads/test_branch"}`))
61+
resp = MakeRequest(t, req, http.StatusCreated)
62+
bs, err = io.ReadAll(resp.Body)
63+
assert.NoError(t, err)
64+
var branch2 api.Branch
65+
assert.NoError(t, json.Unmarshal(bs, &branch2))
66+
assert.EqualValues(t, "test_branch2", branch2.Name)
67+
assert.EqualValues(t, branch.Commit.ID, branch2.Commit.ID)
68+
69+
resp = MakeRequest(t, NewRequest(t, "GET", link.String()), http.StatusOK)
70+
bs, err = io.ReadAll(resp.Body)
71+
assert.NoError(t, err)
72+
73+
branches = []*api.Branch{}
74+
assert.NoError(t, json.Unmarshal(bs, &branches))
75+
assert.Len(t, branches, 3)
76+
sort.Slice(branches, func(i, j int) bool {
77+
return branches[i].Name > branches[j].Name
78+
})
79+
assert.EqualValues(t, "test_branch2", branches[0].Name)
80+
assert.EqualValues(t, "test_branch", branches[1].Name)
81+
assert.EqualValues(t, "master", branches[2].Name)
82+
83+
link3, _ := url.Parse(fmt.Sprintf("/api/v1/repos/user3/%s/branches/test_branch2", repo3.Name))
84+
MakeRequest(t, NewRequest(t, "DELETE", link3.String()), http.StatusNotFound)
85+
86+
link3.RawQuery = url.Values{"token": {token}}.Encode()
87+
MakeRequest(t, NewRequest(t, "DELETE", link3.String()), http.StatusNoContent)
88+
assert.NoError(t, err)
89+
})
90+
}
91+
92+
func TestAPIRepoBranchesMirror(t *testing.T) {
93+
defer tests.PrepareTestEnv(t)()
94+
95+
repo5 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 5})
96+
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
97+
session := loginUser(t, user1.LowerName)
98+
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
99+
100+
link, _ := url.Parse(fmt.Sprintf("/api/v1/repos/user3/%s/branches", repo5.Name)) // a mirror repo
101+
link.RawQuery = url.Values{"token": {token}}.Encode()
102+
resp := MakeRequest(t, NewRequest(t, "GET", link.String()), http.StatusOK)
103+
bs, err := io.ReadAll(resp.Body)
104+
assert.NoError(t, err)
105+
106+
var branches []*api.Branch
107+
assert.NoError(t, json.Unmarshal(bs, &branches))
108+
assert.Len(t, branches, 2)
109+
sort.Slice(branches, func(i, j int) bool {
110+
return branches[i].Name > branches[j].Name
111+
})
112+
assert.EqualValues(t, "test_branch", branches[0].Name)
113+
assert.EqualValues(t, "master", branches[1].Name)
114+
115+
link2, _ := url.Parse(fmt.Sprintf("/api/v1/repos/user3/%s/branches/test_branch", repo5.Name))
116+
link2.RawQuery = url.Values{"token": {token}}.Encode()
117+
resp = MakeRequest(t, NewRequest(t, "GET", link2.String()), http.StatusOK)
118+
bs, err = io.ReadAll(resp.Body)
119+
assert.NoError(t, err)
120+
var branch api.Branch
121+
assert.NoError(t, json.Unmarshal(bs, &branch))
122+
assert.EqualValues(t, "test_branch", branch.Name)
123+
124+
req := NewRequest(t, "POST", link.String())
125+
req.Header.Add("Content-Type", "application/json")
126+
req.Body = io.NopCloser(bytes.NewBufferString(`{"new_branch_name":"test_branch2", "old_branch_name": "test_branch", "old_ref_name":"refs/heads/test_branch"}`))
127+
resp = MakeRequest(t, req, http.StatusForbidden)
128+
bs, err = io.ReadAll(resp.Body)
129+
assert.NoError(t, err)
130+
assert.EqualValues(t, "{\"message\":\"Git Repository is a mirror.\",\"url\":\""+setting.AppURL+"api/swagger\"}\n", string(bs))
131+
132+
resp = MakeRequest(t, NewRequest(t, "DELETE", link2.String()), http.StatusForbidden)
133+
bs, err = io.ReadAll(resp.Body)
134+
assert.NoError(t, err)
135+
assert.EqualValues(t, "{\"message\":\"Git Repository is a mirror.\",\"url\":\""+setting.AppURL+"api/swagger\"}\n", string(bs))
136+
}

tests/integration/empty_repo_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestEmptyRepo(t *testing.T) {
3434
"commit/1ae57b34ccf7e18373",
3535
"graph",
3636
}
37-
emptyRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 5})
37+
emptyRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 6})
3838
assert.True(t, emptyRepo.IsEmpty)
3939
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: emptyRepo.OwnerID})
4040
for _, subPath := range subPaths {

0 commit comments

Comments
 (0)