Skip to content

Commit 13ffa28

Browse files
authored
Fix bug of branches API with tests(go-gitea#25578) (go-gitea#25579)
Backport go-gitea#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

Lines changed: 49 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 35 additions & 2 deletions
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

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ref: refs/heads/master
Lines changed: 6 additions & 0 deletions
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
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Unnamed repository; edit this file 'description' to name the repository.
Lines changed: 7 additions & 0 deletions
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
Lines changed: 2 additions & 0 deletions
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
Lines changed: 7 additions & 0 deletions
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

0 commit comments

Comments
 (0)