Skip to content

Commit e01e78a

Browse files
authored
Handle missing README in create repos API (#23387) (#23509)
Backport #23387 Close #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 04d489d commit e01e78a

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

routers/api/v1/admin/repo.go

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

routers/api/v1/repo/repo.go

+20
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,22 @@ 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+
contains := func(slice []string, s string) bool {
236+
for _, v := range slice {
237+
if v == s {
238+
return true
239+
}
240+
}
241+
return false
242+
}
243+
244+
// If the readme template does not exist, a 400 will be returned.
245+
if opt.AutoInit && len(opt.Readme) > 0 && !contains(repo_module.Readmes, opt.Readme) {
246+
ctx.Error(http.StatusBadRequest, "", fmt.Errorf("readme template does not exist, available templates: %v", repo_module.Readmes))
247+
return
248+
}
249+
234250
repo, err := repo_service.CreateRepository(ctx.Doer, owner, repo_module.CreateRepoOptions{
235251
Name: opt.Name,
236252
Description: opt.Description,
@@ -283,6 +299,8 @@ func Create(ctx *context.APIContext) {
283299
// responses:
284300
// "201":
285301
// "$ref": "#/responses/Repository"
302+
// "400":
303+
// "$ref": "#/responses/error"
286304
// "409":
287305
// description: The repository with the same name already exists.
288306
// "422":
@@ -464,6 +482,8 @@ func CreateOrgRepo(ctx *context.APIContext) {
464482
// responses:
465483
// "201":
466484
// "$ref": "#/responses/Repository"
485+
// "400":
486+
// "$ref": "#/responses/error"
467487
// "404":
468488
// "$ref": "#/responses/notFound"
469489
// "403":

templates/swagger/v1_json.tmpl

+9
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,9 @@
586586
"201": {
587587
"$ref": "#/responses/Repository"
588588
},
589+
"400": {
590+
"$ref": "#/responses/error"
591+
},
589592
"403": {
590593
"$ref": "#/responses/forbidden"
591594
},
@@ -1772,6 +1775,9 @@
17721775
"201": {
17731776
"$ref": "#/responses/Repository"
17741777
},
1778+
"400": {
1779+
"$ref": "#/responses/error"
1780+
},
17751781
"403": {
17761782
"$ref": "#/responses/forbidden"
17771783
},
@@ -12502,6 +12508,9 @@
1250212508
"201": {
1250312509
"$ref": "#/responses/Repository"
1250412510
},
12511+
"400": {
12512+
"$ref": "#/responses/error"
12513+
},
1250512514
"409": {
1250612515
"description": "The repository with the same name already exists."
1250712516
},

0 commit comments

Comments
 (0)