From f690ea8efd4cd712d6e8039ab6b91bf9cecad479 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sun, 18 Oct 2026 21:59:51 +0800 Subject: [PATCH] chore: refactor legacy JS init code to use "data-global-init" (#39836) Move the remaining legacy init code, which probed page classes with `querySelector`, to `data-global-init`, so each feature initializes from its own element. `hashchange` listeners now go through one module that runs them in priority order, because independent listeners broke when mount order changed. - Convert issue list, diff, repo create, settings and org team inits to `data-global-init` - Run `hashchange` listeners by priority, the diff scroll handler last - Make the devtest mock's failing log requests deterministic - Add e2e tests for diff tree navigation to folded files and job log permalinks --------- Co-authored-by: silverwind --- routers/web/devtest/mock_actions.go | 13 ++--- templates/devtest/repo-action-view.tmpl | 1 + templates/org/team/new.tmpl | 2 +- templates/repo/create.tmpl | 2 +- templates/repo/diff/box.tmpl | 4 +- .../repo/issue/filter_item_user_fetch.tmpl | 2 +- templates/repo/issue/filters.tmpl | 2 +- templates/repo/issue/list.tmpl | 2 +- templates/repo/settings/githook_edit.tmpl | 2 +- templates/user/settings/profile.tmpl | 2 +- tests/e2e/actions.test.ts | 5 ++ tests/e2e/pr-diff-filter.test.ts | 10 +++- web_src/js/components/ActionRunJobView.vue | 6 +- web_src/js/components/DiffFileTree.vue | 6 +- web_src/js/features/org-team.ts | 9 +-- web_src/js/features/repo-code.ts | 4 +- web_src/js/features/repo-diff-commitselect.ts | 10 ---- web_src/js/features/repo-diff-filetree.ts | 11 ---- web_src/js/features/repo-diff.ts | 26 ++++++--- web_src/js/features/repo-issue-list.ts | 56 ++++++------------- web_src/js/features/repo-legacy.ts | 2 +- web_src/js/features/repo-new.ts | 6 +- web_src/js/features/repo-settings.ts | 8 +-- web_src/js/features/user-settings.ts | 26 ++++----- web_src/js/index.ts | 5 ++ web_src/js/modules/hashchange.ts | 25 +++++++++ 26 files changed, 122 insertions(+), 125 deletions(-) delete mode 100644 web_src/js/features/repo-diff-commitselect.ts delete mode 100644 web_src/js/features/repo-diff-filetree.ts create mode 100644 web_src/js/modules/hashchange.ts diff --git a/routers/web/devtest/mock_actions.go b/routers/web/devtest/mock_actions.go index f61becd65fd..2feb185cab0 100644 --- a/routers/web/devtest/mock_actions.go +++ b/routers/web/devtest/mock_actions.go @@ -6,7 +6,6 @@ package devtest import ( "fmt" "maps" - mathRand "math/rand/v2" "net/http" "net/url" "slices" @@ -597,7 +596,7 @@ func fillViewRunResponseCurrentJob(ctx *context.Context, resp *actions.ViewRespo mockLogOptions = append(mockLogOptions, generateMockStepsLogOptions{mockCountFirst: 30, mockCountGeneral: 3, groupRepeat: 20}) resp.State.CurrentJob.Steps = append(resp.State.CurrentJob.Steps, &actions.ViewJobStep{ - Summary: "step 2 (mock error)", + Summary: "step 2", Duration: time.Hour.String(), Status: actions_model.StatusRunning.String(), }) @@ -609,20 +608,16 @@ func fillViewRunResponseCurrentJob(ctx *context.Context, resp *actions.ViewRespo resp.Logs.StepsLog = []*actions.ViewStepLog{} doSlowResponse := false - doErrorResponse := false for _, logCur := range req.LogCursors { if !logCur.Expanded { continue } doSlowResponse = doSlowResponse || logCur.Step == 0 - doErrorResponse = doErrorResponse || logCur.Step == 2 resp.Logs.StepsLog = append(resp.Logs.StepsLog, generateMockStepsLog(logCur, mockLogOptions[logCur.Step])...) } - if doErrorResponse { - if mathRand.Float64() > 0.5 { - ctx.HTTPError(http.StatusInternalServerError, "devtest mock error response") - return - } + if ctx.PathParamInt64("run") == 50 && len(resp.Logs.StepsLog) > 0 { + ctx.HTTPError(http.StatusInternalServerError, "devtest mock error response") + return } if doSlowResponse { time.Sleep(time.Duration(3000) * time.Millisecond) diff --git a/templates/devtest/repo-action-view.tmpl b/templates/devtest/repo-action-view.tmpl index 12da63a7e9e..6fa52028f68 100644 --- a/templates/devtest/repo-action-view.tmpl +++ b/templates/devtest/repo-action-view.tmpl @@ -6,6 +6,7 @@ Run:CanRerunLatest Run:PreviousAttempt Run:ReusableCaller + Run:LogsAlwaysFail {{template "repo/actions/view_component" (dict "JobID" (or .JobID 0) diff --git a/templates/org/team/new.tmpl b/templates/org/team/new.tmpl index 45503ac8fe4..b34a12005b0 100644 --- a/templates/org/team/new.tmpl +++ b/templates/org/team/new.tmpl @@ -1,5 +1,5 @@ {{template "base/head" .}} -
+
{{template "org/header" .}}
diff --git a/templates/repo/create.tmpl b/templates/repo/create.tmpl index 767210173b0..59dda087b27 100644 --- a/templates/repo/create.tmpl +++ b/templates/repo/create.tmpl @@ -7,7 +7,7 @@
{{template "base/alert" .}} {{template "repo/create_helper" .}} -
+
diff --git a/templates/repo/diff/box.tmpl b/templates/repo/diff/box.tmpl index d2da9e5f39b..7993ec021a7 100644 --- a/templates/repo/diff/box.tmpl +++ b/templates/repo/diff/box.tmpl @@ -35,7 +35,7 @@ {{template "repo/diff/whitespace_dropdown" .}} {{template "repo/diff/options_dropdown" .}} {{if .PageIsPullFiles}} -
{{/* the following will be replaced by vue component, but this avoids any loading artifacts till the vue component is initialized */}} @@ -63,7 +63,7 @@
{{if $showFileTree}} {{$.FileIconPoolHTML}} -
{{if and $.CanWriteIssuesOrPulls .Issues}} - + {{end}} {{template "repo/issue/openclose" .}} diff --git a/templates/repo/issue/list.tmpl b/templates/repo/issue/list.tmpl index 0d0d8e71f2e..363d4e61c42 100644 --- a/templates/repo/issue/list.tmpl +++ b/templates/repo/issue/list.tmpl @@ -7,7 +7,7 @@ {{template "repo/code/recently_pushed_new_branches" dict "RecentBranchesPromptData" .RecentBranchesPromptData}} {{if .PinnedIssues}} -
+
{{range .PinnedIssues}}
{{template "repo/issue/card" (dict "Issue" . "Page" $ "isPinnedIssueCard" true)}} diff --git a/templates/repo/settings/githook_edit.tmpl b/templates/repo/settings/githook_edit.tmpl index 8e42e3929f4..82a2af87b84 100644 --- a/templates/repo/settings/githook_edit.tmpl +++ b/templates/repo/settings/githook_edit.tmpl @@ -1,6 +1,6 @@ {{template "repo/settings/layout_head" (dict "pageClass" "repository settings edit githook")}}
- +

{{.Hook.Name}}
diff --git a/templates/user/settings/profile.tmpl b/templates/user/settings/profile.tmpl index 7e3b906d2a0..2a43688197d 100644 --- a/templates/user/settings/profile.tmpl +++ b/templates/user/settings/profile.tmpl @@ -5,7 +5,7 @@

{{ctx.Locale.Tr "settings.profile_desc"}}

- +