Skip to content

[refactor] replace int with httpStatusCodes #15282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions modules/context/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,12 @@ func (ctx *APIContext) CheckForOTP() {
if models.IsErrTwoFactorNotEnrolled(err) {
return // No 2FA enrollment for this user
}
ctx.Context.Error(500)
ctx.Context.Error(http.StatusInternalServerError)
return
}
ok, err := twofa.ValidateTOTP(otpHeader)
if err != nil {
ctx.Context.Error(500)
ctx.Context.Error(http.StatusInternalServerError)
return
}
if !ok {
Expand Down Expand Up @@ -288,7 +288,7 @@ func ReferencesGitRepo(allowEmpty bool) func(http.Handler) http.Handler {
repoPath := models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
gitRepo, err := git.OpenRepository(repoPath)
if err != nil {
ctx.Error(500, "RepoRef Invalid repo "+repoPath, err)
ctx.Error(http.StatusInternalServerError, "RepoRef Invalid repo "+repoPath, err)
return
}
ctx.Repo.GitRepo = gitRepo
Expand Down Expand Up @@ -324,7 +324,7 @@ func (ctx *APIContext) NotFound(objs ...interface{}) {
}
}

ctx.JSON(404, map[string]interface{}{
ctx.JSON(http.StatusNotFound, map[string]interface{}{
"message": message,
"documentation_url": setting.API.SwaggerURL,
"errors": errors,
Expand Down
24 changes: 13 additions & 11 deletions modules/context/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
package context

import (
"net/http"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
Expand All @@ -27,13 +29,13 @@ func Toggle(options *ToggleOptions) func(ctx *Context) {
if ctx.IsSigned {
if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm {
ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
ctx.HTML(200, "user/auth/activate")
ctx.HTML(http.StatusOK, "user/auth/activate")
return
}
if !ctx.User.IsActive || ctx.User.ProhibitLogin {
log.Info("Failed authentication attempt for %s from %s", ctx.User.Name, ctx.RemoteAddr())
ctx.Data["Title"] = ctx.Tr("auth.prohibit_login")
ctx.HTML(200, "user/auth/prohibit_login")
ctx.HTML(http.StatusOK, "user/auth/prohibit_login")
return
}

Expand Down Expand Up @@ -76,7 +78,7 @@ func Toggle(options *ToggleOptions) func(ctx *Context) {
return
} else if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm {
ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
ctx.HTML(200, "user/auth/activate")
ctx.HTML(http.StatusOK, "user/auth/activate")
return
}
}
Expand All @@ -93,7 +95,7 @@ func Toggle(options *ToggleOptions) func(ctx *Context) {

if options.AdminRequired {
if !ctx.User.IsAdmin {
ctx.Error(403)
ctx.Error(http.StatusForbidden)
return
}
ctx.Data["PageIsAdmin"] = true
Expand All @@ -108,22 +110,22 @@ func ToggleAPI(options *ToggleOptions) func(ctx *APIContext) {
if ctx.IsSigned {
if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm {
ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
ctx.JSON(403, map[string]string{
ctx.JSON(http.StatusForbidden, map[string]string{
"message": "This account is not activated.",
})
return
}
if !ctx.User.IsActive || ctx.User.ProhibitLogin {
log.Info("Failed authentication attempt for %s from %s", ctx.User.Name, ctx.RemoteAddr())
ctx.Data["Title"] = ctx.Tr("auth.prohibit_login")
ctx.JSON(403, map[string]string{
ctx.JSON(http.StatusForbidden, map[string]string{
"message": "This account is prohibited from signing in, please contact your site administrator.",
})
return
}

if ctx.User.MustChangePassword {
ctx.JSON(403, map[string]string{
ctx.JSON(http.StatusForbidden, map[string]string{
"message": "You must change your password. Change it at: " + setting.AppURL + "/user/change_password",
})
return
Expand All @@ -139,13 +141,13 @@ func ToggleAPI(options *ToggleOptions) func(ctx *APIContext) {
if options.SignInRequired {
if !ctx.IsSigned {
// Restrict API calls with error message.
ctx.JSON(403, map[string]string{
ctx.JSON(http.StatusForbidden, map[string]string{
"message": "Only signed in user is allowed to call APIs.",
})
return
} else if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm {
ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
ctx.HTML(200, "user/auth/activate")
ctx.HTML(http.StatusOK, "user/auth/activate")
return
}
if ctx.IsSigned && ctx.IsBasicAuth {
Expand All @@ -164,7 +166,7 @@ func ToggleAPI(options *ToggleOptions) func(ctx *APIContext) {
return
}
if !ok {
ctx.JSON(403, map[string]string{
ctx.JSON(http.StatusForbidden, map[string]string{
"message": "Only signed in user is allowed to call APIs.",
})
return
Expand All @@ -174,7 +176,7 @@ func ToggleAPI(options *ToggleOptions) func(ctx *APIContext) {

if options.AdminRequired {
if !ctx.User.IsAdmin {
ctx.JSON(403, map[string]string{
ctx.JSON(http.StatusForbidden, map[string]string{
"message": "You have no permission to request for this.",
})
return
Expand Down
2 changes: 1 addition & 1 deletion modules/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (ctx *Context) RenderWithErr(msg string, tpl base.TplName, form interface{}
}
ctx.Flash.ErrorMsg = msg
ctx.Data["Flash"] = ctx.Flash
ctx.HTML(200, tpl)
ctx.HTML(http.StatusOK, tpl)
}

// NotFound displays a 404 (Not Found) page and prints the given error, if any.
Expand Down
47 changes: 24 additions & 23 deletions modules/lfs/locks.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package lfs

import (
"net/http"
"strconv"
"strings"

Expand All @@ -21,19 +22,19 @@ import (
func checkIsValidRequest(ctx *context.Context) bool {
if !setting.LFS.StartServer {
log.Debug("Attempt to access LFS server but LFS server is disabled")
writeStatus(ctx, 404)
writeStatus(ctx, http.StatusNotFound)
return false
}
if !MetaMatcher(ctx.Req) {
log.Info("Attempt access LOCKs without accepting the correct media type: %s", metaMediaType)
writeStatus(ctx, 400)
writeStatus(ctx, http.StatusBadRequest)
return false
}
if !ctx.IsSigned {
user, _, _, err := parseToken(ctx.Req.Header.Get("Authorization"))
if err != nil {
ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs")
writeStatus(ctx, 401)
writeStatus(ctx, http.StatusUnauthorized)
return false
}
ctx.User = user
Expand All @@ -44,23 +45,23 @@ func checkIsValidRequest(ctx *context.Context) bool {
func handleLockListOut(ctx *context.Context, repo *models.Repository, lock *models.LFSLock, err error) {
if err != nil {
if models.IsErrLFSLockNotExist(err) {
ctx.JSON(200, api.LFSLockList{
ctx.JSON(http.StatusOK, api.LFSLockList{
Locks: []*api.LFSLock{},
})
return
}
ctx.JSON(500, api.LFSLockError{
ctx.JSON(http.StatusInternalServerError, api.LFSLockError{
Message: "unable to list locks : Internal Server Error",
})
return
}
if repo.ID != lock.RepoID {
ctx.JSON(200, api.LFSLockList{
ctx.JSON(http.StatusOK, api.LFSLockList{
Locks: []*api.LFSLock{},
})
return
}
ctx.JSON(200, api.LFSLockList{
ctx.JSON(http.StatusOK, api.LFSLockList{
Locks: []*api.LFSLock{convert.ToLFSLock(lock)},
})
}
Expand All @@ -86,7 +87,7 @@ func GetListLockHandler(ctx *context.Context) {
authenticated := authenticate(ctx, repository, rv.Authorization, false)
if !authenticated {
ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs")
ctx.JSON(401, api.LFSLockError{
ctx.JSON(http.StatusUnauthorized, api.LFSLockError{
Message: "You must have pull access to list locks",
})
return
Expand All @@ -106,7 +107,7 @@ func GetListLockHandler(ctx *context.Context) {
if id != "" { //Case where we request a specific id
v, err := strconv.ParseInt(id, 10, 64)
if err != nil {
ctx.JSON(400, api.LFSLockError{
ctx.JSON(http.StatusBadRequest, api.LFSLockError{
Message: "bad request : " + err.Error(),
})
return
Expand All @@ -133,7 +134,7 @@ func GetListLockHandler(ctx *context.Context) {
lockList, err := models.GetLFSLockByRepoID(repository.ID, cursor, limit)
if err != nil {
log.Error("Unable to list locks for repository ID[%d]: Error: %v", repository.ID, err)
ctx.JSON(500, api.LFSLockError{
ctx.JSON(http.StatusInternalServerError, api.LFSLockError{
Message: "unable to list locks : Internal Server Error",
})
return
Expand All @@ -146,7 +147,7 @@ func GetListLockHandler(ctx *context.Context) {
if limit > 0 && len(lockList) == limit {
next = strconv.Itoa(cursor + 1)
}
ctx.JSON(200, api.LFSLockList{
ctx.JSON(http.StatusOK, api.LFSLockList{
Locks: lockListAPI,
Next: next,
})
Expand Down Expand Up @@ -175,7 +176,7 @@ func PostLockHandler(ctx *context.Context) {
authenticated := authenticate(ctx, repository, authorization, true)
if !authenticated {
ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs")
ctx.JSON(401, api.LFSLockError{
ctx.JSON(http.StatusUnauthorized, api.LFSLockError{
Message: "You must have push access to create locks",
})
return
Expand All @@ -199,26 +200,26 @@ func PostLockHandler(ctx *context.Context) {
})
if err != nil {
if models.IsErrLFSLockAlreadyExist(err) {
ctx.JSON(409, api.LFSLockError{
ctx.JSON(http.StatusConflict, api.LFSLockError{
Lock: convert.ToLFSLock(lock),
Message: "already created lock",
})
return
}
if models.IsErrLFSUnauthorizedAction(err) {
ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs")
ctx.JSON(401, api.LFSLockError{
ctx.JSON(http.StatusUnauthorized, api.LFSLockError{
Message: "You must have push access to create locks : " + err.Error(),
})
return
}
log.Error("Unable to CreateLFSLock in repository %-v at %s for user %-v: Error: %v", repository, req.Path, ctx.User, err)
ctx.JSON(500, api.LFSLockError{
ctx.JSON(http.StatusInternalServerError, api.LFSLockError{
Message: "internal server error : Internal Server Error",
})
return
}
ctx.JSON(201, api.LFSLockResponse{Lock: convert.ToLFSLock(lock)})
ctx.JSON(http.StatusCreated, api.LFSLockResponse{Lock: convert.ToLFSLock(lock)})
}

// VerifyLockHandler list locks for verification
Expand All @@ -244,7 +245,7 @@ func VerifyLockHandler(ctx *context.Context) {
authenticated := authenticate(ctx, repository, authorization, true)
if !authenticated {
ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs")
ctx.JSON(401, api.LFSLockError{
ctx.JSON(http.StatusUnauthorized, api.LFSLockError{
Message: "You must have push access to verify locks",
})
return
Expand All @@ -263,7 +264,7 @@ func VerifyLockHandler(ctx *context.Context) {
lockList, err := models.GetLFSLockByRepoID(repository.ID, cursor, limit)
if err != nil {
log.Error("Unable to list locks for repository ID[%d]: Error: %v", repository.ID, err)
ctx.JSON(500, api.LFSLockError{
ctx.JSON(http.StatusInternalServerError, api.LFSLockError{
Message: "unable to list locks : Internal Server Error",
})
return
Expand All @@ -281,7 +282,7 @@ func VerifyLockHandler(ctx *context.Context) {
lockTheirsListAPI = append(lockTheirsListAPI, convert.ToLFSLock(l))
}
}
ctx.JSON(200, api.LFSLockListVerify{
ctx.JSON(http.StatusOK, api.LFSLockListVerify{
Ours: lockOursListAPI,
Theirs: lockTheirsListAPI,
Next: next,
Expand Down Expand Up @@ -311,7 +312,7 @@ func UnLockHandler(ctx *context.Context) {
authenticated := authenticate(ctx, repository, authorization, true)
if !authenticated {
ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs")
ctx.JSON(401, api.LFSLockError{
ctx.JSON(http.StatusUnauthorized, api.LFSLockError{
Message: "You must have push access to delete locks",
})
return
Expand All @@ -332,16 +333,16 @@ func UnLockHandler(ctx *context.Context) {
if err != nil {
if models.IsErrLFSUnauthorizedAction(err) {
ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs")
ctx.JSON(401, api.LFSLockError{
ctx.JSON(http.StatusUnauthorized, api.LFSLockError{
Message: "You must have push access to delete locks : " + err.Error(),
})
return
}
log.Error("Unable to DeleteLFSLockByID[%d] by user %-v with force %t: Error: %v", ctx.ParamsInt64("lid"), ctx.User, req.Force, err)
ctx.JSON(500, api.LFSLockError{
ctx.JSON(http.StatusInternalServerError, api.LFSLockError{
Message: "unable to delete lock : Internal Server Error",
})
return
}
ctx.JSON(200, api.LFSLockResponse{Lock: convert.ToLFSLock(lock)})
ctx.JSON(http.StatusOK, api.LFSLockResponse{Lock: convert.ToLFSLock(lock)})
}
13 changes: 7 additions & 6 deletions routers/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package admin

import (
"fmt"
"net/http"
"net/url"
"os"
"runtime"
Expand Down Expand Up @@ -128,7 +129,7 @@ func Dashboard(ctx *context.Context) {
updateSystemStatus()
ctx.Data["SysStatus"] = sysStatus
ctx.Data["SSH"] = setting.SSH
ctx.HTML(200, tplDashboard)
ctx.HTML(http.StatusOK, tplDashboard)
}

// DashboardPost run an admin operation
Expand Down Expand Up @@ -315,7 +316,7 @@ func Config(ctx *context.Context) {
ctx.Data["EnableXORMLog"] = setting.EnableXORMLog
ctx.Data["LogSQL"] = setting.Database.LogSQL

ctx.HTML(200, tplConfig)
ctx.HTML(http.StatusOK, tplConfig)
}

// Monitor show admin monitor page
Expand All @@ -326,14 +327,14 @@ func Monitor(ctx *context.Context) {
ctx.Data["Processes"] = process.GetManager().Processes()
ctx.Data["Entries"] = cron.ListTasks()
ctx.Data["Queues"] = queue.GetManager().ManagedQueues()
ctx.HTML(200, tplMonitor)
ctx.HTML(http.StatusOK, tplMonitor)
}

// MonitorCancel cancels a process
func MonitorCancel(ctx *context.Context) {
pid := ctx.ParamsInt64("pid")
process.GetManager().Cancel(pid)
ctx.JSON(200, map[string]interface{}{
ctx.JSON(http.StatusOK, map[string]interface{}{
"redirect": setting.AppSubURL + "/admin/monitor",
})
}
Expand All @@ -350,7 +351,7 @@ func Queue(ctx *context.Context) {
ctx.Data["PageIsAdmin"] = true
ctx.Data["PageIsAdminMonitor"] = true
ctx.Data["Queue"] = mq
ctx.HTML(200, tplQueue)
ctx.HTML(http.StatusOK, tplQueue)
}

// WorkerCancel cancels a worker group
Expand All @@ -364,7 +365,7 @@ func WorkerCancel(ctx *context.Context) {
pid := ctx.ParamsInt64("pid")
mq.CancelWorkers(pid)
ctx.Flash.Info(ctx.Tr("admin.monitor.queue.pool.cancelling"))
ctx.JSON(200, map[string]interface{}{
ctx.JSON(http.StatusOK, map[string]interface{}{
"redirect": setting.AppSubURL + "/admin/monitor/queue/" + strconv.FormatInt(qid, 10),
})
}
Expand Down
Loading