Skip to content

Commit 8421b82

Browse files
authored
Handle missing README in create repos API (#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 5eea61d commit 8421b82

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, 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
@@ -713,6 +713,9 @@
713713
"201": {
714714
"$ref": "#/responses/Repository"
715715
},
716+
"400": {
717+
"$ref": "#/responses/error"
718+
},
716719
"403": {
717720
"$ref": "#/responses/forbidden"
718721
},
@@ -1926,6 +1929,9 @@
19261929
"201": {
19271930
"$ref": "#/responses/Repository"
19281931
},
1932+
"400": {
1933+
"$ref": "#/responses/error"
1934+
},
19291935
"403": {
19301936
"$ref": "#/responses/forbidden"
19311937
},
@@ -13382,6 +13388,9 @@
1338213388
"201": {
1338313389
"$ref": "#/responses/Repository"
1338413390
},
13391+
"400": {
13392+
"$ref": "#/responses/error"
13393+
},
1338513394
"409": {
1338613395
"description": "The repository with the same name already exists."
1338713396
},

0 commit comments

Comments
 (0)