diff options
author | 6543 <6543@obermui.de> | 2019-12-20 18:07:12 +0100 |
---|---|---|
committer | Lauris BH <lauris@nix.lv> | 2019-12-20 19:07:12 +0200 |
commit | 2848c5eb8f7333b6791afd296b12d21751d0516b (patch) | |
tree | 67ff6244026174116edbff1b4c4cdb5934401968 /routers/api/v1/repo/release_attachment.go | |
parent | 050a8af4243d7f5fff0a2f492b9166f4dfdf0ecf (diff) | |
download | gitea-2848c5eb8f7333b6791afd296b12d21751d0516b.tar.gz gitea-2848c5eb8f7333b6791afd296b12d21751d0516b.zip |
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 :+1:
* use ctx.InternalServerError
* Revert "use numbers and not http.Status___ enum"
This reverts commit b1ff386e2418ed6a7f183e756b13277d701278ef.
* use http.Status* enum everywhere
Diffstat (limited to 'routers/api/v1/repo/release_attachment.go')
-rw-r--r-- | routers/api/v1/repo/release_attachment.go | 37 |
1 files changed, 21 insertions, 16 deletions
diff --git a/routers/api/v1/repo/release_attachment.go b/routers/api/v1/repo/release_attachment.go index c49e4d3e34..6ba6489e27 100644 --- a/routers/api/v1/repo/release_attachment.go +++ b/routers/api/v1/repo/release_attachment.go @@ -5,6 +5,7 @@ package repo import ( + "net/http" "strings" "code.gitea.io/gitea/models" @@ -48,11 +49,12 @@ func GetReleaseAttachment(ctx *context.APIContext) { // responses: // "200": // "$ref": "#/responses/Attachment" + releaseID := ctx.ParamsInt64(":id") attachID := ctx.ParamsInt64(":asset") attach, err := models.GetAttachmentByID(attachID) if err != nil { - ctx.Error(500, "GetAttachmentByID", err) + ctx.Error(http.StatusInternalServerError, "GetAttachmentByID", err) return } if attach.ReleaseID != releaseID { @@ -61,7 +63,7 @@ func GetReleaseAttachment(ctx *context.APIContext) { return } // FIXME Should prove the existence of the given repo, but results in unnecessary database requests - ctx.JSON(200, attach.APIFormat()) + ctx.JSON(http.StatusOK, attach.APIFormat()) } // ListReleaseAttachments lists all attachments of the release @@ -91,10 +93,11 @@ func ListReleaseAttachments(ctx *context.APIContext) { // responses: // "200": // "$ref": "#/responses/AttachmentList" + releaseID := ctx.ParamsInt64(":id") release, err := models.GetReleaseByID(releaseID) if err != nil { - ctx.Error(500, "GetReleaseByID", err) + ctx.Error(http.StatusInternalServerError, "GetReleaseByID", err) return } if release.RepoID != ctx.Repo.Repository.ID { @@ -102,10 +105,10 @@ func ListReleaseAttachments(ctx *context.APIContext) { return } if err := release.LoadAttributes(); err != nil { - ctx.Error(500, "LoadAttributes", err) + ctx.Error(http.StatusInternalServerError, "LoadAttributes", err) return } - ctx.JSON(200, release.APIFormat().Attachments) + ctx.JSON(http.StatusOK, release.APIFormat().Attachments) } // CreateReleaseAttachment creates an attachment and saves the given file @@ -147,6 +150,8 @@ func CreateReleaseAttachment(ctx *context.APIContext) { // responses: // "201": // "$ref": "#/responses/Attachment" + // "400": + // "$ref": "#/responses/error" // Check if attachments are enabled if !setting.AttachmentEnabled { @@ -158,14 +163,14 @@ func CreateReleaseAttachment(ctx *context.APIContext) { releaseID := ctx.ParamsInt64(":id") release, err := models.GetReleaseByID(releaseID) if err != nil { - ctx.Error(500, "GetReleaseByID", err) + ctx.Error(http.StatusInternalServerError, "GetReleaseByID", err) return } // Get uploaded file from request file, header, err := ctx.GetFile("attachment") if err != nil { - ctx.Error(500, "GetFile", err) + ctx.Error(http.StatusInternalServerError, "GetFile", err) return } defer file.Close() @@ -179,7 +184,7 @@ func CreateReleaseAttachment(ctx *context.APIContext) { // Check if the filetype is allowed by the settings err = upload.VerifyAllowedContentType(buf, strings.Split(setting.AttachmentAllowedTypes, ",")) if err != nil { - ctx.Error(400, "DetectContentType", err) + ctx.Error(http.StatusBadRequest, "DetectContentType", err) return } @@ -195,11 +200,11 @@ func CreateReleaseAttachment(ctx *context.APIContext) { ReleaseID: release.ID, }, buf, file) if err != nil { - ctx.Error(500, "NewAttachment", err) + ctx.Error(http.StatusInternalServerError, "NewAttachment", err) return } - ctx.JSON(201, attach.APIFormat()) + ctx.JSON(http.StatusCreated, attach.APIFormat()) } // EditReleaseAttachment updates the given attachment @@ -247,7 +252,7 @@ func EditReleaseAttachment(ctx *context.APIContext, form api.EditAttachmentOptio attachID := ctx.ParamsInt64(":asset") attach, err := models.GetAttachmentByID(attachID) if err != nil { - ctx.Error(500, "GetAttachmentByID", err) + ctx.Error(http.StatusInternalServerError, "GetAttachmentByID", err) return } if attach.ReleaseID != releaseID { @@ -261,9 +266,9 @@ func EditReleaseAttachment(ctx *context.APIContext, form api.EditAttachmentOptio } if err := models.UpdateAttachment(attach); err != nil { - ctx.Error(500, "UpdateAttachment", attach) + ctx.Error(http.StatusInternalServerError, "UpdateAttachment", attach) } - ctx.JSON(201, attach.APIFormat()) + ctx.JSON(http.StatusCreated, attach.APIFormat()) } // DeleteReleaseAttachment delete a given attachment @@ -305,7 +310,7 @@ func DeleteReleaseAttachment(ctx *context.APIContext) { attachID := ctx.ParamsInt64(":asset") attach, err := models.GetAttachmentByID(attachID) if err != nil { - ctx.Error(500, "GetAttachmentByID", err) + ctx.Error(http.StatusInternalServerError, "GetAttachmentByID", err) return } if attach.ReleaseID != releaseID { @@ -316,8 +321,8 @@ func DeleteReleaseAttachment(ctx *context.APIContext) { // FIXME Should prove the existence of the given repo, but results in unnecessary database requests if err := models.DeleteAttachment(attach, true); err != nil { - ctx.Error(500, "DeleteAttachment", err) + ctx.Error(http.StatusInternalServerError, "DeleteAttachment", err) return } - ctx.Status(204) + ctx.Status(http.StatusNoContent) } |