package misc
import (
- "net/http"
"strings"
"code.gitea.io/gitea/modules/context"
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
}
}
}
_, err = ctx.Write(markdown.RenderRaw(body, "", false))
if err != nil {
- ctx.Error(http.StatusInternalServerError, "", err)
+ ctx.Error(500, "", err)
return
}
}
import (
"io/ioutil"
- "net/http"
"net/http/httptest"
"net/url"
"strings"
import (
"fmt"
- "net/http"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/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))
}
}
package repo
import (
- "net/http"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/repofiles"
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)
}
}
import (
"encoding/base64"
- "net/http"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
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)
}
}
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
if git.IsErrNotExist(err) {
ctx.NotFound(err)
} else {
- ctx.Error(http.StatusInternalServerError, "GetEditorconfig", err)
+ ctx.Error(500, "GetEditorconfig", err)
}
return
}
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.
}
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)
}
}
}
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)
}
}
// "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,
})
}
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)
}
}
// "$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,
})
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)
}
}
package repo
import (
- "net/http"
"testing"
"code.gitea.io/gitea/models"
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,
import (
"fmt"
- "net/http"
"strings"
"time"
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)
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)
import (
"fmt"
- "net/http"
"strings"
"time"
err = pr.LoadIssue()
if err != nil {
- ctx.Error(http.StatusInternalServerError, "LoadIssue", err)
+ ctx.Error(500, "LoadIssue", err)
return
}
issue := pr.Issue
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)
err = pr.LoadIssue()
if err != nil {
- ctx.Error(http.StatusInternalServerError, "LoadIssue", err)
+ ctx.Error(500, "LoadIssue", err)
return
}
pr.Issue.Repo = ctx.Repo.Repository
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)
"bytes"
"errors"
"fmt"
- "net/http"
"net/url"
"strings"
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
}
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
}
}
}
}
- 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
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
}
// 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
}
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
}
}
}
if err := models.UpdateRepository(repo, visibilityChanged); err != nil {
- ctx.Error(http.StatusInternalServerError, "UpdateRepository", err)
+ ctx.Error(500, "UpdateRepository", err)
return err
}
// 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
}
// 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
}
}
if err := models.UpdateRepositoryUnits(repo, units); err != nil {
- ctx.Error(http.StatusInternalServerError, "UpdateRepositoryUnits", err)
+ ctx.Error(500, "UpdateRepositoryUnits", err)
return err
}
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)
package repo
import (
- "net/http"
"testing"
"code.gitea.io/gitea/models"
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))
}
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,
package repo
import (
- "net/http"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/convert"
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))
}
}
package repo
import (
- "net/http"
"strings"
"code.gitea.io/gitea/models"
})
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
for i, topic := range topics {
topicNames[i] = topic.Name
}
- ctx.JSON(http.StatusOK, map[string]interface{}{
+ ctx.JSON(200, map[string]interface{}{
"topics": topicNames,
})
}
// 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",
})
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
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
}
})
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
_, 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
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
ctx.NotFound()
}
- ctx.Status(http.StatusNoContent)
+ ctx.Status(204)
}
// TopicSearch search for creating topic
// "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
})
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
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,
})
}
package user
import (
- "net/http"
"strings"
"code.gitea.io/gitea/models"
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)
import (
"encoding/json"
- "net/http"
"strings"
"code.gitea.io/gitea/models"
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))
}
}
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))
}
}