Skip to content

Commit 16dea6c

Browse files
authored
[refactor] replace int with httpStatusCodes (#15282)
* replace "200" (int) with "http.StatusOK" (const) * ctx.Error & ctx.HTML * ctx.JSON Part1 * ctx.JSON Part2 * ctx.JSON Part3
1 parent e9fba18 commit 16dea6c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+504
-441
lines changed

modules/context/api.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,12 @@ func (ctx *APIContext) CheckForOTP() {
203203
if models.IsErrTwoFactorNotEnrolled(err) {
204204
return // No 2FA enrollment for this user
205205
}
206-
ctx.Context.Error(500)
206+
ctx.Context.Error(http.StatusInternalServerError)
207207
return
208208
}
209209
ok, err := twofa.ValidateTOTP(otpHeader)
210210
if err != nil {
211-
ctx.Context.Error(500)
211+
ctx.Context.Error(http.StatusInternalServerError)
212212
return
213213
}
214214
if !ok {
@@ -288,7 +288,7 @@ func ReferencesGitRepo(allowEmpty bool) func(http.Handler) http.Handler {
288288
repoPath := models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
289289
gitRepo, err := git.OpenRepository(repoPath)
290290
if err != nil {
291-
ctx.Error(500, "RepoRef Invalid repo "+repoPath, err)
291+
ctx.Error(http.StatusInternalServerError, "RepoRef Invalid repo "+repoPath, err)
292292
return
293293
}
294294
ctx.Repo.GitRepo = gitRepo
@@ -324,7 +324,7 @@ func (ctx *APIContext) NotFound(objs ...interface{}) {
324324
}
325325
}
326326

327-
ctx.JSON(404, map[string]interface{}{
327+
ctx.JSON(http.StatusNotFound, map[string]interface{}{
328328
"message": message,
329329
"documentation_url": setting.API.SwaggerURL,
330330
"errors": errors,

modules/context/auth.go

+13-11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
package context
77

88
import (
9+
"net/http"
10+
911
"code.gitea.io/gitea/models"
1012
"code.gitea.io/gitea/modules/log"
1113
"code.gitea.io/gitea/modules/setting"
@@ -27,13 +29,13 @@ func Toggle(options *ToggleOptions) func(ctx *Context) {
2729
if ctx.IsSigned {
2830
if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm {
2931
ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
30-
ctx.HTML(200, "user/auth/activate")
32+
ctx.HTML(http.StatusOK, "user/auth/activate")
3133
return
3234
}
3335
if !ctx.User.IsActive || ctx.User.ProhibitLogin {
3436
log.Info("Failed authentication attempt for %s from %s", ctx.User.Name, ctx.RemoteAddr())
3537
ctx.Data["Title"] = ctx.Tr("auth.prohibit_login")
36-
ctx.HTML(200, "user/auth/prohibit_login")
38+
ctx.HTML(http.StatusOK, "user/auth/prohibit_login")
3739
return
3840
}
3941

@@ -76,7 +78,7 @@ func Toggle(options *ToggleOptions) func(ctx *Context) {
7678
return
7779
} else if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm {
7880
ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
79-
ctx.HTML(200, "user/auth/activate")
81+
ctx.HTML(http.StatusOK, "user/auth/activate")
8082
return
8183
}
8284
}
@@ -93,7 +95,7 @@ func Toggle(options *ToggleOptions) func(ctx *Context) {
9395

9496
if options.AdminRequired {
9597
if !ctx.User.IsAdmin {
96-
ctx.Error(403)
98+
ctx.Error(http.StatusForbidden)
9799
return
98100
}
99101
ctx.Data["PageIsAdmin"] = true
@@ -108,22 +110,22 @@ func ToggleAPI(options *ToggleOptions) func(ctx *APIContext) {
108110
if ctx.IsSigned {
109111
if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm {
110112
ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
111-
ctx.JSON(403, map[string]string{
113+
ctx.JSON(http.StatusForbidden, map[string]string{
112114
"message": "This account is not activated.",
113115
})
114116
return
115117
}
116118
if !ctx.User.IsActive || ctx.User.ProhibitLogin {
117119
log.Info("Failed authentication attempt for %s from %s", ctx.User.Name, ctx.RemoteAddr())
118120
ctx.Data["Title"] = ctx.Tr("auth.prohibit_login")
119-
ctx.JSON(403, map[string]string{
121+
ctx.JSON(http.StatusForbidden, map[string]string{
120122
"message": "This account is prohibited from signing in, please contact your site administrator.",
121123
})
122124
return
123125
}
124126

125127
if ctx.User.MustChangePassword {
126-
ctx.JSON(403, map[string]string{
128+
ctx.JSON(http.StatusForbidden, map[string]string{
127129
"message": "You must change your password. Change it at: " + setting.AppURL + "/user/change_password",
128130
})
129131
return
@@ -139,13 +141,13 @@ func ToggleAPI(options *ToggleOptions) func(ctx *APIContext) {
139141
if options.SignInRequired {
140142
if !ctx.IsSigned {
141143
// Restrict API calls with error message.
142-
ctx.JSON(403, map[string]string{
144+
ctx.JSON(http.StatusForbidden, map[string]string{
143145
"message": "Only signed in user is allowed to call APIs.",
144146
})
145147
return
146148
} else if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm {
147149
ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
148-
ctx.HTML(200, "user/auth/activate")
150+
ctx.HTML(http.StatusOK, "user/auth/activate")
149151
return
150152
}
151153
if ctx.IsSigned && ctx.IsBasicAuth {
@@ -164,7 +166,7 @@ func ToggleAPI(options *ToggleOptions) func(ctx *APIContext) {
164166
return
165167
}
166168
if !ok {
167-
ctx.JSON(403, map[string]string{
169+
ctx.JSON(http.StatusForbidden, map[string]string{
168170
"message": "Only signed in user is allowed to call APIs.",
169171
})
170172
return
@@ -174,7 +176,7 @@ func ToggleAPI(options *ToggleOptions) func(ctx *APIContext) {
174176

175177
if options.AdminRequired {
176178
if !ctx.User.IsAdmin {
177-
ctx.JSON(403, map[string]string{
179+
ctx.JSON(http.StatusForbidden, map[string]string{
178180
"message": "You have no permission to request for this.",
179181
})
180182
return

modules/context/context.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ func (ctx *Context) RenderWithErr(msg string, tpl base.TplName, form interface{}
213213
}
214214
ctx.Flash.ErrorMsg = msg
215215
ctx.Data["Flash"] = ctx.Flash
216-
ctx.HTML(200, tpl)
216+
ctx.HTML(http.StatusOK, tpl)
217217
}
218218

219219
// NotFound displays a 404 (Not Found) page and prints the given error, if any.

modules/lfs/locks.go

+24-23
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package lfs
66

77
import (
8+
"net/http"
89
"strconv"
910
"strings"
1011

@@ -21,19 +22,19 @@ import (
2122
func checkIsValidRequest(ctx *context.Context) bool {
2223
if !setting.LFS.StartServer {
2324
log.Debug("Attempt to access LFS server but LFS server is disabled")
24-
writeStatus(ctx, 404)
25+
writeStatus(ctx, http.StatusNotFound)
2526
return false
2627
}
2728
if !MetaMatcher(ctx.Req) {
2829
log.Info("Attempt access LOCKs without accepting the correct media type: %s", metaMediaType)
29-
writeStatus(ctx, 400)
30+
writeStatus(ctx, http.StatusBadRequest)
3031
return false
3132
}
3233
if !ctx.IsSigned {
3334
user, _, _, err := parseToken(ctx.Req.Header.Get("Authorization"))
3435
if err != nil {
3536
ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs")
36-
writeStatus(ctx, 401)
37+
writeStatus(ctx, http.StatusUnauthorized)
3738
return false
3839
}
3940
ctx.User = user
@@ -44,23 +45,23 @@ func checkIsValidRequest(ctx *context.Context) bool {
4445
func handleLockListOut(ctx *context.Context, repo *models.Repository, lock *models.LFSLock, err error) {
4546
if err != nil {
4647
if models.IsErrLFSLockNotExist(err) {
47-
ctx.JSON(200, api.LFSLockList{
48+
ctx.JSON(http.StatusOK, api.LFSLockList{
4849
Locks: []*api.LFSLock{},
4950
})
5051
return
5152
}
52-
ctx.JSON(500, api.LFSLockError{
53+
ctx.JSON(http.StatusInternalServerError, api.LFSLockError{
5354
Message: "unable to list locks : Internal Server Error",
5455
})
5556
return
5657
}
5758
if repo.ID != lock.RepoID {
58-
ctx.JSON(200, api.LFSLockList{
59+
ctx.JSON(http.StatusOK, api.LFSLockList{
5960
Locks: []*api.LFSLock{},
6061
})
6162
return
6263
}
63-
ctx.JSON(200, api.LFSLockList{
64+
ctx.JSON(http.StatusOK, api.LFSLockList{
6465
Locks: []*api.LFSLock{convert.ToLFSLock(lock)},
6566
})
6667
}
@@ -86,7 +87,7 @@ func GetListLockHandler(ctx *context.Context) {
8687
authenticated := authenticate(ctx, repository, rv.Authorization, false)
8788
if !authenticated {
8889
ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs")
89-
ctx.JSON(401, api.LFSLockError{
90+
ctx.JSON(http.StatusUnauthorized, api.LFSLockError{
9091
Message: "You must have pull access to list locks",
9192
})
9293
return
@@ -106,7 +107,7 @@ func GetListLockHandler(ctx *context.Context) {
106107
if id != "" { //Case where we request a specific id
107108
v, err := strconv.ParseInt(id, 10, 64)
108109
if err != nil {
109-
ctx.JSON(400, api.LFSLockError{
110+
ctx.JSON(http.StatusBadRequest, api.LFSLockError{
110111
Message: "bad request : " + err.Error(),
111112
})
112113
return
@@ -133,7 +134,7 @@ func GetListLockHandler(ctx *context.Context) {
133134
lockList, err := models.GetLFSLockByRepoID(repository.ID, cursor, limit)
134135
if err != nil {
135136
log.Error("Unable to list locks for repository ID[%d]: Error: %v", repository.ID, err)
136-
ctx.JSON(500, api.LFSLockError{
137+
ctx.JSON(http.StatusInternalServerError, api.LFSLockError{
137138
Message: "unable to list locks : Internal Server Error",
138139
})
139140
return
@@ -146,7 +147,7 @@ func GetListLockHandler(ctx *context.Context) {
146147
if limit > 0 && len(lockList) == limit {
147148
next = strconv.Itoa(cursor + 1)
148149
}
149-
ctx.JSON(200, api.LFSLockList{
150+
ctx.JSON(http.StatusOK, api.LFSLockList{
150151
Locks: lockListAPI,
151152
Next: next,
152153
})
@@ -175,7 +176,7 @@ func PostLockHandler(ctx *context.Context) {
175176
authenticated := authenticate(ctx, repository, authorization, true)
176177
if !authenticated {
177178
ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs")
178-
ctx.JSON(401, api.LFSLockError{
179+
ctx.JSON(http.StatusUnauthorized, api.LFSLockError{
179180
Message: "You must have push access to create locks",
180181
})
181182
return
@@ -199,26 +200,26 @@ func PostLockHandler(ctx *context.Context) {
199200
})
200201
if err != nil {
201202
if models.IsErrLFSLockAlreadyExist(err) {
202-
ctx.JSON(409, api.LFSLockError{
203+
ctx.JSON(http.StatusConflict, api.LFSLockError{
203204
Lock: convert.ToLFSLock(lock),
204205
Message: "already created lock",
205206
})
206207
return
207208
}
208209
if models.IsErrLFSUnauthorizedAction(err) {
209210
ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs")
210-
ctx.JSON(401, api.LFSLockError{
211+
ctx.JSON(http.StatusUnauthorized, api.LFSLockError{
211212
Message: "You must have push access to create locks : " + err.Error(),
212213
})
213214
return
214215
}
215216
log.Error("Unable to CreateLFSLock in repository %-v at %s for user %-v: Error: %v", repository, req.Path, ctx.User, err)
216-
ctx.JSON(500, api.LFSLockError{
217+
ctx.JSON(http.StatusInternalServerError, api.LFSLockError{
217218
Message: "internal server error : Internal Server Error",
218219
})
219220
return
220221
}
221-
ctx.JSON(201, api.LFSLockResponse{Lock: convert.ToLFSLock(lock)})
222+
ctx.JSON(http.StatusCreated, api.LFSLockResponse{Lock: convert.ToLFSLock(lock)})
222223
}
223224

224225
// VerifyLockHandler list locks for verification
@@ -244,7 +245,7 @@ func VerifyLockHandler(ctx *context.Context) {
244245
authenticated := authenticate(ctx, repository, authorization, true)
245246
if !authenticated {
246247
ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs")
247-
ctx.JSON(401, api.LFSLockError{
248+
ctx.JSON(http.StatusUnauthorized, api.LFSLockError{
248249
Message: "You must have push access to verify locks",
249250
})
250251
return
@@ -263,7 +264,7 @@ func VerifyLockHandler(ctx *context.Context) {
263264
lockList, err := models.GetLFSLockByRepoID(repository.ID, cursor, limit)
264265
if err != nil {
265266
log.Error("Unable to list locks for repository ID[%d]: Error: %v", repository.ID, err)
266-
ctx.JSON(500, api.LFSLockError{
267+
ctx.JSON(http.StatusInternalServerError, api.LFSLockError{
267268
Message: "unable to list locks : Internal Server Error",
268269
})
269270
return
@@ -281,7 +282,7 @@ func VerifyLockHandler(ctx *context.Context) {
281282
lockTheirsListAPI = append(lockTheirsListAPI, convert.ToLFSLock(l))
282283
}
283284
}
284-
ctx.JSON(200, api.LFSLockListVerify{
285+
ctx.JSON(http.StatusOK, api.LFSLockListVerify{
285286
Ours: lockOursListAPI,
286287
Theirs: lockTheirsListAPI,
287288
Next: next,
@@ -311,7 +312,7 @@ func UnLockHandler(ctx *context.Context) {
311312
authenticated := authenticate(ctx, repository, authorization, true)
312313
if !authenticated {
313314
ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs")
314-
ctx.JSON(401, api.LFSLockError{
315+
ctx.JSON(http.StatusUnauthorized, api.LFSLockError{
315316
Message: "You must have push access to delete locks",
316317
})
317318
return
@@ -332,16 +333,16 @@ func UnLockHandler(ctx *context.Context) {
332333
if err != nil {
333334
if models.IsErrLFSUnauthorizedAction(err) {
334335
ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs")
335-
ctx.JSON(401, api.LFSLockError{
336+
ctx.JSON(http.StatusUnauthorized, api.LFSLockError{
336337
Message: "You must have push access to delete locks : " + err.Error(),
337338
})
338339
return
339340
}
340341
log.Error("Unable to DeleteLFSLockByID[%d] by user %-v with force %t: Error: %v", ctx.ParamsInt64("lid"), ctx.User, req.Force, err)
341-
ctx.JSON(500, api.LFSLockError{
342+
ctx.JSON(http.StatusInternalServerError, api.LFSLockError{
342343
Message: "unable to delete lock : Internal Server Error",
343344
})
344345
return
345346
}
346-
ctx.JSON(200, api.LFSLockResponse{Lock: convert.ToLFSLock(lock)})
347+
ctx.JSON(http.StatusOK, api.LFSLockResponse{Lock: convert.ToLFSLock(lock)})
347348
}

routers/admin/admin.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package admin
77

88
import (
99
"fmt"
10+
"net/http"
1011
"net/url"
1112
"os"
1213
"runtime"
@@ -128,7 +129,7 @@ func Dashboard(ctx *context.Context) {
128129
updateSystemStatus()
129130
ctx.Data["SysStatus"] = sysStatus
130131
ctx.Data["SSH"] = setting.SSH
131-
ctx.HTML(200, tplDashboard)
132+
ctx.HTML(http.StatusOK, tplDashboard)
132133
}
133134

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

318-
ctx.HTML(200, tplConfig)
319+
ctx.HTML(http.StatusOK, tplConfig)
319320
}
320321

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

332333
// MonitorCancel cancels a process
333334
func MonitorCancel(ctx *context.Context) {
334335
pid := ctx.ParamsInt64("pid")
335336
process.GetManager().Cancel(pid)
336-
ctx.JSON(200, map[string]interface{}{
337+
ctx.JSON(http.StatusOK, map[string]interface{}{
337338
"redirect": setting.AppSubURL + "/admin/monitor",
338339
})
339340
}
@@ -350,7 +351,7 @@ func Queue(ctx *context.Context) {
350351
ctx.Data["PageIsAdmin"] = true
351352
ctx.Data["PageIsAdminMonitor"] = true
352353
ctx.Data["Queue"] = mq
353-
ctx.HTML(200, tplQueue)
354+
ctx.HTML(http.StatusOK, tplQueue)
354355
}
355356

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

0 commit comments

Comments
 (0)