diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2023-08-25 19:07:42 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-25 19:07:42 +0800 |
commit | 412e5c0946fc5b83456685bc2fb1aed682228f57 (patch) | |
tree | 1dc1a823618d554f740cff449e20c1538ad95ba3 /routers/web | |
parent | ee9e83b230981437acf8c331e93a41cdc95af443 (diff) | |
download | gitea-412e5c0946fc5b83456685bc2fb1aed682228f57.tar.gz gitea-412e5c0946fc5b83456685bc2fb1aed682228f57.zip |
Make web context initialize correctly for different cases (#26726)
The web context (modules/context.Context) is quite complex, it's
difficult for the callers to initialize correctly.
This PR introduces a `NewWebContext` function, to make sure the web
context have the same behavior for different cases.
Diffstat (limited to 'routers/web')
-rw-r--r-- | routers/web/repo/actions/actions.go | 2 | ||||
-rw-r--r-- | routers/web/repo/helper.go | 7 | ||||
-rw-r--r-- | routers/web/repo/helper_test.go | 7 | ||||
-rw-r--r-- | routers/web/repo/issue.go | 6 | ||||
-rw-r--r-- | routers/web/repo/pull.go | 2 | ||||
-rw-r--r-- | routers/web/repo/release.go | 4 |
6 files changed, 13 insertions, 15 deletions
diff --git a/routers/web/repo/actions/actions.go b/routers/web/repo/actions/actions.go index b0f4b6f897..6284d21463 100644 --- a/routers/web/repo/actions/actions.go +++ b/routers/web/repo/actions/actions.go @@ -191,7 +191,7 @@ func List(ctx *context.Context) { ctx.Error(http.StatusInternalServerError, err.Error()) return } - ctx.Data["Actors"] = repo.MakeSelfOnTop(ctx, actors) + ctx.Data["Actors"] = repo.MakeSelfOnTop(ctx.Doer, actors) ctx.Data["StatusInfoList"] = actions_model.GetStatusInfoList(ctx) diff --git a/routers/web/repo/helper.go b/routers/web/repo/helper.go index fb5ada1bdb..f8cdefdc8e 100644 --- a/routers/web/repo/helper.go +++ b/routers/web/repo/helper.go @@ -7,16 +7,15 @@ import ( "sort" "code.gitea.io/gitea/models/user" - "code.gitea.io/gitea/modules/context" ) -func MakeSelfOnTop(ctx *context.Context, users []*user.User) []*user.User { - if ctx.Doer != nil { +func MakeSelfOnTop(doer *user.User, users []*user.User) []*user.User { + if doer != nil { sort.Slice(users, func(i, j int) bool { if users[i].ID == users[j].ID { return false } - return users[i].ID == ctx.Doer.ID // if users[i] is self, put it before others, so less=true + return users[i].ID == doer.ID // if users[i] is self, put it before others, so less=true }) } return users diff --git a/routers/web/repo/helper_test.go b/routers/web/repo/helper_test.go index 226e2e81f4..978758e77f 100644 --- a/routers/web/repo/helper_test.go +++ b/routers/web/repo/helper_test.go @@ -7,21 +7,20 @@ import ( "testing" "code.gitea.io/gitea/models/user" - "code.gitea.io/gitea/modules/context" "github.com/stretchr/testify/assert" ) func TestMakeSelfOnTop(t *testing.T) { - users := MakeSelfOnTop(&context.Context{}, []*user.User{{ID: 2}, {ID: 1}}) + users := MakeSelfOnTop(nil, []*user.User{{ID: 2}, {ID: 1}}) assert.Len(t, users, 2) assert.EqualValues(t, 2, users[0].ID) - users = MakeSelfOnTop(&context.Context{Doer: &user.User{ID: 1}}, []*user.User{{ID: 2}, {ID: 1}}) + users = MakeSelfOnTop(&user.User{ID: 1}, []*user.User{{ID: 2}, {ID: 1}}) assert.Len(t, users, 2) assert.EqualValues(t, 1, users[0].ID) - users = MakeSelfOnTop(&context.Context{Doer: &user.User{ID: 2}}, []*user.User{{ID: 2}, {ID: 1}}) + users = MakeSelfOnTop(&user.User{ID: 2}, []*user.User{{ID: 2}, {ID: 1}}) assert.Len(t, users, 2) assert.EqualValues(t, 2, users[0].ID) } diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index 9a2add1452..b8b5a2dff2 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -331,7 +331,7 @@ func issues(ctx *context.Context, milestoneID, projectID int64, isPullOption uti ctx.ServerError("GetRepoAssignees", err) return } - ctx.Data["Assignees"] = MakeSelfOnTop(ctx, assigneeUsers) + ctx.Data["Assignees"] = MakeSelfOnTop(ctx.Doer, assigneeUsers) handleTeamMentions(ctx) if ctx.Written() { @@ -535,7 +535,7 @@ func RetrieveRepoMilestonesAndAssignees(ctx *context.Context, repo *repo_model.R ctx.ServerError("GetRepoAssignees", err) return } - ctx.Data["Assignees"] = MakeSelfOnTop(ctx, assigneeUsers) + ctx.Data["Assignees"] = MakeSelfOnTop(ctx.Doer, assigneeUsers) handleTeamMentions(ctx) } @@ -3625,7 +3625,7 @@ func issuePosters(ctx *context.Context, isPullList bool) { } } - posters = MakeSelfOnTop(ctx, posters) + posters = MakeSelfOnTop(ctx.Doer, posters) resp := &userSearchResponse{} resp.Results = make([]*userSearchInfo, len(posters)) diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go index e3854779fe..e697a0d5b6 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -956,7 +956,7 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi ctx.ServerError("GetRepoAssignees", err) return } - ctx.Data["Assignees"] = MakeSelfOnTop(ctx, assigneeUsers) + ctx.Data["Assignees"] = MakeSelfOnTop(ctx.Doer, assigneeUsers) handleTeamMentions(ctx) if ctx.Written() { diff --git a/routers/web/repo/release.go b/routers/web/repo/release.go index 957cf56972..138df45857 100644 --- a/routers/web/repo/release.go +++ b/routers/web/repo/release.go @@ -349,7 +349,7 @@ func NewRelease(ctx *context.Context) { ctx.ServerError("GetRepoAssignees", err) return } - ctx.Data["Assignees"] = MakeSelfOnTop(ctx, assigneeUsers) + ctx.Data["Assignees"] = MakeSelfOnTop(ctx.Doer, assigneeUsers) upload.AddUploadContext(ctx, "release") @@ -538,7 +538,7 @@ func EditRelease(ctx *context.Context) { ctx.ServerError("GetRepoAssignees", err) return } - ctx.Data["Assignees"] = MakeSelfOnTop(ctx, assigneeUsers) + ctx.Data["Assignees"] = MakeSelfOnTop(ctx.Doer, assigneeUsers) ctx.HTML(http.StatusOK, tplReleaseNew) } |