Skip to content

Commit 2a828e2

Browse files
authored
Clarify path param naming (#32969)
In history (from some legacy frameworks), both `:name` and `name` are supported as path path name, `:name` is an alias to `name`. To make code consistent, now we should only use `name` but not `:name`. Also added panic check in related functions to make sure the name won't be abused in case some downstreams still use them.
1 parent b8b690f commit 2a828e2

Some content is hidden

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

102 files changed

+461
-429
lines changed

modules/setting/setting.go

+6
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

+1-3
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/admin/adopt.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ func AdoptRepository(ctx *context.APIContext) {
8080
// "$ref": "#/responses/notFound"
8181
// "403":
8282
// "$ref": "#/responses/forbidden"
83-
ownerName := ctx.PathParam(":username")
84-
repoName := ctx.PathParam(":reponame")
83+
ownerName := ctx.PathParam("username")
84+
repoName := ctx.PathParam("reponame")
8585

8686
ctxUser, err := user_model.GetUserByName(ctx, ownerName)
8787
if err != nil {
@@ -142,8 +142,8 @@ func DeleteUnadoptedRepository(ctx *context.APIContext) {
142142
// "$ref": "#/responses/empty"
143143
// "403":
144144
// "$ref": "#/responses/forbidden"
145-
ownerName := ctx.PathParam(":username")
146-
repoName := ctx.PathParam(":reponame")
145+
ownerName := ctx.PathParam("username")
146+
repoName := ctx.PathParam("reponame")
147147

148148
ctxUser, err := user_model.GetUserByName(ctx, ownerName)
149149
if err != nil {

routers/api/v1/admin/cron.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func PostCronTask(ctx *context.APIContext) {
7474
// "$ref": "#/responses/empty"
7575
// "404":
7676
// "$ref": "#/responses/notFound"
77-
task := cron.GetTask(ctx.PathParam(":task"))
77+
task := cron.GetTask(ctx.PathParam("task"))
7878
if task == nil {
7979
ctx.NotFound()
8080
return

routers/api/v1/admin/email.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func GetAllEmails(ctx *context.APIContext) {
3838
listOptions := utils.GetListOptions(ctx)
3939

4040
emails, maxResults, err := user_model.SearchEmails(ctx, &user_model.SearchEmailOptions{
41-
Keyword: ctx.PathParam(":email"),
41+
Keyword: ctx.PathParam("email"),
4242
ListOptions: listOptions,
4343
})
4444
if err != nil {
@@ -82,6 +82,6 @@ func SearchEmail(ctx *context.APIContext) {
8282
// "403":
8383
// "$ref": "#/responses/forbidden"
8484

85-
ctx.SetPathParam(":email", ctx.FormTrim("q"))
85+
ctx.SetPathParam("email", ctx.FormTrim("q"))
8686
GetAllEmails(ctx)
8787
}

routers/api/v1/admin/hooks.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func GetHook(ctx *context.APIContext) {
7373
// "200":
7474
// "$ref": "#/responses/Hook"
7575

76-
hookID := ctx.PathParamInt64(":id")
76+
hookID := ctx.PathParamInt64("id")
7777
hook, err := webhook.GetSystemOrDefaultWebhook(ctx, hookID)
7878
if err != nil {
7979
if errors.Is(err, util.ErrNotExist) {
@@ -142,7 +142,7 @@ func EditHook(ctx *context.APIContext) {
142142
form := web.GetForm(ctx).(*api.EditHookOption)
143143

144144
// TODO in body params
145-
hookID := ctx.PathParamInt64(":id")
145+
hookID := ctx.PathParamInt64("id")
146146
utils.EditSystemHook(ctx, form, hookID)
147147
}
148148

@@ -164,7 +164,7 @@ func DeleteHook(ctx *context.APIContext) {
164164
// "204":
165165
// "$ref": "#/responses/empty"
166166

167-
hookID := ctx.PathParamInt64(":id")
167+
hookID := ctx.PathParamInt64("id")
168168
if err := webhook.DeleteDefaultSystemWebhook(ctx, hookID); err != nil {
169169
if errors.Is(err, util.ErrNotExist) {
170170
ctx.NotFound()

routers/api/v1/admin/user.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ func DeleteUserPublicKey(ctx *context.APIContext) {
375375
// "404":
376376
// "$ref": "#/responses/notFound"
377377

378-
if err := asymkey_service.DeletePublicKey(ctx, ctx.ContextUser, ctx.PathParamInt64(":id")); err != nil {
378+
if err := asymkey_service.DeletePublicKey(ctx, ctx.ContextUser, ctx.PathParamInt64("id")); err != nil {
379379
if asymkey_model.IsErrKeyNotExist(err) {
380380
ctx.NotFound()
381381
} else if asymkey_model.IsErrKeyAccessDenied(err) {

routers/api/v1/api.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -596,12 +596,12 @@ func orgAssignment(args ...bool) func(ctx *context.APIContext) {
596596

597597
var err error
598598
if assignOrg {
599-
ctx.Org.Organization, err = organization.GetOrgByName(ctx, ctx.PathParam(":org"))
599+
ctx.Org.Organization, err = organization.GetOrgByName(ctx, ctx.PathParam("org"))
600600
if err != nil {
601601
if organization.IsErrOrgNotExist(err) {
602-
redirectUserID, err := user_model.LookupUserRedirect(ctx, ctx.PathParam(":org"))
602+
redirectUserID, err := user_model.LookupUserRedirect(ctx, ctx.PathParam("org"))
603603
if err == nil {
604-
context.RedirectToUser(ctx.Base, ctx.PathParam(":org"), redirectUserID)
604+
context.RedirectToUser(ctx.Base, ctx.PathParam("org"), redirectUserID)
605605
} else if user_model.IsErrUserRedirectNotExist(err) {
606606
ctx.NotFound("GetOrgByName", err)
607607
} else {
@@ -616,7 +616,7 @@ func orgAssignment(args ...bool) func(ctx *context.APIContext) {
616616
}
617617

618618
if assignTeam {
619-
ctx.Org.Team, err = organization.GetTeamByID(ctx, ctx.PathParamInt64(":teamid"))
619+
ctx.Org.Team, err = organization.GetTeamByID(ctx, ctx.PathParamInt64("teamid"))
620620
if err != nil {
621621
if organization.IsErrTeamNotExist(err) {
622622
ctx.NotFound()

routers/api/v1/notify/threads.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func ReadThread(ctx *context.APIContext) {
101101
}
102102

103103
func getThread(ctx *context.APIContext) *activities_model.Notification {
104-
n, err := activities_model.GetNotificationByID(ctx, ctx.PathParamInt64(":id"))
104+
n, err := activities_model.GetNotificationByID(ctx, ctx.PathParamInt64("id"))
105105
if err != nil {
106106
if db.IsErrNotExist(err) {
107107
ctx.Error(http.StatusNotFound, "GetNotificationByID", err)

routers/api/v1/org/label.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func GetLabel(ctx *context.APIContext) {
139139
label *issues_model.Label
140140
err error
141141
)
142-
strID := ctx.PathParam(":id")
142+
strID := ctx.PathParam("id")
143143
if intID, err2 := strconv.ParseInt(strID, 10, 64); err2 != nil {
144144
label, err = issues_model.GetLabelInOrgByName(ctx, ctx.Org.Organization.ID, strID)
145145
} else {
@@ -190,7 +190,7 @@ func EditLabel(ctx *context.APIContext) {
190190
// "422":
191191
// "$ref": "#/responses/validationError"
192192
form := web.GetForm(ctx).(*api.EditLabelOption)
193-
l, err := issues_model.GetLabelInOrgByID(ctx, ctx.Org.Organization.ID, ctx.PathParamInt64(":id"))
193+
l, err := issues_model.GetLabelInOrgByID(ctx, ctx.Org.Organization.ID, ctx.PathParamInt64("id"))
194194
if err != nil {
195195
if issues_model.IsErrOrgLabelNotExist(err) {
196196
ctx.NotFound()
@@ -249,7 +249,7 @@ func DeleteLabel(ctx *context.APIContext) {
249249
// "404":
250250
// "$ref": "#/responses/notFound"
251251

252-
if err := issues_model.DeleteLabel(ctx, ctx.Org.Organization.ID, ctx.PathParamInt64(":id")); err != nil {
252+
if err := issues_model.DeleteLabel(ctx, ctx.Org.Organization.ID, ctx.PathParamInt64("id")); err != nil {
253253
ctx.Error(http.StatusInternalServerError, "DeleteLabel", err)
254254
return
255255
}

routers/api/v1/org/member.go

+5-5
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

+1-1
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

+4-4
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
}
@@ -645,7 +645,7 @@ func GetTeamRepo(ctx *context.APIContext) {
645645

646646
// getRepositoryByParams get repository by a team's organization ID and repo name
647647
func getRepositoryByParams(ctx *context.APIContext) *repo_model.Repository {
648-
repo, err := repo_model.GetRepositoryByName(ctx, ctx.Org.Team.OrgID, ctx.PathParam(":reponame"))
648+
repo, err := repo_model.GetRepositoryByName(ctx, ctx.Org.Team.OrgID, ctx.PathParam("reponame"))
649649
if err != nil {
650650
if repo_model.IsErrRepoNotExist(err) {
651651
ctx.NotFound()

routers/api/v1/repo/branch.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ func GetBranchProtection(ctx *context.APIContext) {
487487
// "$ref": "#/responses/notFound"
488488

489489
repo := ctx.Repo.Repository
490-
bpName := ctx.PathParam(":name")
490+
bpName := ctx.PathParam("name")
491491
bp, err := git_model.GetProtectedBranchRuleByName(ctx, repo.ID, bpName)
492492
if err != nil {
493493
ctx.Error(http.StatusInternalServerError, "GetProtectedBranchByID", err)
@@ -805,7 +805,7 @@ func EditBranchProtection(ctx *context.APIContext) {
805805
// "$ref": "#/responses/repoArchivedError"
806806
form := web.GetForm(ctx).(*api.EditBranchProtectionOption)
807807
repo := ctx.Repo.Repository
808-
bpName := ctx.PathParam(":name")
808+
bpName := ctx.PathParam("name")
809809
protectBranch, err := git_model.GetProtectedBranchRuleByName(ctx, repo.ID, bpName)
810810
if err != nil {
811811
ctx.Error(http.StatusInternalServerError, "GetProtectedBranchByID", err)
@@ -1124,7 +1124,7 @@ func DeleteBranchProtection(ctx *context.APIContext) {
11241124
// "$ref": "#/responses/notFound"
11251125

11261126
repo := ctx.Repo.Repository
1127-
bpName := ctx.PathParam(":name")
1127+
bpName := ctx.PathParam("name")
11281128
bp, err := git_model.GetProtectedBranchRuleByName(ctx, repo.ID, bpName)
11291129
if err != nil {
11301130
ctx.Error(http.StatusInternalServerError, "GetProtectedBranchByID", err)

routers/api/v1/repo/collaborators.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func IsCollaborator(ctx *context.APIContext) {
103103
// "422":
104104
// "$ref": "#/responses/validationError"
105105

106-
user, err := user_model.GetUserByName(ctx, ctx.PathParam(":collaborator"))
106+
user, err := user_model.GetUserByName(ctx, ctx.PathParam("collaborator"))
107107
if err != nil {
108108
if user_model.IsErrUserNotExist(err) {
109109
ctx.Error(http.StatusUnprocessableEntity, "", err)
@@ -163,7 +163,7 @@ func AddOrUpdateCollaborator(ctx *context.APIContext) {
163163

164164
form := web.GetForm(ctx).(*api.AddCollaboratorOption)
165165

166-
collaborator, err := user_model.GetUserByName(ctx, ctx.PathParam(":collaborator"))
166+
collaborator, err := user_model.GetUserByName(ctx, ctx.PathParam("collaborator"))
167167
if err != nil {
168168
if user_model.IsErrUserNotExist(err) {
169169
ctx.Error(http.StatusUnprocessableEntity, "", err)
@@ -226,7 +226,7 @@ func DeleteCollaborator(ctx *context.APIContext) {
226226
// "422":
227227
// "$ref": "#/responses/validationError"
228228

229-
collaborator, err := user_model.GetUserByName(ctx, ctx.PathParam(":collaborator"))
229+
collaborator, err := user_model.GetUserByName(ctx, ctx.PathParam("collaborator"))
230230
if err != nil {
231231
if user_model.IsErrUserNotExist(err) {
232232
ctx.Error(http.StatusUnprocessableEntity, "", err)
@@ -274,12 +274,12 @@ func GetRepoPermissions(ctx *context.APIContext) {
274274
// "403":
275275
// "$ref": "#/responses/forbidden"
276276

277-
if !ctx.Doer.IsAdmin && ctx.Doer.LoginName != ctx.PathParam(":collaborator") && !ctx.IsUserRepoAdmin() {
277+
if !ctx.Doer.IsAdmin && ctx.Doer.LoginName != ctx.PathParam("collaborator") && !ctx.IsUserRepoAdmin() {
278278
ctx.Error(http.StatusForbidden, "User", "Only admins can query all permissions, repo admins can query all repo permissions, collaborators can query only their own")
279279
return
280280
}
281281

282-
collaborator, err := user_model.GetUserByName(ctx, ctx.PathParam(":collaborator"))
282+
collaborator, err := user_model.GetUserByName(ctx, ctx.PathParam("collaborator"))
283283
if err != nil {
284284
if user_model.IsErrUserNotExist(err) {
285285
ctx.Error(http.StatusNotFound, "GetUserByName", err)

routers/api/v1/repo/commits.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func GetSingleCommit(ctx *context.APIContext) {
6363
// "404":
6464
// "$ref": "#/responses/notFound"
6565

66-
sha := ctx.PathParam(":sha")
66+
sha := ctx.PathParam("sha")
6767
if !git.IsValidRefPattern(sha) {
6868
ctx.Error(http.StatusUnprocessableEntity, "no valid ref or sha", fmt.Sprintf("no valid ref or sha: %s", sha))
6969
return
@@ -312,8 +312,8 @@ func DownloadCommitDiffOrPatch(ctx *context.APIContext) {
312312
// "$ref": "#/responses/string"
313313
// "404":
314314
// "$ref": "#/responses/notFound"
315-
sha := ctx.PathParam(":sha")
316-
diffType := git.RawDiffType(ctx.PathParam(":diffType"))
315+
sha := ctx.PathParam("sha")
316+
diffType := git.RawDiffType(ctx.PathParam("diffType"))
317317

318318
if err := git.GetRawDiff(ctx.Repo.GitRepo, sha, diffType, ctx.Resp); err != nil {
319319
if git.IsErrNotExist(err) {

routers/api/v1/repo/git_hook.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func GetGitHook(ctx *context.APIContext) {
7979
// "404":
8080
// "$ref": "#/responses/notFound"
8181

82-
hookID := ctx.PathParam(":id")
82+
hookID := ctx.PathParam("id")
8383
hook, err := ctx.Repo.GitRepo.GetHook(hookID)
8484
if err != nil {
8585
if err == git.ErrNotValidHook {
@@ -126,7 +126,7 @@ func EditGitHook(ctx *context.APIContext) {
126126
// "$ref": "#/responses/notFound"
127127

128128
form := web.GetForm(ctx).(*api.EditGitHookOption)
129-
hookID := ctx.PathParam(":id")
129+
hookID := ctx.PathParam("id")
130130
hook, err := ctx.Repo.GitRepo.GetHook(hookID)
131131
if err != nil {
132132
if err == git.ErrNotValidHook {
@@ -175,7 +175,7 @@ func DeleteGitHook(ctx *context.APIContext) {
175175
// "404":
176176
// "$ref": "#/responses/notFound"
177177

178-
hookID := ctx.PathParam(":id")
178+
hookID := ctx.PathParam("id")
179179
hook, err := ctx.Repo.GitRepo.GetHook(hookID)
180180
if err != nil {
181181
if err == git.ErrNotValidHook {

0 commit comments

Comments
 (0)