Skip to content

Commit dcb91c4

Browse files
committed
Handle missing README in create repos API (go-gitea#23387)
Close go-gitea#22934 In `/user/repos` API (and other APIs related to creating repos), user can specify a readme template for auto init. At present, if the specified template does not exist, a `500` will be returned . This PR improved the logic and will return a `400` instead of `500`.
1 parent 4c1e248 commit dcb91c4

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

routers/api/v1/admin/repo.go

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ func CreateRepo(ctx *context.APIContext) {
3232
// responses:
3333
// "201":
3434
// "$ref": "#/responses/Repository"
35+
// "400":
36+
// "$ref": "#/responses/error"
3537
// "403":
3638
// "$ref": "#/responses/forbidden"
3739
// "404":

routers/api/v1/repo/repo.go

+11
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,13 @@ func CreateUserRepo(ctx *context.APIContext, owner *user_model.User, opt api.Cre
231231
if opt.AutoInit && opt.Readme == "" {
232232
opt.Readme = "Default"
233233
}
234+
235+
// If the readme template does not exist, a 400 will be returned.
236+
if opt.AutoInit && len(opt.Readme) > 0 && !util.SliceContains(repo_module.Readmes, opt.Readme) {
237+
ctx.Error(http.StatusBadRequest, "", fmt.Errorf("readme template does not exist, available templates: %v", repo_module.Readmes))
238+
return
239+
}
240+
234241
repo, err := repo_service.CreateRepository(ctx.Doer, owner, repo_module.CreateRepoOptions{
235242
Name: opt.Name,
236243
Description: opt.Description,
@@ -283,6 +290,8 @@ func Create(ctx *context.APIContext) {
283290
// responses:
284291
// "201":
285292
// "$ref": "#/responses/Repository"
293+
// "400":
294+
// "$ref": "#/responses/error"
286295
// "409":
287296
// description: The repository with the same name already exists.
288297
// "422":
@@ -464,6 +473,8 @@ func CreateOrgRepo(ctx *context.APIContext) {
464473
// responses:
465474
// "201":
466475
// "$ref": "#/responses/Repository"
476+
// "400":
477+
// "$ref": "#/responses/error"
467478
// "404":
468479
// "$ref": "#/responses/notFound"
469480
// "403":

templates/swagger/v1_json.tmpl

+9
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,9 @@
707707
"201": {
708708
"$ref": "#/responses/Repository"
709709
},
710+
"400": {
711+
"$ref": "#/responses/error"
712+
},
710713
"403": {
711714
"$ref": "#/responses/forbidden"
712715
},
@@ -1920,6 +1923,9 @@
19201923
"201": {
19211924
"$ref": "#/responses/Repository"
19221925
},
1926+
"400": {
1927+
"$ref": "#/responses/error"
1928+
},
19231929
"403": {
19241930
"$ref": "#/responses/forbidden"
19251931
},
@@ -13223,6 +13229,9 @@
1322313229
"201": {
1322413230
"$ref": "#/responses/Repository"
1322513231
},
13232+
"400": {
13233+
"$ref": "#/responses/error"
13234+
},
1322613235
"409": {
1322713236
"description": "The repository with the same name already exists."
1322813237
},

0 commit comments

Comments
 (0)