1
0
Fork 0

use numbers and not http.Status___ enum

This commit is contained in:
6543 2019-12-19 23:55:23 +01:00
parent e2fc0a0dfd
commit b1ff386e24
No known key found for this signature in database
GPG key ID: A1CA74D27FD13271
14 changed files with 82 additions and 96 deletions

View file

@ -5,7 +5,6 @@
package misc package misc
import ( import (
"net/http"
"strings" "strings"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
@ -65,20 +64,20 @@ func Markdown(ctx *context.APIContext, form api.MarkdownOption) {
if form.Wiki { if form.Wiki {
_, err := ctx.Write([]byte(markdown.RenderWiki(md, urlPrefix, meta))) _, err := ctx.Write([]byte(markdown.RenderWiki(md, urlPrefix, meta)))
if err != nil { if err != nil {
ctx.Error(http.StatusInternalServerError, "", err) ctx.Error(500, "", err)
return return
} }
} else { } else {
_, err := ctx.Write(markdown.Render(md, urlPrefix, meta)) _, err := ctx.Write(markdown.Render(md, urlPrefix, meta))
if err != nil { if err != nil {
ctx.Error(http.StatusInternalServerError, "", err) ctx.Error(500, "", err)
return return
} }
} }
default: default:
_, err := ctx.Write(markdown.RenderRaw([]byte(form.Text), "", false)) _, err := ctx.Write(markdown.RenderRaw([]byte(form.Text), "", false))
if err != nil { if err != nil {
ctx.Error(http.StatusInternalServerError, "", err) ctx.Error(500, "", err)
return return
} }
} }
@ -112,7 +111,7 @@ func MarkdownRaw(ctx *context.APIContext) {
} }
_, err = ctx.Write(markdown.RenderRaw(body, "", false)) _, err = ctx.Write(markdown.RenderRaw(body, "", false))
if err != nil { if err != nil {
ctx.Error(http.StatusInternalServerError, "", err) ctx.Error(500, "", err)
return return
} }
} }

View file

