aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZettat123 <zettat123@gmail.com>2023-03-17 09:05:41 +0800
committerGitHub <noreply@github.com>2023-03-16 21:05:41 -0400
commite01e78a9471a9147fc50860b50196b6cebfb5d06 (patch)
treeae7573b859ccb4019b009aceb13116d2b06817c7
parent04d489dbdd9906c51bf836203eb5d1af061902e1 (diff)
downloadgitea-e01e78a9471a9147fc50860b50196b6cebfb5d06.tar.gz
gitea-e01e78a9471a9147fc50860b50196b6cebfb5d06.zip
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`.
-rw-r--r--routers/api/v1/admin/repo.go2
-rw-r--r--routers/api/v1/repo/repo.go20
-rw-r--r--templates/swagger/v1_json.tmpl9
3 files changed, 31 insertions, 0 deletions
diff --git a/routers/api/v1/admin/repo.go b/routers/api/v1/admin/repo.go
index 712ced89c9..097b48f318 100644
--- a/routers/api/v1/admin/repo.go
+++ b/routers/api/v1/admin/repo.go
@@ -33,6 +33,8 @@ func CreateRepo(ctx *context.APIContext) {
// responses:
// "201":
// "$ref": "#/responses/Repository"
+ // "400":
+ // "$ref": "#/responses/error"
// "403":
// "$ref": "#/responses/forbidden"
// "404":
diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go
index de8a4d1864..5aa9111eb7 100644
--- a/routers/api/v1/repo/repo.go
+++ b/routers/api/v1/repo/repo.go
@@ -231,6 +231,22 @@ func CreateUserRepo(ctx *context.APIContext, owner *user_model.User, opt api.Cre
if opt.AutoInit && opt.Readme == "" {
opt.Readme = "Default"
}
+
+ contains := func(slice []string, s string) bool {
+ for _, v := range slice {
+ if v == s {
+ return true
+ }
+ }
+ return false
+ }
+
+ // If the readme template does not exist, a 400 will be returned.
+ if opt.AutoInit && len(opt.Readme) > 0 && !contains(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{
Name: opt.Name,
Description: opt.Description,
@@ -283,6 +299,8 @@ func Create(ctx *context.APIContext) {
// responses:
// "201":
// "$ref": "#/responses/Repository"
+ // "400":
+ // "$ref": "#/responses/error"
// "409":
// description: The repository with the same name already exists.
// "422":
@@ -464,6 +482,8 @@ func CreateOrgRepo(ctx *context.APIContext) {
// responses:
// "201":
// "$ref": "#/responses/Repository"
+ // "400":
+ // "$ref": "#/responses/error"
// "404":
// "$ref": "#/responses/notFound"
// "403":
diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl
index 5df9f2bd2b..5763fb9fd4 100644
--- a/templates/swagger/v1_json.tmpl
+++ b/templates/swagger/v1_json.tmpl
@@ -586,6 +586,9 @@
"201": {
"$ref": "#/responses/Repository"
},
+ "400": {
+ "$ref": "#/responses/error"
+ },
"403": {
"$ref": "#/responses/forbidden"
},
@@ -1772,6 +1775,9 @@
"201": {
"$ref": "#/responses/Repository"
},
+ "400": {
+ "$ref": "#/responses/error"
+ },
"403": {
"$ref": "#/responses/forbidden"
},
@@ -12502,6 +12508,9 @@
"201": {
"$ref": "#/responses/Repository"
},
+ "400": {
+ "$ref": "#/responses/error"
+ },
"409": {
"description": "The repository with the same name already exists."
},