mirror of
https://github.com/go-gitea/gitea
synced 2025-07-22 18:28:37 +00:00
Swagger info corrections (#9441)
* use numbers and not http.Status___ enum
* fix test
* add many missing swagger responses
* code format
* Deletion Sould return 204 ...
* error handling improvements
* if special error type ... then add it to swagger too
* one smal nit
* invalidTopicsError is []string
* valid swagger specification 2.0
- if you add responses swagger can tell you if you do it right 👍
* use ctx.InternalServerError
* Revert "use numbers and not http.Status___ enum"
This reverts commit b1ff386e24
.
* use http.Status* enum everywhere
This commit is contained in:
@@ -5,6 +5,8 @@
|
||||
package repo
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
)
|
||||
@@ -41,20 +43,21 @@ func StartIssueStopwatch(ctx *context.APIContext) {
|
||||
// "403":
|
||||
// description: Not repo writer, user does not have rights to toggle stopwatch
|
||||
// "404":
|
||||
// description: Issue not found
|
||||
// "$ref": "#/responses/notFound"
|
||||
// "409":
|
||||
// description: Cannot start a stopwatch again if it already exists
|
||||
|
||||
issue, err := prepareIssueStopwatch(ctx, false)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if err := models.CreateOrStopIssueStopwatch(ctx.User, issue); err != nil {
|
||||
ctx.Error(500, "CreateOrStopIssueStopwatch", err)
|
||||
ctx.Error(http.StatusInternalServerError, "CreateOrStopIssueStopwatch", err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Status(201)
|
||||
ctx.Status(http.StatusCreated)
|
||||
}
|
||||
|
||||
// StopIssueStopwatch stops a stopwatch for the given issue.
|
||||
@@ -89,20 +92,21 @@ func StopIssueStopwatch(ctx *context.APIContext) {
|
||||
// "403":
|
||||
// description: Not repo writer, user does not have rights to toggle stopwatch
|
||||
// "404":
|
||||
// description: Issue not found
|
||||
// "$ref": "#/responses/notFound"
|
||||
// "409":
|
||||
// description: Cannot stop a non existent stopwatch
|
||||
|
||||
issue, err := prepareIssueStopwatch(ctx, true)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if err := models.CreateOrStopIssueStopwatch(ctx.User, issue); err != nil {
|
||||
ctx.Error(500, "CreateOrStopIssueStopwatch", err)
|
||||
ctx.Error(http.StatusInternalServerError, "CreateOrStopIssueStopwatch", err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Status(201)
|
||||
ctx.Status(http.StatusCreated)
|
||||
}
|
||||
|
||||
// DeleteIssueStopwatch delete a specific stopwatch
|
||||
@@ -137,20 +141,21 @@ func DeleteIssueStopwatch(ctx *context.APIContext) {
|
||||
// "403":
|
||||
// description: Not repo writer, user does not have rights to toggle stopwatch
|
||||
// "404":
|
||||
// description: Issue not found
|
||||
// "$ref": "#/responses/notFound"
|
||||
// "409":
|
||||
// description: Cannot cancel a non existent stopwatch
|
||||
|
||||
issue, err := prepareIssueStopwatch(ctx, true)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if err := models.CancelStopwatch(ctx.User, issue); err != nil {
|
||||
ctx.Error(500, "CancelStopwatch", err)
|
||||
ctx.Error(http.StatusInternalServerError, "CancelStopwatch", err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Status(204)
|
||||
ctx.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func prepareIssueStopwatch(ctx *context.APIContext, shouldExist bool) (*models.Issue, error) {
|
||||
@@ -159,27 +164,27 @@ func prepareIssueStopwatch(ctx *context.APIContext, shouldExist bool) (*models.I
|
||||
if models.IsErrIssueNotExist(err) {
|
||||
ctx.NotFound()
|
||||
} else {
|
||||
ctx.Error(500, "GetIssueByIndex", err)
|
||||
ctx.Error(http.StatusInternalServerError, "GetIssueByIndex", err)
|
||||
}
|
||||
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !ctx.Repo.CanWrite(models.UnitTypeIssues) {
|
||||
ctx.Status(403)
|
||||
ctx.Status(http.StatusForbidden)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !ctx.Repo.CanUseTimetracker(issue, ctx.User) {
|
||||
ctx.Status(403)
|
||||
ctx.Status(http.StatusForbidden)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if models.StopwatchExists(ctx.User.ID, issue.ID) != shouldExist {
|
||||
if shouldExist {
|
||||
ctx.Error(409, "StopwatchExists", "cannot stop/cancel a non existent stopwatch")
|
||||
ctx.Error(http.StatusConflict, "StopwatchExists", "cannot stop/cancel a non existent stopwatch")
|
||||
} else {
|
||||
ctx.Error(409, "StopwatchExists", "cannot start a stopwatch again if it already exists")
|
||||
ctx.Error(http.StatusConflict, "StopwatchExists", "cannot start a stopwatch again if it already exists")
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
@@ -202,15 +207,15 @@ func GetStopwatches(ctx *context.APIContext) {
|
||||
|
||||
sws, err := models.GetUserStopwatches(ctx.User.ID)
|
||||
if err != nil {
|
||||
ctx.Error(500, "GetUserStopwatches", err)
|
||||
ctx.Error(http.StatusInternalServerError, "GetUserStopwatches", err)
|
||||
return
|
||||
}
|
||||
|
||||
apiSWs, err := sws.APIFormat()
|
||||
if err != nil {
|
||||
ctx.Error(500, "APIFormat", err)
|
||||
ctx.Error(http.StatusInternalServerError, "APIFormat", err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(200, apiSWs)
|
||||
ctx.JSON(http.StatusOK, apiSWs)
|
||||
}
|
||||
|
Reference in New Issue
Block a user