Skip to content

Commit

Permalink
Handle missing README in create repos API (go-gitea#23387) (go-gite…
Browse files Browse the repository at this point in the history
…a#23509)

Backport 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`.
  • Loading branch information
Zettat123 authored Mar 17, 2023
1 parent 04d489d commit e01e78a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions routers/api/v1/admin/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func CreateRepo(ctx *context.APIContext) {
// responses:
// "201":
// "$ref": "#/responses/Repository"
// "400":
// "$ref": "#/responses/error"
// "403":
// "$ref": "#/responses/forbidden"
// "404":
Expand Down
20 changes: 20 additions & 0 deletions routers/api/v1/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -464,6 +482,8 @@ func CreateOrgRepo(ctx *context.APIContext) {
// responses:
// "201":
// "$ref": "#/responses/Repository"
// "400":
// "$ref": "#/responses/error"
// "404":
// "$ref": "#/responses/notFound"
// "403":
Expand Down
9 changes: 9 additions & 0 deletions templates/swagger/v1_json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,9 @@
"201": {
"$ref": "#/responses/Repository"
},
"400": {
"$ref": "#/responses/error"
},
"403": {
"$ref": "#/responses/forbidden"
},
Expand Down Expand Up @@ -1772,6 +1775,9 @@
"201": {
"$ref": "#/responses/Repository"
},
"400": {
"$ref": "#/responses/error"
},
"403": {
"$ref": "#/responses/forbidden"
},
Expand Down Expand Up @@ -12502,6 +12508,9 @@
"201": {
"$ref": "#/responses/Repository"
},
"400": {
"$ref": "#/responses/error"
},
"409": {
"description": "The repository with the same name already exists."
},
Expand Down

0 comments on commit e01e78a

Please sign in to comment.