Skip to content

Commit d4be7f0

Browse files
committed
fix
1 parent adefa63 commit d4be7f0

File tree

8 files changed

+30
-25
lines changed

8 files changed

+30
-25
lines changed

modules/setting/setting.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,9 @@ func checkOverlappedPath(name, path string) {
235235
}
236236
configuredPaths[path] = name
237237
}
238+
239+
func PanicInDevOrTesting(msg string, a ...any) {
240+
if !IsProd || IsInTesting {
241+
panic(fmt.Sprintf(msg, a...))
242+
}
243+
}

modules/templates/helper.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,5 @@ func QueryBuild(a ...any) template.URL {
331331
}
332332

333333
func panicIfDevOrTesting() {
334-
if !setting.IsProd || setting.IsInTesting {
335-
panic("legacy template functions are for backward compatibility only, do not use them in new code")
336-
}
334+
setting.PanicInDevOrTesting("legacy template functions are for backward compatibility only, do not use them in new code")
337335
}

routers/api/v1/org/member.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func IsMember(ctx *context.APIContext) {
143143
// "404":
144144
// description: user is not a member
145145

146-
userToCheck := user.GetUserByParams(ctx)
146+
userToCheck := user.GetContextUserByPathParam(ctx)
147147
if ctx.Written() {
148148
return
149149
}
@@ -194,7 +194,7 @@ func IsPublicMember(ctx *context.APIContext) {
194194
// "404":
195195
// description: user is not a public member
196196

197-
userToCheck := user.GetUserByParams(ctx)
197+
userToCheck := user.GetContextUserByPathParam(ctx)
198198
if ctx.Written() {
199199
return
200200
}
@@ -236,7 +236,7 @@ func PublicizeMember(ctx *context.APIContext) {
236236
// "404":
237237
// "$ref": "#/responses/notFound"
238238

239-
userToPublicize := user.GetUserByParams(ctx)
239+
userToPublicize := user.GetContextUserByPathParam(ctx)
240240
if ctx.Written() {
241241
return
242242
}
@@ -278,7 +278,7 @@ func ConcealMember(ctx *context.APIContext) {
278278
// "404":
279279
// "$ref": "#/responses/notFound"
280280

281-
userToConceal := user.GetUserByParams(ctx)
281+
userToConceal := user.GetContextUserByPathParam(ctx)
282282
if ctx.Written() {
283283
return
284284
}
@@ -318,7 +318,7 @@ func DeleteMember(ctx *context.APIContext) {
318318
// "404":
319319
// "$ref": "#/responses/notFound"
320320

321-
member := user.GetUserByParams(ctx)
321+
member := user.GetContextUserByPathParam(ctx)
322322
if ctx.Written() {
323323
return
324324
}

routers/api/v1/org/org.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func GetUserOrgsPermissions(ctx *context.APIContext) {
131131
// "$ref": "#/responses/notFound"
132132

133133
var o *user_model.User
134-
if o = user.GetUserByParamsName(ctx, ":org"); o == nil {
134+
if o = user.GetUserByPathParam(ctx, "org"); o == nil {
135135
return
136136
}
137137

routers/api/v1/org/team.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ func GetTeamMember(ctx *context.APIContext) {
449449
// "404":
450450
// "$ref": "#/responses/notFound"
451451

452-
u := user.GetUserByParams(ctx)
452+
u := user.GetContextUserByPathParam(ctx)
453453
if ctx.Written() {
454454
return
455455
}
@@ -492,7 +492,7 @@ func AddTeamMember(ctx *context.APIContext) {
492492
// "404":
493493
// "$ref": "#/responses/notFound"
494494

495-
u := user.GetUserByParams(ctx)
495+
u := user.GetContextUserByPathParam(ctx)
496496
if ctx.Written() {
497497
return
498498
}
@@ -532,7 +532,7 @@ func RemoveTeamMember(ctx *context.APIContext) {
532532
// "404":
533533
// "$ref": "#/responses/notFound"
534534

535-
u := user.GetUserByParams(ctx)
535+
u := user.GetContextUserByPathParam(ctx)
536536
if ctx.Written() {
537537
return
538538
}

routers/api/v1/user/follower.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ func CheckFollowing(ctx *context.APIContext) {
201201
// "404":
202202
// "$ref": "#/responses/notFound"
203203

204-
target := GetUserByParamsName(ctx, ":target")
204+
target := GetUserByPathParam(ctx, "target") // FIXME: it is not right to call this function, it should load the "target" directly
205205
if ctx.Written() {
206206
return
207207
}

routers/api/v1/user/helper.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import (
1010
"code.gitea.io/gitea/services/context"
1111
)
1212

13-
// GetUserByParamsName get user by name
14-
func GetUserByParamsName(ctx *context.APIContext, name string) *user_model.User {
13+
// GetUserByPathParam get user by the path param name
14+
// it will redirect to the user's new name if the user's name has been changed
15+
func GetUserByPathParam(ctx *context.APIContext, name string) *user_model.User {
1516
username := ctx.PathParam(name)
1617
user, err := user_model.GetUserByName(ctx, username)
1718
if err != nil {
@@ -29,7 +30,7 @@ func GetUserByParamsName(ctx *context.APIContext, name string) *user_model.User
2930
return user
3031
}
3132

32-
// GetUserByParams returns user whose name is presented in URL (":username").
33-
func GetUserByParams(ctx *context.APIContext) *user_model.User {
34-
return GetUserByParamsName(ctx, ":username")
33+
// GetContextUserByPathParam returns user whose name is presented in URL (path param "username").
34+
func GetContextUserByPathParam(ctx *context.APIContext) *user_model.User {
35+
return GetUserByPathParam(ctx, "username")
3536
}

services/context/base_path.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ func (b *Base) PathParam(name string) string {
2424

2525
// PathParamRaw returns the raw param in request path, eg: "/{var}" => "/a%2fb", then `var == "a%2fb"`
2626
func (b *Base) PathParamRaw(name string) string {
27-
// this check should be removed before 1.25 release
2827
if strings.HasPrefix(name, ":") {
29-
panic("path param should not start with ':'")
28+
setting.PanicInDevOrTesting("path param should not start with ':'")
29+
name = name[1:]
3030
}
3131
return chi.URLParam(b.Req, name)
3232
}
@@ -38,10 +38,10 @@ func (b *Base) PathParamInt64(p string) int64 {
3838
}
3939

4040
// SetPathParam set request path params into routes
41-
func (b *Base) SetPathParam(k, v string) {
42-
// this check should be removed before 1.25 release
43-
if strings.HasPrefix(k, ":") {
44-
panic("path param should not start with ':'")
41+
func (b *Base) SetPathParam(name, value string) {
42+
if strings.HasPrefix(name, ":") {
43+
setting.PanicInDevOrTesting("path param should not start with ':'")
44+
name = name[1:]
4545
}
46-
chi.RouteContext(b).URLParams.Add(k, url.PathEscape(v))
46+
chi.RouteContext(b).URLParams.Add(name, url.PathEscape(value))
4747
}

0 commit comments

Comments
 (0)