Handle missing `README` in create repos API (#23387) (#23510)

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`.

Co-authored-by: techknowlogick <techknowlogick@gitea.io>
pull/23533/head
Zettat123 2 years ago committed by GitHub
parent 8f8bd3c0cb
commit 301de3ab6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      routers/api/v1/admin/repo.go
  2. 11
      routers/api/v1/repo/repo.go
  3. 9
      templates/swagger/v1_json.tmpl

@ -32,6 +32,8 @@ func CreateRepo(ctx *context.APIContext) {
// responses: // responses:
// "201": // "201":
// "$ref": "#/responses/Repository" // "$ref": "#/responses/Repository"
// "400":
// "$ref": "#/responses/error"
// "403": // "403":
// "$ref": "#/responses/forbidden" // "$ref": "#/responses/forbidden"
// "404": // "404":

@ -231,6 +231,13 @@ func CreateUserRepo(ctx *context.APIContext, owner *user_model.User, opt api.Cre
if opt.AutoInit && opt.Readme == "" { if opt.AutoInit && opt.Readme == "" {
opt.Readme = "Default" opt.Readme = "Default"
} }
// If the readme template does not exist, a 400 will be returned.
if opt.AutoInit && len(opt.Readme) > 0 && !util.SliceContains(repo_module.Readmes, opt.Readme) {
ctx.Error(http.StatusBadRequest, "", fmt.Errorf("readme template does not exist, available templates: %v", repo_module.Readmes))
return
}
repo, err := repo_service.CreateRepository(ctx.Doer, owner, repo_module.CreateRepoOptions{ repo, err := repo_service.CreateRepository(ctx.Doer, owner, repo_module.CreateRepoOptions{
Name: opt.Name, Name: opt.Name,
Description: opt.Description, Description: opt.Description,
@ -283,6 +290,8 @@ func Create(ctx *context.APIContext) {
// responses: // responses:
// "201": // "201":
// "$ref": "#/responses/Repository" // "$ref": "#/responses/Repository"
// "400":
// "$ref": "#/responses/error"
// "409": // "409":
// description: The repository with the same name already exists. // description: The repository with the same name already exists.
// "422": // "422":
@ -464,6 +473,8 @@ func CreateOrgRepo(ctx *context.APIContext) {
// responses: // responses:
// "201": // "201":
// "$ref": "#/responses/Repository" // "$ref": "#/responses/Repository"
// "400":
// "$ref": "#/responses/error"
// "404": // "404":
// "$ref": "#/responses/notFound" // "$ref": "#/responses/notFound"
// "403": // "403":

@ -713,6 +713,9 @@
"201": { "201": {
"$ref": "#/responses/Repository" "$ref": "#/responses/Repository"
}, },
"400": {
"$ref": "#/responses/error"
},
"403": { "403": {
"$ref": "#/responses/forbidden" "$ref": "#/responses/forbidden"
}, },
@ -1926,6 +1929,9 @@
"201": { "201": {
"$ref": "#/responses/Repository" "$ref": "#/responses/Repository"
}, },
"400": {
"$ref": "#/responses/error"
},
"403": { "403": {
"$ref": "#/responses/forbidden" "$ref": "#/responses/forbidden"
}, },
@ -13229,6 +13235,9 @@
"201": { "201": {
"$ref": "#/responses/Repository" "$ref": "#/responses/Repository"
}, },
"400": {
"$ref": "#/responses/error"
},
"409": { "409": {
"description": "The repository with the same name already exists." "description": "The repository with the same name already exists."
}, },

Loading…
Cancel
Save