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:
@@ -6,6 +6,7 @@ package repo
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
@@ -45,6 +46,7 @@ func ListIssueComments(ctx *context.APIContext) {
|
||||
// responses:
|
||||
// "200":
|
||||
// "$ref": "#/responses/CommentList"
|
||||
|
||||
var since time.Time
|
||||
if len(ctx.Query("since")) > 0 {
|
||||
since, _ = time.Parse(time.RFC3339, ctx.Query("since"))
|
||||
@@ -53,7 +55,7 @@ func ListIssueComments(ctx *context.APIContext) {
|
||||
// comments,err:=models.GetCommentsByIssueIDSince(, since)
|
||||
issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
|
||||
if err != nil {
|
||||
ctx.Error(500, "GetRawIssueByIndex", err)
|
||||
ctx.Error(http.StatusInternalServerError, "GetRawIssueByIndex", err)
|
||||
return
|
||||
}
|
||||
issue.Repo = ctx.Repo.Repository
|
||||
@@ -64,12 +66,12 @@ func ListIssueComments(ctx *context.APIContext) {
|
||||
Type: models.CommentTypeComment,
|
||||
})
|
||||
if err != nil {
|
||||
ctx.Error(500, "FindComments", err)
|
||||
ctx.Error(http.StatusInternalServerError, "FindComments", err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := models.CommentList(comments).LoadPosters(); err != nil {
|
||||
ctx.Error(500, "LoadPosters", err)
|
||||
ctx.Error(http.StatusInternalServerError, "LoadPosters", err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -78,7 +80,7 @@ func ListIssueComments(ctx *context.APIContext) {
|
||||
comment.Issue = issue
|
||||
apiComments[i] = comments[i].APIFormat()
|
||||
}
|
||||
ctx.JSON(200, &apiComments)
|
||||
ctx.JSON(http.StatusOK, &apiComments)
|
||||
}
|
||||
|
||||
// ListRepoIssueComments returns all issue-comments for a repo
|
||||
@@ -106,6 +108,7 @@ func ListRepoIssueComments(ctx *context.APIContext) {
|
||||
// responses:
|
||||
// "200":
|
||||
// "$ref": "#/responses/CommentList"
|
||||
|
||||
var since time.Time
|
||||
if len(ctx.Query("since")) > 0 {
|
||||
since, _ = time.Parse(time.RFC3339, ctx.Query("since"))
|
||||
@@ -117,32 +120,32 @@ func ListRepoIssueComments(ctx *context.APIContext) {
|
||||
Type: models.CommentTypeComment,
|
||||
})
|
||||
if err != nil {
|
||||
ctx.Error(500, "FindComments", err)
|
||||
ctx.Error(http.StatusInternalServerError, "FindComments", err)
|
||||
return
|
||||
}
|
||||
|
||||
if err = models.CommentList(comments).LoadPosters(); err != nil {
|
||||
ctx.Error(500, "LoadPosters", err)
|
||||
ctx.Error(http.StatusInternalServerError, "LoadPosters", err)
|
||||
return
|
||||
}
|
||||
|
||||
apiComments := make([]*api.Comment, len(comments))
|
||||
if err := models.CommentList(comments).LoadIssues(); err != nil {
|
||||
ctx.Error(500, "LoadIssues", err)
|
||||
ctx.Error(http.StatusInternalServerError, "LoadIssues", err)
|
||||
return
|
||||
}
|
||||
if err := models.CommentList(comments).LoadPosters(); err != nil {
|
||||
ctx.Error(500, "LoadPosters", err)
|
||||
ctx.Error(http.StatusInternalServerError, "LoadPosters", err)
|
||||
return
|
||||
}
|
||||
if _, err := models.CommentList(comments).Issues().LoadRepositories(); err != nil {
|
||||
ctx.Error(500, "LoadRepositories", err)
|
||||
ctx.Error(http.StatusInternalServerError, "LoadRepositories", err)
|
||||
return
|
||||
}
|
||||
for i := range comments {
|
||||
apiComments[i] = comments[i].APIFormat()
|
||||
}
|
||||
ctx.JSON(200, &apiComments)
|
||||
ctx.JSON(http.StatusOK, &apiComments)
|
||||
}
|
||||
|
||||
// CreateIssueComment create a comment for an issue
|
||||
@@ -178,24 +181,27 @@ func CreateIssueComment(ctx *context.APIContext, form api.CreateIssueCommentOpti
|
||||
// responses:
|
||||
// "201":
|
||||
// "$ref": "#/responses/Comment"
|
||||
// "403":
|
||||
// "$ref": "#/responses/forbidden"
|
||||
|
||||
issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
|
||||
if err != nil {
|
||||
ctx.Error(500, "GetIssueByIndex", err)
|
||||
ctx.Error(http.StatusInternalServerError, "GetIssueByIndex", err)
|
||||
return
|
||||
}
|
||||
|
||||
if issue.IsLocked && !ctx.Repo.CanWrite(models.UnitTypeIssues) && !ctx.User.IsAdmin {
|
||||
ctx.Error(403, "CreateIssueComment", errors.New(ctx.Tr("repo.issues.comment_on_locked")))
|
||||
ctx.Error(http.StatusForbidden, "CreateIssueComment", errors.New(ctx.Tr("repo.issues.comment_on_locked")))
|
||||
return
|
||||
}
|
||||
|
||||
comment, err := comment_service.CreateIssueComment(ctx.User, ctx.Repo.Repository, issue, form.Body, nil)
|
||||
if err != nil {
|
||||
ctx.Error(500, "CreateIssueComment", err)
|
||||
ctx.Error(http.StatusInternalServerError, "CreateIssueComment", err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(201, comment.APIFormat())
|
||||
ctx.JSON(http.StatusCreated, comment.APIFormat())
|
||||
}
|
||||
|
||||
// EditIssueComment modify a comment of an issue
|
||||
@@ -273,6 +279,11 @@ func EditIssueCommentDeprecated(ctx *context.APIContext, form api.EditIssueComme
|
||||
// responses:
|
||||
// "200":
|
||||
// "$ref": "#/responses/Comment"
|
||||
// "204":
|
||||
// "$ref": "#/responses/empty"
|
||||
// "403":
|
||||
// "$ref": "#/responses/forbidden"
|
||||
|
||||
editIssueComment(ctx, form)
|
||||
}
|
||||
|
||||
@@ -282,27 +293,27 @@ func editIssueComment(ctx *context.APIContext, form api.EditIssueCommentOption)
|
||||
if models.IsErrCommentNotExist(err) {
|
||||
ctx.NotFound(err)
|
||||
} else {
|
||||
ctx.Error(500, "GetCommentByID", err)
|
||||
ctx.Error(http.StatusInternalServerError, "GetCommentByID", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if !ctx.IsSigned || (ctx.User.ID != comment.PosterID && !ctx.Repo.IsAdmin()) {
|
||||
ctx.Status(403)
|
||||
ctx.Status(http.StatusForbidden)
|
||||
return
|
||||
} else if comment.Type != models.CommentTypeComment {
|
||||
ctx.Status(204)
|
||||
ctx.Status(http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
|
||||
oldContent := comment.Content
|
||||
comment.Content = form.Body
|
||||
if err := comment_service.UpdateComment(comment, ctx.User, oldContent); err != nil {
|
||||
ctx.Error(500, "UpdateComment", err)
|
||||
ctx.Error(http.StatusInternalServerError, "UpdateComment", err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(200, comment.APIFormat())
|
||||
ctx.JSON(http.StatusOK, comment.APIFormat())
|
||||
}
|
||||
|
||||
// DeleteIssueComment delete a comment from an issue
|
||||
@@ -330,6 +341,9 @@ func DeleteIssueComment(ctx *context.APIContext) {
|
||||
// responses:
|
||||
// "204":
|
||||
// "$ref": "#/responses/empty"
|
||||
// "403":
|
||||
// "$ref": "#/responses/forbidden"
|
||||
|
||||
deleteIssueComment(ctx)
|
||||
}
|
||||
|
||||
@@ -364,6 +378,9 @@ func DeleteIssueCommentDeprecated(ctx *context.APIContext) {
|
||||
// responses:
|
||||
// "204":
|
||||
// "$ref": "#/responses/empty"
|
||||
// "403":
|
||||
// "$ref": "#/responses/forbidden"
|
||||
|
||||
deleteIssueComment(ctx)
|
||||
}
|
||||
|
||||
@@ -373,23 +390,23 @@ func deleteIssueComment(ctx *context.APIContext) {
|
||||
if models.IsErrCommentNotExist(err) {
|
||||
ctx.NotFound(err)
|
||||
} else {
|
||||
ctx.Error(500, "GetCommentByID", err)
|
||||
ctx.Error(http.StatusInternalServerError, "GetCommentByID", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if !ctx.IsSigned || (ctx.User.ID != comment.PosterID && !ctx.Repo.IsAdmin()) {
|
||||
ctx.Status(403)
|
||||
ctx.Status(http.StatusForbidden)
|
||||
return
|
||||
} else if comment.Type != models.CommentTypeComment {
|
||||
ctx.Status(204)
|
||||
ctx.Status(http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
|
||||
if err = comment_service.DeleteComment(comment, ctx.User); err != nil {
|
||||
ctx.Error(500, "DeleteCommentByID", err)
|
||||
ctx.Error(http.StatusInternalServerError, "DeleteCommentByID", err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Status(204)
|
||||
ctx.Status(http.StatusNoContent)
|
||||
}
|
||||
|
Reference in New Issue
Block a user