Skip to content

Commit de981c3

Browse files
authored
Fix bug of branches API with tests (#25578)
Fix #25558 Extract from #22743 This PR added a repository's check when creating/deleting branches via API. Mirror repository and archive repository cannot do that.
1 parent 1704c64 commit de981c3

26 files changed

+258
-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
@@ -118,6 +118,21 @@ func DeleteBranch(ctx *context.APIContext) {
118118
// "404":
119119
// "$ref": "#/responses/notFound"
120120

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

123138
if ctx.Repo.Repository.IsEmpty {
@@ -195,17 +210,30 @@ func CreateBranch(ctx *context.APIContext) {
195210
// responses:
196211
// "201":
197212
// "$ref": "#/responses/Branch"
213+
// "403":
214+
// description: The branch is archived or a mirror.
198215
// "404":
199216
// description: The old branch does not exist.
200217
// "409":
201218
// description: The branch with the same name already exists.
202219

203-
opt := web.GetForm(ctx).(*api.CreateBranchRepoOption)
204220
if ctx.Repo.Repository.IsEmpty {
205221
ctx.Error(http.StatusNotFound, "", "Git Repository is empty.")
206222
return
207223
}
208224

225+
if ctx.Repo.Repository.IsArchived {
226+
ctx.Error(http.StatusForbidden, "", "Git Repository is archived.")
227+
return
228+
}
229+
230+
if ctx.Repo.Repository.IsMirror {
231+
ctx.Error(http.StatusForbidden, "", "Git Repository is a mirror.")
232+
return
233+
}
234+
235+
opt := web.GetForm(ctx).(*api.CreateBranchRepoOption)
236+
209237
var oldCommit *git.Commit
210238
var err error
211239

@@ -313,7 +341,12 @@ func ListBranches(ctx *context.APIContext) {
313341

314342
listOptions := utils.GetListOptions(ctx)
315343

316-
if !ctx.Repo.Repository.IsEmpty && ctx.Repo.GitRepo != nil {
344+
if !ctx.Repo.Repository.IsEmpty {
345+
if ctx.Repo.GitRepo == nil {
346+
ctx.Error(http.StatusInternalServerError, "Load git repository failed", nil)
347+
return
348+
}
349+
317350
branchOpts := git_model.FindBranchOptions{
318351
ListOptions: listOptions,
319352
RepoID: ctx.Repo.Repository.ID,

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)