From b1ff386e2418ed6a7f183e756b13277d701278ef Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 19 Dec 2019 23:55:23 +0100 Subject: [PATCH] use numbers and not http.Status___ enum --- routers/api/v1/misc/markdown.go | 9 +++--- routers/api/v1/misc/markdown_test.go | 1 - routers/api/v1/misc/signing.go | 3 +- routers/api/v1/repo/blob.go | 7 ++--- routers/api/v1/repo/file.go | 31 ++++++++++----------- routers/api/v1/repo/hook_test.go | 3 +- routers/api/v1/repo/issue.go | 5 ++-- routers/api/v1/repo/pull.go | 15 +++++----- routers/api/v1/repo/repo.go | 37 ++++++++++++------------- routers/api/v1/repo/repo_test.go | 5 ++-- routers/api/v1/repo/tag.go | 9 +++--- routers/api/v1/repo/topic.go | 41 ++++++++++++++-------------- routers/api/v1/user/user.go | 7 ++--- routers/api/v1/utils/hook.go | 5 ++-- 14 files changed, 82 insertions(+), 96 deletions(-) diff --git a/routers/api/v1/misc/markdown.go b/routers/api/v1/misc/markdown.go index 23f0b168d0..3d099ae0c0 100644 --- a/routers/api/v1/misc/markdown.go +++ b/routers/api/v1/misc/markdown.go @@ -5,7 +5,6 @@ package misc import ( - "net/http" "strings" "code.gitea.io/gitea/modules/context" @@ -65,20 +64,20 @@ func Markdown(ctx *context.APIContext, form api.MarkdownOption) { if form.Wiki { _, err := ctx.Write([]byte(markdown.RenderWiki(md, urlPrefix, meta))) if err != nil { - ctx.Error(http.StatusInternalServerError, "", err) + ctx.Error(500, "", err) return } } else { _, err := ctx.Write(markdown.Render(md, urlPrefix, meta)) if err != nil { - ctx.Error(http.StatusInternalServerError, "", err) + ctx.Error(500, "", err) return } } default: _, err := ctx.Write(markdown.RenderRaw([]byte(form.Text), "", false)) if err != nil { - ctx.Error(http.StatusInternalServerError, "", err) + ctx.Error(500, "", err) return } } @@ -112,7 +111,7 @@ func MarkdownRaw(ctx *context.APIContext) { } _, err = ctx.Write(markdown.RenderRaw(body, "", false)) if err != nil { - ctx.Error(http.StatusInternalServerError, "", err) + ctx.Error(500, "", err) return } } diff --git a/routers/api/v1/misc/markdown_test.go b/routers/api/v1/misc/markdown_test.go index 47e99d2f06..ed80023bc2 100644 --- a/routers/api/v1/misc/markdown_test.go +++ b/routers/api/v1/misc/markdown_test.go @@ -2,7 +2,6 @@ package misc import ( "io/ioutil" - "net/http" "net/http/httptest" "net/url" "strings" diff --git a/routers/api/v1/misc/signing.go b/routers/api/v1/misc/signing.go index f5428670af..e8bff381cf 100644 --- a/routers/api/v1/misc/signing.go +++ b/routers/api/v1/misc/signing.go @@ -2,7 +2,6 @@ package misc import ( "fmt" - "net/http" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" @@ -57,6 +56,6 @@ func SigningKey(ctx *context.Context) { _, err = ctx.Write([]byte(content)) if err != nil { log.Error("Error writing key content %v", err) - ctx.Error(http.StatusInternalServerError, fmt.Sprintf("%v", err)) + ctx.Error(500, fmt.Sprintf("%v", err)) } } diff --git a/routers/api/v1/repo/blob.go b/routers/api/v1/repo/blob.go index d6265e16ce..70f7a8bbe2 100644 --- a/routers/api/v1/repo/blob.go +++ b/routers/api/v1/repo/blob.go @@ -5,7 +5,6 @@ package repo import ( - "net/http" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/repofiles" @@ -40,12 +39,12 @@ func GetBlob(ctx *context.APIContext) { sha := ctx.Params("sha") if len(sha) == 0 { - ctx.Error(http.StatusBadRequest, "", "sha not provided") + ctx.Error(400, "", "sha not provided") return } if blob, err := repofiles.GetBlobBySHA(ctx.Repo.Repository, sha); err != nil { - ctx.Error(http.StatusBadRequest, "", err) + ctx.Error(400, "", err) } else { - ctx.JSON(http.StatusOK, blob) + ctx.JSON(200, blob) } } diff --git a/routers/api/v1/repo/file.go b/routers/api/v1/repo/file.go index 175235c5ef..20ef4f8841 100644 --- a/routers/api/v1/repo/file.go +++ b/routers/api/v1/repo/file.go @@ -7,7 +7,6 @@ package repo import ( "encoding/base64" - "net/http" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" @@ -53,12 +52,12 @@ func GetRawFile(ctx *context.APIContext) { if git.IsErrNotExist(err) { ctx.NotFound() } else { - ctx.Error(http.StatusInternalServerError, "GetBlobByPath", err) + ctx.Error(500, "GetBlobByPath", err) } return } if err = repo.ServeBlob(ctx.Context, blob); err != nil { - ctx.Error(http.StatusInternalServerError, "ServeBlob", err) + ctx.Error(500, "ServeBlob", err) } } @@ -91,7 +90,7 @@ func GetArchive(ctx *context.APIContext) { repoPath := models.RepoPath(ctx.Params(":username"), ctx.Params(":reponame")) gitRepo, err := git.OpenRepository(repoPath) if err != nil { - ctx.Error(http.StatusInternalServerError, "OpenRepository", err) + ctx.Error(500, "OpenRepository", err) return } ctx.Repo.GitRepo = gitRepo @@ -131,7 +130,7 @@ func GetEditorconfig(ctx *context.APIContext) { if git.IsErrNotExist(err) { ctx.NotFound(err) } else { - ctx.Error(http.StatusInternalServerError, "GetEditorconfig", err) + ctx.Error(500, "GetEditorconfig", err) } return } @@ -142,7 +141,7 @@ func GetEditorconfig(ctx *context.APIContext) { ctx.NotFound(err) return } - ctx.JSON(http.StatusOK, def) + ctx.JSON(200, def) } // CanWriteFiles returns true if repository is editable and user has proper access level. @@ -211,9 +210,9 @@ func CreateFile(ctx *context.APIContext, apiOpts api.CreateFileOptions) { } if fileResponse, err := createOrUpdateFile(ctx, opts); err != nil { - ctx.Error(http.StatusInternalServerError, "CreateFile", err) + ctx.Error(500, "CreateFile", err) } else { - ctx.JSON(http.StatusCreated, fileResponse) + ctx.JSON(201, fileResponse) } } @@ -275,9 +274,9 @@ func UpdateFile(ctx *context.APIContext, apiOpts api.UpdateFileOptions) { } if fileResponse, err := createOrUpdateFile(ctx, opts); err != nil { - ctx.Error(http.StatusInternalServerError, "UpdateFile", err) + ctx.Error(500, "UpdateFile", err) } else { - ctx.JSON(http.StatusOK, fileResponse) + ctx.JSON(200, fileResponse) } } @@ -333,7 +332,7 @@ func DeleteFile(ctx *context.APIContext, apiOpts api.DeleteFileOptions) { // "200": // "$ref": "#/responses/FileDeleteResponse" if !CanWriteFiles(ctx.Repo) { - ctx.Error(http.StatusInternalServerError, "DeleteFile", models.ErrUserDoesNotHaveAccessToRepo{ + ctx.Error(500, "DeleteFile", models.ErrUserDoesNotHaveAccessToRepo{ UserID: ctx.User.ID, RepoName: ctx.Repo.Repository.LowerName, }) @@ -361,9 +360,9 @@ func DeleteFile(ctx *context.APIContext, apiOpts api.DeleteFileOptions) { } if fileResponse, err := repofiles.DeleteRepoFile(ctx.Repo.Repository, ctx.User, opts); err != nil { - ctx.Error(http.StatusInternalServerError, "DeleteFile", err) + ctx.Error(500, "DeleteFile", err) } else { - ctx.JSON(http.StatusOK, fileResponse) + ctx.JSON(200, fileResponse) } } @@ -400,7 +399,7 @@ func GetContents(ctx *context.APIContext) { // "$ref": "#/responses/ContentsResponse" if !CanReadFiles(ctx.Repo) { - ctx.Error(http.StatusInternalServerError, "GetContentsOrList", models.ErrUserDoesNotHaveAccessToRepo{ + ctx.Error(500, "GetContentsOrList", models.ErrUserDoesNotHaveAccessToRepo{ UserID: ctx.User.ID, RepoName: ctx.Repo.Repository.LowerName, }) @@ -411,9 +410,9 @@ func GetContents(ctx *context.APIContext) { ref := ctx.QueryTrim("ref") if fileList, err := repofiles.GetContentsOrList(ctx.Repo.Repository, treePath, ref); err != nil { - ctx.Error(http.StatusInternalServerError, "GetContentsOrList", err) + ctx.Error(500, "GetContentsOrList", err) } else { - ctx.JSON(http.StatusOK, fileList) + ctx.JSON(200, fileList) } } diff --git a/routers/api/v1/repo/hook_test.go b/routers/api/v1/repo/hook_test.go index 8ed4bc4b0c..ec68df6697 100644 --- a/routers/api/v1/repo/hook_test.go +++ b/routers/api/v1/repo/hook_test.go @@ -5,7 +5,6 @@ package repo import ( - "net/http" "testing" "code.gitea.io/gitea/models" @@ -24,7 +23,7 @@ func TestTestHook(t *testing.T) { test.LoadRepoCommit(t, ctx) test.LoadUser(t, ctx, 2) TestHook(&context.APIContext{Context: ctx, Org: nil}) - assert.EqualValues(t, http.StatusNoContent, ctx.Resp.Status()) + assert.EqualValues(t, 204, ctx.Resp.Status()) models.AssertExistsAndLoadBean(t, &models.HookTask{ RepoID: 1, diff --git a/routers/api/v1/repo/issue.go b/routers/api/v1/repo/issue.go index 6972d447a6..3c5e02e362 100644 --- a/routers/api/v1/repo/issue.go +++ b/routers/api/v1/repo/issue.go @@ -7,7 +7,6 @@ package repo import ( "fmt" - "net/http" "strings" "time" @@ -379,7 +378,7 @@ func CreateIssue(ctx *context.APIContext, form api.CreateIssueOption) { if form.Closed { if err := issue_service.ChangeStatus(issue, ctx.User, true); err != nil { if models.IsErrDependenciesLeft(err) { - ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this issue because it still has open dependencies") + ctx.Error(412, "DependenciesLeft", "cannot close this issue because it still has open dependencies") return } ctx.Error(500, "ChangeStatus", err) @@ -513,7 +512,7 @@ func EditIssue(ctx *context.APIContext, form api.EditIssueOption) { if form.State != nil { if err = issue_service.ChangeStatus(issue, ctx.User, api.StateClosed == api.StateType(*form.State)); err != nil { if models.IsErrDependenciesLeft(err) { - ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this issue because it still has open dependencies") + ctx.Error(412, "DependenciesLeft", "cannot close this issue because it still has open dependencies") return } ctx.Error(500, "ChangeStatus", err) diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index 1c273b7dc9..aa7b5edf5d 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -6,7 +6,6 @@ package repo import ( "fmt" - "net/http" "strings" "time" @@ -360,7 +359,7 @@ func EditPullRequest(ctx *context.APIContext, form api.EditPullRequestOption) { err = pr.LoadIssue() if err != nil { - ctx.Error(http.StatusInternalServerError, "LoadIssue", err) + ctx.Error(500, "LoadIssue", err) return } issue := pr.Issue @@ -443,7 +442,7 @@ func EditPullRequest(ctx *context.APIContext, form api.EditPullRequestOption) { if form.State != nil { if err = issue_service.ChangeStatus(issue, ctx.User, api.StateClosed == api.StateType(*form.State)); err != nil { if models.IsErrDependenciesLeft(err) { - ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this pull request because it still has open dependencies") + ctx.Error(412, "DependenciesLeft", "cannot close this pull request because it still has open dependencies") return } ctx.Error(500, "ChangeStatus", err) @@ -561,7 +560,7 @@ func MergePullRequest(ctx *context.APIContext, form auth.MergePullRequestForm) { err = pr.LoadIssue() if err != nil { - ctx.Error(http.StatusInternalServerError, "LoadIssue", err) + ctx.Error(500, "LoadIssue", err) return } pr.Issue.Repo = ctx.Repo.Repository @@ -620,15 +619,15 @@ func MergePullRequest(ctx *context.APIContext, form auth.MergePullRequestForm) { return } else if models.IsErrMergeConflicts(err) { conflictError := err.(models.ErrMergeConflicts) - ctx.JSON(http.StatusConflict, conflictError) + ctx.JSON(409, conflictError) } else if models.IsErrRebaseConflicts(err) { conflictError := err.(models.ErrRebaseConflicts) - ctx.JSON(http.StatusConflict, conflictError) + ctx.JSON(409, conflictError) } else if models.IsErrMergeUnrelatedHistories(err) { conflictError := err.(models.ErrMergeUnrelatedHistories) - ctx.JSON(http.StatusConflict, conflictError) + ctx.JSON(409, conflictError) } else if models.IsErrMergePushOutOfDate(err) { - ctx.Status(http.StatusConflict) + ctx.Status(409) return } ctx.Error(500, "Merge", err) diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index be226c3438..4f789e4ef4 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -9,7 +9,6 @@ import ( "bytes" "errors" "fmt" - "net/http" "net/url" "strings" @@ -162,7 +161,7 @@ func Search(ctx *context.APIContext) { opts.Collaborate = util.OptionalBoolTrue case "": default: - ctx.Error(http.StatusUnprocessableEntity, "", fmt.Errorf("Invalid search mode: \"%s\"", mode)) + ctx.Error(422, "", fmt.Errorf("Invalid search mode: \"%s\"", mode)) return } @@ -176,11 +175,11 @@ func Search(ctx *context.APIContext) { if orderBy, ok := searchModeMap[sortMode]; ok { opts.OrderBy = orderBy } else { - ctx.Error(http.StatusUnprocessableEntity, "", fmt.Errorf("Invalid sort mode: \"%s\"", sortMode)) + ctx.Error(422, "", fmt.Errorf("Invalid sort mode: \"%s\"", sortMode)) return } } else { - ctx.Error(http.StatusUnprocessableEntity, "", fmt.Errorf("Invalid sort order: \"%s\"", sortOrder)) + ctx.Error(422, "", fmt.Errorf("Invalid sort order: \"%s\"", sortOrder)) return } } @@ -626,7 +625,7 @@ func Edit(ctx *context.APIContext, opts api.EditRepoOption) { } } - ctx.JSON(http.StatusOK, ctx.Repo.Repository.APIFormat(ctx.Repo.AccessMode)) + ctx.JSON(200, ctx.Repo.Repository.APIFormat(ctx.Repo.AccessMode)) } // updateBasicProperties updates the basic properties of a repo: Name, Description, Website and Visibility @@ -642,13 +641,13 @@ func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) err if err := repo_service.ChangeRepositoryName(ctx.User, repo, newRepoName); err != nil { switch { case models.IsErrRepoAlreadyExist(err): - ctx.Error(http.StatusUnprocessableEntity, fmt.Sprintf("repo name is already taken [name: %s]", newRepoName), err) + ctx.Error(422, fmt.Sprintf("repo name is already taken [name: %s]", newRepoName), err) case models.IsErrNameReserved(err): - ctx.Error(http.StatusUnprocessableEntity, fmt.Sprintf("repo name is reserved [name: %s]", newRepoName), err) + ctx.Error(422, fmt.Sprintf("repo name is reserved [name: %s]", newRepoName), err) case models.IsErrNamePatternNotAllowed(err): - ctx.Error(http.StatusUnprocessableEntity, fmt.Sprintf("repo name's pattern is not allowed [name: %s, pattern: %s]", newRepoName, err.(models.ErrNamePatternNotAllowed).Pattern), err) + ctx.Error(422, fmt.Sprintf("repo name's pattern is not allowed [name: %s, pattern: %s]", newRepoName, err.(models.ErrNamePatternNotAllowed).Pattern), err) default: - ctx.Error(http.StatusUnprocessableEntity, "ChangeRepositoryName", err) + ctx.Error(422, "ChangeRepositoryName", err) } return err } @@ -678,7 +677,7 @@ func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) err // when ForcePrivate enabled, you could change public repo to private, but only admin users can change private to public if visibilityChanged && setting.Repository.ForcePrivate && !*opts.Private && !ctx.User.IsAdmin { err := fmt.Errorf("cannot change private repository to public") - ctx.Error(http.StatusUnprocessableEntity, "Force Private enabled", err) + ctx.Error(422, "Force Private enabled", err) return err } @@ -693,7 +692,7 @@ func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) err if opts.DefaultBranch != nil && repo.DefaultBranch != *opts.DefaultBranch && ctx.Repo.GitRepo.IsBranchExist(*opts.DefaultBranch) { if err := ctx.Repo.GitRepo.SetDefaultBranch(*opts.DefaultBranch); err != nil { if !git.IsErrUnsupportedVersion(err) { - ctx.Error(http.StatusInternalServerError, "SetDefaultBranch", err) + ctx.Error(500, "SetDefaultBranch", err) return err } } @@ -701,7 +700,7 @@ func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) err } if err := models.UpdateRepository(repo, visibilityChanged); err != nil { - ctx.Error(http.StatusInternalServerError, "UpdateRepository", err) + ctx.Error(500, "UpdateRepository", err) return err } @@ -737,12 +736,12 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error { // Check that values are valid if !validation.IsValidExternalURL(opts.ExternalTracker.ExternalTrackerURL) { err := fmt.Errorf("External tracker URL not valid") - ctx.Error(http.StatusUnprocessableEntity, "Invalid external tracker URL", err) + ctx.Error(422, "Invalid external tracker URL", err) return err } if len(opts.ExternalTracker.ExternalTrackerFormat) != 0 && !validation.IsValidExternalTrackerURLFormat(opts.ExternalTracker.ExternalTrackerFormat) { err := fmt.Errorf("External tracker URL format not valid") - ctx.Error(http.StatusUnprocessableEntity, "Invalid external tracker URL format", err) + ctx.Error(422, "Invalid external tracker URL format", err) return err } @@ -797,7 +796,7 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error { // Check that values are valid if !validation.IsValidExternalURL(opts.ExternalWiki.ExternalWikiURL) { err := fmt.Errorf("External wiki URL not valid") - ctx.Error(http.StatusUnprocessableEntity, "", "Invalid external wiki URL") + ctx.Error(422, "", "Invalid external wiki URL") return err } @@ -866,7 +865,7 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error { } if err := models.UpdateRepositoryUnits(repo, units); err != nil { - ctx.Error(http.StatusInternalServerError, "UpdateRepositoryUnits", err) + ctx.Error(500, "UpdateRepositoryUnits", err) return err } @@ -881,20 +880,20 @@ func updateRepoArchivedState(ctx *context.APIContext, opts api.EditRepoOption) e if opts.Archived != nil { if repo.IsMirror { err := fmt.Errorf("repo is a mirror, cannot archive/un-archive") - ctx.Error(http.StatusUnprocessableEntity, err.Error(), err) + ctx.Error(422, err.Error(), err) return err } if *opts.Archived { if err := repo.SetArchiveRepoState(*opts.Archived); err != nil { log.Error("Tried to archive a repo: %s", err) - ctx.Error(http.StatusInternalServerError, "ArchiveRepoState", err) + ctx.Error(500, "ArchiveRepoState", err) return err } log.Trace("Repository was archived: %s/%s", ctx.Repo.Owner.Name, repo.Name) } else { if err := repo.SetArchiveRepoState(*opts.Archived); err != nil { log.Error("Tried to un-archive a repo: %s", err) - ctx.Error(http.StatusInternalServerError, "ArchiveRepoState", err) + ctx.Error(500, "ArchiveRepoState", err) return err } log.Trace("Repository was un-archived: %s/%s", ctx.Repo.Owner.Name, repo.Name) diff --git a/routers/api/v1/repo/repo_test.go b/routers/api/v1/repo/repo_test.go index 053134ec61..74ea884e5a 100644 --- a/routers/api/v1/repo/repo_test.go +++ b/routers/api/v1/repo/repo_test.go @@ -5,7 +5,6 @@ package repo import ( - "net/http" "testing" "code.gitea.io/gitea/models" @@ -55,7 +54,7 @@ func TestRepoEdit(t *testing.T) { Edit(&context.APIContext{Context: ctx, Org: nil}, opts) - assert.EqualValues(t, http.StatusOK, ctx.Resp.Status()) + assert.EqualValues(t, 200, ctx.Resp.Status()) models.AssertExistsAndLoadBean(t, &models.Repository{ ID: 1, }, models.Cond("name = ? AND is_archived = 1", *opts.Name)) @@ -74,7 +73,7 @@ func TestRepoEditNameChange(t *testing.T) { } Edit(&context.APIContext{Context: ctx, Org: nil}, opts) - assert.EqualValues(t, http.StatusOK, ctx.Resp.Status()) + assert.EqualValues(t, 200, ctx.Resp.Status()) models.AssertExistsAndLoadBean(t, &models.Repository{ ID: 1, diff --git a/routers/api/v1/repo/tag.go b/routers/api/v1/repo/tag.go index 0a764113ab..9f8b749afd 100644 --- a/routers/api/v1/repo/tag.go +++ b/routers/api/v1/repo/tag.go @@ -5,7 +5,6 @@ package repo import ( - "net/http" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" @@ -76,17 +75,17 @@ func GetTag(ctx *context.APIContext) { sha := ctx.Params("sha") if len(sha) == 0 { - ctx.Error(http.StatusBadRequest, "", "SHA not provided") + ctx.Error(400, "", "SHA not provided") return } if tag, err := ctx.Repo.GitRepo.GetAnnotatedTag(sha); err != nil { - ctx.Error(http.StatusBadRequest, "GetTag", err) + ctx.Error(400, "GetTag", err) } else { commit, err := tag.Commit() if err != nil { - ctx.Error(http.StatusBadRequest, "GetTag", err) + ctx.Error(400, "GetTag", err) } - ctx.JSON(http.StatusOK, convert.ToAnnotatedTag(ctx.Repo.Repository, tag, commit)) + ctx.JSON(200, convert.ToAnnotatedTag(ctx.Repo.Repository, tag, commit)) } } diff --git a/routers/api/v1/repo/topic.go b/routers/api/v1/repo/topic.go index 1656fd1b16..65701d4ff5 100644 --- a/routers/api/v1/repo/topic.go +++ b/routers/api/v1/repo/topic.go @@ -5,7 +5,6 @@ package repo import ( - "net/http" "strings" "code.gitea.io/gitea/models" @@ -42,7 +41,7 @@ func ListTopics(ctx *context.APIContext) { }) if err != nil { log.Error("ListTopics failed: %v", err) - ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ + ctx.JSON(500, map[string]interface{}{ "message": "ListTopics failed.", }) return @@ -52,7 +51,7 @@ func ListTopics(ctx *context.APIContext) { for i, topic := range topics { topicNames[i] = topic.Name } - ctx.JSON(http.StatusOK, map[string]interface{}{ + ctx.JSON(200, map[string]interface{}{ "topics": topicNames, }) } @@ -82,20 +81,20 @@ func UpdateTopics(ctx *context.APIContext, form api.RepoTopicOptions) { // responses: // "204": // "$ref": "#/responses/empty" + // "422": + // "$ref": "#/responses/validationError" topicNames := form.Topics validTopics, invalidTopics := models.SanitizeAndValidateTopics(topicNames) if len(validTopics) > 25 { - ctx.JSON(http.StatusUnprocessableEntity, map[string]interface{}{ - "invalidTopics": nil, - "message": "Exceeding maximum number of topics per repo", - }) + ctx.Error(422, "", "Exceeding maximum number of topics per repo") return } if len(invalidTopics) > 0 { - ctx.JSON(http.StatusUnprocessableEntity, map[string]interface{}{ + ctx.JSON(422, map[string]interface{}{ + "invalidTopics": invalidTopics, "message": "Topic names are invalid", }) @@ -105,13 +104,13 @@ func UpdateTopics(ctx *context.APIContext, form api.RepoTopicOptions) { err := models.SaveTopics(ctx.Repo.Repository.ID, validTopics...) if err != nil { log.Error("SaveTopics failed: %v", err) - ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ + ctx.JSON(500, map[string]interface{}{ "message": "Save topics failed.", }) return } - ctx.Status(http.StatusNoContent) + ctx.Status(204) } // AddTopic adds a topic name to a repo @@ -144,7 +143,7 @@ func AddTopic(ctx *context.APIContext) { topicName := strings.TrimSpace(strings.ToLower(ctx.Params(":topic"))) if !models.ValidateTopic(topicName) { - ctx.Error(http.StatusUnprocessableEntity, "", "Topic name is invalid") + ctx.Error(422, "", "Topic name is invalid") return } @@ -154,13 +153,13 @@ func AddTopic(ctx *context.APIContext) { }) if err != nil { log.Error("AddTopic failed: %v", err) - ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ + ctx.JSON(500, map[string]interface{}{ "message": "ListTopics failed.", }) return } if len(topics) >= 25 { - ctx.JSON(http.StatusUnprocessableEntity, map[string]interface{}{ + ctx.JSON(422, map[string]interface{}{ "message": "Exceeding maximum allowed topics per repo.", }) return @@ -169,13 +168,13 @@ func AddTopic(ctx *context.APIContext) { _, err = models.AddTopic(ctx.Repo.Repository.ID, topicName) if err != nil { log.Error("AddTopic failed: %v", err) - ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ + ctx.JSON(500, map[string]interface{}{ "message": "AddTopic failed.", }) return } - ctx.Status(http.StatusNoContent) + ctx.Status(204) } // DeleteTopic removes topic name from repo @@ -207,14 +206,14 @@ func DeleteTopic(ctx *context.APIContext) { topicName := strings.TrimSpace(strings.ToLower(ctx.Params(":topic"))) if !models.ValidateTopic(topicName) { - ctx.Error(http.StatusUnprocessableEntity, "", "Topic name is invalid") + ctx.Error(422, "", "Topic name is invalid") return } topic, err := models.DeleteTopic(ctx.Repo.Repository.ID, topicName) if err != nil { log.Error("DeleteTopic failed: %v", err) - ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ + ctx.JSON(500, map[string]interface{}{ "message": "DeleteTopic failed.", }) return @@ -224,7 +223,7 @@ func DeleteTopic(ctx *context.APIContext) { ctx.NotFound() } - ctx.Status(http.StatusNoContent) + ctx.Status(204) } // TopicSearch search for creating topic @@ -244,7 +243,7 @@ func TopicSearch(ctx *context.Context) { // "200": // "$ref": "#/responses/TopicListResponse" if ctx.User == nil { - ctx.JSON(http.StatusForbidden, map[string]interface{}{ + ctx.JSON(403, map[string]interface{}{ "message": "Only owners could change the topics.", }) return @@ -258,7 +257,7 @@ func TopicSearch(ctx *context.Context) { }) if err != nil { log.Error("SearchTopics failed: %v", err) - ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ + ctx.JSON(500, map[string]interface{}{ "message": "Search topics failed.", }) return @@ -268,7 +267,7 @@ func TopicSearch(ctx *context.Context) { for i, topic := range topics { topicResponses[i] = convert.ToTopicResponse(topic) } - ctx.JSON(http.StatusOK, map[string]interface{}{ + ctx.JSON(200, map[string]interface{}{ "topics": topicResponses, }) } diff --git a/routers/api/v1/user/user.go b/routers/api/v1/user/user.go index 6f3fc4d32b..a22be39b51 100644 --- a/routers/api/v1/user/user.go +++ b/routers/api/v1/user/user.go @@ -5,7 +5,6 @@ package user import ( - "net/http" "strings" "code.gitea.io/gitea/models" @@ -143,16 +142,16 @@ func GetUserHeatmapData(ctx *context.APIContext) { user, err := models.GetUserByName(ctx.Params(":username")) if err != nil { if models.IsErrUserNotExist(err) { - ctx.Status(http.StatusNotFound) + ctx.Status(404) } else { - ctx.Error(http.StatusInternalServerError, "GetUserByName", err) + ctx.Error(500, "GetUserByName", err) } return } heatmap, err := models.GetUserHeatmapDataByUser(user) if err != nil { - ctx.Error(http.StatusInternalServerError, "GetUserHeatmapDataByUser", err) + ctx.Error(500, "GetUserHeatmapDataByUser", err) return } ctx.JSON(200, heatmap) diff --git a/routers/api/v1/utils/hook.go b/routers/api/v1/utils/hook.go index f88b152003..1f2a4fafc6 100644 --- a/routers/api/v1/utils/hook.go +++ b/routers/api/v1/utils/hook.go @@ -6,7 +6,6 @@ package utils import ( "encoding/json" - "net/http" "strings" "code.gitea.io/gitea/models" @@ -74,7 +73,7 @@ func AddOrgHook(ctx *context.APIContext, form *api.CreateHookOption) { org := ctx.Org.Organization hook, ok := addHook(ctx, form, org.ID, 0) if ok { - ctx.JSON(http.StatusCreated, convert.ToHook(org.HomeLink(), hook)) + ctx.JSON(201, convert.ToHook(org.HomeLink(), hook)) } } @@ -83,7 +82,7 @@ func AddRepoHook(ctx *context.APIContext, form *api.CreateHookOption) { repo := ctx.Repo hook, ok := addHook(ctx, form, 0, repo.Repository.ID) if ok { - ctx.JSON(http.StatusCreated, convert.ToHook(repo.RepoLink, hook)) + ctx.JSON(201, convert.ToHook(repo.RepoLink, hook)) } }