@ -2,7 +2,6 @@ package misc
import ( import (
"io/ioutil" "io/ioutil"
"net/http"
"net/http/httptest" "net/http/httptest"
"net/url" "net/url"
"strings" "strings"

View file

@ -2,7 +2,6 @@ package misc
import ( import (
"fmt" "fmt"
"net/http"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
@ -57,6 +56,6 @@ func SigningKey(ctx *context.Context) {
_, err = ctx.Write([]byte(content)) _, err = ctx.Write([]byte(content))
if err != nil { if err != nil {
log.Error("Error writing key content %v", err) log.Error("Error writing key content %v", err)
ctx.Error(http.StatusInternalServerError, fmt.Sprintf("%v", err)) ctx.Error(500, fmt.Sprintf("%v", err))
} }
} }

View file

@ -5,7 +5,6 @@
package repo package repo
import ( import (
"net/http"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/repofiles" "code.gitea.io/gitea/modules/repofiles"
@ -40,12 +39,12 @@ func GetBlob(ctx *context.APIContext) {
sha := ctx.Params("sha") sha := ctx.Params("sha")
if len(sha) == 0 { if len(sha) == 0 {
ctx.Error(http.StatusBadRequest, "", "sha not provided") ctx.Error(400, "", "sha not provided")
return return
} }
if blob, err := repofiles.GetBlobBySHA(ctx.Repo.Repository, sha); err != nil { if blob, err := repofiles.GetBlobBySHA(ctx.Repo.Repository, sha); err != nil {
ctx.Error(http.StatusBadRequest, "", err) ctx.Error(400, "", err)
} else { } else {
ctx.JSON(http.StatusOK, blob) ctx.JSON(200, blob)
} }
} }

View file

@ -7,7 +7,6 @@ package repo
import ( import (
"encoding/base64" "encoding/base64"
"net/http"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
@ -53,12 +52,12 @@ func GetRawFile(ctx *context.APIContext) {
if git.IsErrNotExist(err) { if git.IsErrNotExist(err) {
ctx.NotFound() ctx.NotFound()
} else { } else {
ctx.Error(http.StatusInternalServerError, "GetBlobByPath", err) ctx.Error(500, "GetBlobByPath", err)
} }
return return
} }
if err = repo.ServeBlob(ctx.Context, blob); err != nil { 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")) repoPath := models.RepoPath(ctx.Params(":username"), ctx.Params(":reponame"))
gitRepo, err := git.OpenRepository(repoPath) gitRepo, err := git.OpenRepository(repoPath)
if err != nil { if err != nil {
ctx.Error(http.StatusInternalServerError, "OpenRepository", err) ctx.Error(500, "OpenRepository", err)
return return
} }
ctx.Repo.GitRepo = gitRepo ctx.Repo.GitRepo = gitRepo
@ -131,7 +130,7 @@ func GetEditorconfig(ctx *context.APIContext) {
if git.IsErrNotExist(err) { if git.IsErrNotExist(err) {
ctx.NotFound(err) ctx.NotFound(err)
} else { } else {
ctx.Error(http.StatusInternalServerError, "GetEditorconfig", err) ctx.Error(500, "GetEditorconfig", err)
} }
return return
} }
@ -142,7 +141,7 @@ func GetEditorconfig(ctx *context.APIContext) {
ctx.NotFound(err) ctx.NotFound(err)
return return
} }
ctx.JSON(http.StatusOK, def) ctx.JSON(200, def)
} }
// CanWriteFiles returns true if repository is editable and user has proper access level. // 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 { if fileResponse, err := createOrUpdateFile(ctx, opts); err != nil {
ctx.Error(http.StatusInternalServerError, "CreateFile", err) ctx.Error(500, "CreateFile", err)
} else { } 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 { if fileResponse, err := createOrUpdateFile(ctx, opts); err != nil {
ctx.Error(http.StatusInternalServerError, "UpdateFile", err) ctx.Error(500, "UpdateFile", err)
} else { } else {
ctx.JSON(http.StatusOK, fileResponse) ctx.JSON(200, fileResponse)
} }
} }
@ -333,7 +332,7 @@ func DeleteFile(ctx *context.APIContext, apiOpts api.DeleteFileOptions) {
// "200": // "200":
// "$ref": "#/responses/FileDeleteResponse" // "$ref": "#/responses/FileDeleteResponse"
if !CanWriteFiles(ctx.Repo) { if !CanWriteFiles(ctx.Repo) {
ctx.Error(http.StatusInternalServerError, "DeleteFile", models.ErrUserDoesNotHaveAccessToRepo{ ctx.Error(500, "DeleteFile", models.ErrUserDoesNotHaveAccessToRepo{
UserID: ctx.User.ID, UserID: ctx.User.ID,
RepoName: ctx.Repo.Repository.LowerName, 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 { 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 { } else {
ctx.JSON(http.StatusOK, fileResponse) ctx.JSON(200, fileResponse)
} }
} }
@ -400,7 +399,7 @@ func GetContents(ctx *context.APIContext) {
// "$ref": "#/responses/ContentsResponse" // "$ref": "#/responses/ContentsResponse"
if !CanReadFiles(ctx.Repo) { if !CanReadFiles(ctx.Repo) {
ctx.Error(http.StatusInternalServerError, "GetContentsOrList", models.ErrUserDoesNotHaveAccessToRepo{ ctx.Error(500, "GetContentsOrList", models.ErrUserDoesNotHaveAccessToRepo{
UserID: ctx.User.ID, UserID: ctx.User.ID,
RepoName: ctx.Repo.Repository.LowerName, RepoName: ctx.Repo.Repository.LowerName,
}) })
@ -411,9 +410,9 @@ func GetContents(ctx *context.APIContext) {
ref := ctx.QueryTrim("ref") ref := ctx.QueryTrim("ref")
if fileList, err := repofiles.GetContentsOrList(ctx.Repo.Repository, treePath, ref); err != nil { if fileList, err := repofiles.GetContentsOrList(ctx.Repo.Repository, treePath, ref); err != nil {
ctx.Error(http.StatusInternalServerError, "GetContentsOrList", err) ctx.Error(500, "GetContentsOrList", err)
} else { } else {
ctx.JSON(http.StatusOK, fileList) ctx.JSON(200, fileList)
} }
} }

View file

@ -5,7 +5,6 @@
package repo package repo
import ( import (
"net/http"
"testing" "testing"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
@ -24,7 +23,7 @@ func TestTestHook(t *testing.T) {
test.LoadRepoCommit(t, ctx) test.LoadRepoCommit(t, ctx)
test.LoadUser(t, ctx, 2) test.LoadUser(t, ctx, 2)
TestHook(&context.APIContext{Context: ctx, Org: nil}) 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{ models.AssertExistsAndLoadBean(t, &models.HookTask{
RepoID: 1, RepoID: 1,

View file

@ -7,7 +7,6 @@ package repo
import ( import (
"fmt" "fmt"
"net/http"
"strings" "strings"
"time" "time"
@ -379,7 +378,7 @@ func CreateIssue(ctx *context.APIContext, form api.CreateIssueOption) {
if form.Closed { if form.Closed {
if err := issue_service.ChangeStatus(issue, ctx.User, true); err != nil { if err := issue_service.ChangeStatus(issue, ctx.User, true); err != nil {
if models.IsErrDependenciesLeft(err) { 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 return
} }
ctx.Error(500, "ChangeStatus", err) ctx.Error(500, "ChangeStatus", err)
@ -513,7 +512,7 @@ func EditIssue(ctx *context.APIContext, form api.EditIssueOption) {
if form.State != nil { if form.State != nil {
if err = issue_service.ChangeStatus(issue, ctx.User, api.StateClosed == api.StateType(*form.State)); err != nil { if err = issue_service.ChangeStatus(issue, ctx.User, api.StateClosed == api.StateType(*form.State)); err != nil {
if models.IsErrDependenciesLeft(err) { 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 return
} }
ctx.Error(500, "ChangeStatus", err) ctx.Error(500, "ChangeStatus", err)

View file

@ -6,7 +6,6 @@ package repo
import ( import (
"fmt" "fmt"
"net/http"
"strings" "strings"
"time" "time"
@ -360,7 +359,7 @@ func EditPullRequest(ctx *context.APIContext, form api.EditPullRequestOption) {
err = pr.LoadIssue() err = pr.LoadIssue()
if err != nil { if err != nil {
ctx.Error(http.StatusInternalServerError, "LoadIssue", err) ctx.Error(500, "LoadIssue", err)
return return
} }
issue := pr.Issue issue := pr.Issue
@ -443,7 +442,7 @@ func EditPullRequest(ctx *context.APIContext, form api.EditPullRequestOption) {
if form.State != nil { if form.State != nil {
if err = issue_service.ChangeStatus(issue, ctx.User, api.StateClosed == api.StateType(*form.State)); err != nil { if err = issue_service.ChangeStatus(issue, ctx.User, api.StateClosed == api.StateType(*form.State)); err != nil {
if models.IsErrDependenciesLeft(err) { 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 return
} }
ctx.Error(500, "ChangeStatus", err) ctx.Error(500, "ChangeStatus", err)
@ -561,7 +560,7 @@ func MergePullRequest(ctx *context.APIContext, form auth.MergePullRequestForm) {
err = pr.LoadIssue() err = pr.LoadIssue()
if err != nil { if err != nil {
ctx.Error(http.StatusInternalServerError, "LoadIssue", err) ctx.Error(500, "LoadIssue", err)
return return
} }
pr.Issue.Repo = ctx.Repo.Repository pr.Issue.Repo = ctx.Repo.Repository
@ -620,15 +619,15 @@ func MergePullRequest(ctx *context.APIContext, form auth.MergePullRequestForm) {
return return
} else if models.IsErrMergeConflicts(err) { } else if models.IsErrMergeConflicts(err) {
conflictError := err.(models.ErrMergeConflicts) conflictError := err.(models.ErrMergeConflicts)
ctx.JSON(http.StatusConflict, conflictError) ctx.JSON(409, conflictError)
} else if models.IsErrRebaseConflicts(err) { } else if models.IsErrRebaseConflicts(err) {
conflictError := err.(models.ErrRebaseConflicts) conflictError := err.(models.ErrRebaseConflicts)
ctx.JSON(http.StatusConflict, conflictError) ctx.JSON(409, conflictError)
} else if models.IsErrMergeUnrelatedHistories(err) { } else if models.IsErrMergeUnrelatedHistories(err) {
conflictError := err.(models.ErrMergeUnrelatedHistories) conflictError := err.(models.ErrMergeUnrelatedHistories)
ctx.JSON(http.StatusConflict, conflictError) ctx.JSON(409, conflictError)
} else if models.IsErrMergePushOutOfDate(err) { } else if models.IsErrMergePushOutOfDate(err) {
ctx.Status(http.StatusConflict) ctx.Status(409)
return return
} }
ctx.Error(500, "Merge", err) ctx.Error(500, "Merge", err)

View file

@ -9,7 +9,6 @@ import (
"bytes" "bytes"
"errors" "errors"
"fmt" "fmt"
"net/http"
"net/url" "net/url"
"strings" "strings"
@ -162,7 +161,7 @@ func Search(ctx *context.APIContext) {
opts.Collaborate = util.OptionalBoolTrue opts.Collaborate = util.OptionalBoolTrue
case "": case "":
default: default:
ctx.Error(http.StatusUnprocessableEntity, "", fmt.Errorf("Invalid search mode: \"%s\"", mode)) ctx.Error(422, "", fmt.Errorf("Invalid search mode: \"%s\"", mode))
return return
} }
@ -176,11 +175,11 @@ func Search(ctx *context.APIContext) {
if orderBy, ok := searchModeMap[sortMode]; ok { if orderBy, ok := searchModeMap[sortMode]; ok {
opts.OrderBy = orderBy opts.OrderBy = orderBy
} else { } else {
ctx.Error(http.StatusUnprocessableEntity, "", fmt.Errorf("Invalid sort mode: \"%s\"", sortMode)) ctx.Error(422, "", fmt.Errorf("Invalid sort mode: \"%s\"", sortMode))
return return
} }
} else { } else {
ctx.Error(http.StatusUnprocessableEntity, "", fmt.Errorf("Invalid sort order: \"%s\"", sortOrder)) ctx.Error(422, "", fmt.Errorf("Invalid sort order: \"%s\"", sortOrder))
return 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 // 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 { if err := repo_service.ChangeRepositoryName(ctx.User, repo, newRepoName); err != nil {
switch { switch {
case models.IsErrRepoAlreadyExist(err): 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): 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): 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: default:
ctx.Error(http.StatusUnprocessableEntity, "ChangeRepositoryName", err) ctx.Error(422, "ChangeRepositoryName", err)
} }
return 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 // 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 { if visibilityChanged && setting.Repository.ForcePrivate && !*opts.Private && !ctx.User.IsAdmin {
err := fmt.Errorf("cannot change private repository to public") 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 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 opts.DefaultBranch != nil && repo.DefaultBranch != *opts.DefaultBranch && ctx.Repo.GitRepo.IsBranchExist(*opts.DefaultBranch) {
if err := ctx.Repo.GitRepo.SetDefaultBranch(*opts.DefaultBranch); err != nil { if err := ctx.Repo.GitRepo.SetDefaultBranch(*opts.DefaultBranch); err != nil {
if !git.IsErrUnsupportedVersion(err) { if !git.IsErrUnsupportedVersion(err) {
ctx.Error(http.StatusInternalServerError, "SetDefaultBranch", err) ctx.Error(500, "SetDefaultBranch", err)
return err return err
} }
} }
@ -701,7 +700,7 @@ func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) err
} }
if err := models.UpdateRepository(repo, visibilityChanged); err != nil { if err := models.UpdateRepository(repo, visibilityChanged); err != nil {
ctx.Error(http.StatusInternalServerError, "UpdateRepository", err) ctx.Error(500, "UpdateRepository", err)
return err return err
} }
@ -737,12 +736,12 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
// Check that values are valid // Check that values are valid
if !validation.IsValidExternalURL(opts.ExternalTracker.ExternalTrackerURL) { if !validation.IsValidExternalURL(opts.ExternalTracker.ExternalTrackerURL) {
err := fmt.Errorf("External tracker URL not valid") 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 return err
} }
if len(opts.ExternalTracker.ExternalTrackerFormat) != 0 && !validation.IsValidExternalTrackerURLFormat(opts.ExternalTracker.ExternalTrackerFormat) { if len(opts.ExternalTracker.ExternalTrackerFormat) != 0 && !validation.IsValidExternalTrackerURLFormat(opts.ExternalTracker.ExternalTrackerFormat) {
err := fmt.Errorf("External tracker URL format not valid") 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 return err
} }
@ -797,7 +796,7 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
// Check that values are valid // Check that values are valid
if !validation.IsValidExternalURL(opts.ExternalWiki.ExternalWikiURL) { if !validation.IsValidExternalURL(opts.ExternalWiki.ExternalWikiURL) {
err := fmt.Errorf("External wiki URL not valid") 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 return err
} }
@ -866,7 +865,7 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
} }
if err := models.UpdateRepositoryUnits(repo, units); err != nil { if err := models.UpdateRepositoryUnits(repo, units); err != nil {
ctx.Error(http.StatusInternalServerError, "UpdateRepositoryUnits", err) ctx.Error(500, "UpdateRepositoryUnits", err)
return err return err
} }
@ -881,20 +880,20 @@ func updateRepoArchivedState(ctx *context.APIContext, opts api.EditRepoOption) e
if opts.Archived != nil { if opts.Archived != nil {
if repo.IsMirror { if repo.IsMirror {
err := fmt.Errorf("repo is a mirror, cannot archive/un-archive") 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 return err
} }
if *opts.Archived { if *opts.Archived {
if err := repo.SetArchiveRepoState(*opts.Archived); err != nil { if err := repo.SetArchiveRepoState(*opts.Archived); err != nil {
log.Error("Tried to archive a repo: %s", err) log.Error("Tried to archive a repo: %s", err)
ctx.Error(http.StatusInternalServerError, "ArchiveRepoState", err) ctx.Error(500, "ArchiveRepoState", err)
return err return err
} }
log.Trace("Repository was archived: %s/%s", ctx.Repo.Owner.Name, repo.Name) log.Trace("Repository was archived: %s/%s", ctx.Repo.Owner.Name, repo.Name)
} else { } else {
if err := repo.SetArchiveRepoState(*opts.Archived); err != nil { if err := repo.SetArchiveRepoState(*opts.Archived); err != nil {
log.Error("Tried to un-archive a repo: %s", err) log.Error("Tried to un-archive a repo: %s", err)
ctx.Error(http.StatusInternalServerError, "ArchiveRepoState", err) ctx.Error(500, "ArchiveRepoState", err)
return err return err
} }
log.Trace("Repository was un-archived: %s/%s", ctx.Repo.Owner.Name, repo.Name) log.Trace("Repository was un-archived: %s/%s", ctx.Repo.Owner.Name, repo.Name)

View file

@ -5,7 +5,6 @@
package repo package repo
import ( import (
"net/http"
"testing" "testing"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
@ -55,7 +54,7 @@ func TestRepoEdit(t *testing.T) {
Edit(&context.APIContext{Context: ctx, Org: nil}, opts) 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{ models.AssertExistsAndLoadBean(t, &models.Repository{
ID: 1, ID: 1,
}, models.Cond("name = ? AND is_archived = 1", *opts.Name)) }, 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) 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{ models.AssertExistsAndLoadBean(t, &models.Repository{
ID: 1, ID: 1,

View file

@ -5,7 +5,6 @@
package repo package repo
import ( import (
"net/http"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/convert"
@ -76,17 +75,17 @@ func GetTag(ctx *context.APIContext) {
sha := ctx.Params("sha") sha := ctx.Params("sha")
if len(sha) == 0 { if len(sha) == 0 {
ctx.Error(http.StatusBadRequest, "", "SHA not provided") ctx.Error(400, "", "SHA not provided")
return return
} }
if tag, err := ctx.Repo.GitRepo.GetAnnotatedTag(sha); err != nil { if tag, err := ctx.Repo.GitRepo.GetAnnotatedTag(sha); err != nil {
ctx.Error(http.StatusBadRequest, "GetTag", err) ctx.Error(400, "GetTag", err)
} else { } else {
commit, err := tag.Commit() commit, err := tag.Commit()
if err != nil { 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))
} }
} }

View file

@ -5,7 +5,6 @@
package repo package repo
import ( import (
"net/http"
"strings" "strings"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
@ -42,7 +41,7 @@ func ListTopics(ctx *context.APIContext) {
}) })
if err != nil { if err != nil {
log.Error("ListTopics failed: %v", err) log.Error("ListTopics failed: %v", err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(500, map[string]interface{}{
"message": "ListTopics failed.", "message": "ListTopics failed.",
}) })
return return
@ -52,7 +51,7 @@ func ListTopics(ctx *context.APIContext) {
for i, topic := range topics { for i, topic := range topics {
topicNames[i] = topic.Name topicNames[i] = topic.Name
} }
ctx.JSON(http.StatusOK, map[string]interface{}{ ctx.JSON(200, map[string]interface{}{
"topics": topicNames, "topics": topicNames,
}) })
} }
@ -82,20 +81,20 @@ func UpdateTopics(ctx *context.APIContext, form api.RepoTopicOptions) {
// responses: // responses:
// "204": // "204":
// "$ref": "#/responses/empty" // "$ref": "#/responses/empty"
// "422":
// "$ref": "#/responses/validationError"
topicNames := form.Topics topicNames := form.Topics
validTopics, invalidTopics := models.SanitizeAndValidateTopics(topicNames) validTopics, invalidTopics := models.SanitizeAndValidateTopics(topicNames)
if len(validTopics) > 25 { if len(validTopics) > 25 {
ctx.JSON(http.StatusUnprocessableEntity, map[string]interface{}{ ctx.Error(422, "", "Exceeding maximum number of topics per repo")
"invalidTopics": nil,
"message": "Exceeding maximum number of topics per repo",
})
return return
} }
if len(invalidTopics) > 0 { if len(invalidTopics) > 0 {
ctx.JSON(http.StatusUnprocessableEntity, map[string]interface{}{ ctx.JSON(422, map[string]interface{}{
"invalidTopics": invalidTopics, "invalidTopics": invalidTopics,
"message": "Topic names are invalid", "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...) err := models.SaveTopics(ctx.Repo.Repository.ID, validTopics...)
if err != nil { if err != nil {
log.Error("SaveTopics failed: %v", err) log.Error("SaveTopics failed: %v", err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(500, map[string]interface{}{
"message": "Save topics failed.", "message": "Save topics failed.",
}) })
return return
} }
ctx.Status(http.StatusNoContent) ctx.Status(204)
} }
// AddTopic adds a topic name to a repo // 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"))) topicName := strings.TrimSpace(strings.ToLower(ctx.Params(":topic")))
if !models.ValidateTopic(topicName) { if !models.ValidateTopic(topicName) {
ctx.Error(http.StatusUnprocessableEntity, "", "Topic name is invalid") ctx.Error(422, "", "Topic name is invalid")
return return
} }
@ -154,13 +153,13 @@ func AddTopic(ctx *context.APIContext) {
}) })
if err != nil { if err != nil {
log.Error("AddTopic failed: %v", err) log.Error("AddTopic failed: %v", err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(500, map[string]interface{}{
"message": "ListTopics failed.", "message": "ListTopics failed.",
}) })
return return
} }
if len(topics) >= 25 { if len(topics) >= 25 {
ctx.JSON(http.StatusUnprocessableEntity, map[string]interface{}{ ctx.JSON(422, map[string]interface{}{
"message": "Exceeding maximum allowed topics per repo.", "message": "Exceeding maximum allowed topics per repo.",
}) })
return return
@ -169,13 +168,13 @@ func AddTopic(ctx *context.APIContext) {
_, err = models.AddTopic(ctx.Repo.Repository.ID, topicName) _, err = models.AddTopic(ctx.Repo.Repository.ID, topicName)
if err != nil { if err != nil {
log.Error("AddTopic failed: %v", err) log.Error("AddTopic failed: %v", err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(500, map[string]interface{}{
"message": "AddTopic failed.", "message": "AddTopic failed.",
}) })
return return
} }
ctx.Status(http.StatusNoContent) ctx.Status(204)
} }
// DeleteTopic removes topic name from repo // DeleteTopic removes topic name from repo
@ -207,14 +206,14 @@ func DeleteTopic(ctx *context.APIContext) {
topicName := strings.TrimSpace(strings.ToLower(ctx.Params(":topic"))) topicName := strings.TrimSpace(strings.ToLower(ctx.Params(":topic")))
if !models.ValidateTopic(topicName) { if !models.ValidateTopic(topicName) {
ctx.Error(http.StatusUnprocessableEntity, "", "Topic name is invalid") ctx.Error(422, "", "Topic name is invalid")
return return
} }
topic, err := models.DeleteTopic(ctx.Repo.Repository.ID, topicName) topic, err := models.DeleteTopic(ctx.Repo.Repository.ID, topicName)
if err != nil { if err != nil {
log.Error("DeleteTopic failed: %v", err) log.Error("DeleteTopic failed: %v", err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(500, map[string]interface{}{
"message": "DeleteTopic failed.", "message": "DeleteTopic failed.",
}) })
return return
@ -224,7 +223,7 @@ func DeleteTopic(ctx *context.APIContext) {
ctx.NotFound() ctx.NotFound()
} }
ctx.Status(http.StatusNoContent) ctx.Status(204)
} }
// TopicSearch search for creating topic // TopicSearch search for creating topic
@ -244,7 +243,7 @@ func TopicSearch(ctx *context.Context) {
// "200": // "200":
// "$ref": "#/responses/TopicListResponse" // "$ref": "#/responses/TopicListResponse"
if ctx.User == nil { if ctx.User == nil {
ctx.JSON(http.StatusForbidden, map[string]interface{}{ ctx.JSON(403, map[string]interface{}{
"message": "Only owners could change the topics.", "message": "Only owners could change the topics.",
}) })
return return
@ -258,7 +257,7 @@ func TopicSearch(ctx *context.Context) {
}) })
if err != nil { if err != nil {
log.Error("SearchTopics failed: %v", err) log.Error("SearchTopics failed: %v", err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(500, map[string]interface{}{
"message": "Search topics failed.", "message": "Search topics failed.",
}) })
return return
@ -268,7 +267,7 @@ func TopicSearch(ctx *context.Context) {
for i, topic := range topics { for i, topic := range topics {
topicResponses[i] = convert.ToTopicResponse(topic) topicResponses[i] = convert.ToTopicResponse(topic)
} }
ctx.JSON(http.StatusOK, map[string]interface{}{ ctx.JSON(200, map[string]interface{}{
"topics": topicResponses, "topics": topicResponses,
}) })
} }

View file

@ -5,7 +5,6 @@
package user package user
import ( import (
"net/http"
"strings" "strings"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
@ -143,16 +142,16 @@ func GetUserHeatmapData(ctx *context.APIContext) {
user, err := models.GetUserByName(ctx.Params(":username")) user, err := models.GetUserByName(ctx.Params(":username"))
if err != nil { if err != nil {
if models.IsErrUserNotExist(err) { if models.IsErrUserNotExist(err) {
ctx.Status(http.StatusNotFound) ctx.Status(404)
} else { } else {
ctx.Error(http.StatusInternalServerError, "GetUserByName", err) ctx.Error(500, "GetUserByName", err)
} }
return return
} }
heatmap, err := models.GetUserHeatmapDataByUser(user) heatmap, err := models.GetUserHeatmapDataByUser(user)
if err != nil { if err != nil {
ctx.Error(http.StatusInternalServerError, "GetUserHeatmapDataByUser", err) ctx.Error(500, "GetUserHeatmapDataByUser", err)
return return
} }
ctx.JSON(200, heatmap) ctx.JSON(200, heatmap)

View file

@ -6,7 +6,6 @@ package utils
import ( import (
"encoding/json" "encoding/json"
"net/http"
"strings" "strings"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
@ -74,7 +73,7 @@ func AddOrgHook(ctx *context.APIContext, form *api.CreateHookOption) {
org := ctx.Org.Organization org := ctx.Org.Organization
hook, ok := addHook(ctx, form, org.ID, 0) hook, ok := addHook(ctx, form, org.ID, 0)
if ok { 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 repo := ctx.Repo
hook, ok := addHook(ctx, form, 0, repo.Repository.ID) hook, ok := addHook(ctx, form, 0, repo.Repository.ID)
if ok { if ok {
ctx.JSON(http.StatusCreated, convert.ToHook(repo.RepoLink, hook)) ctx.JSON(201, convert.ToHook(repo.RepoLink, hook))
} }
} }