Repository navigation
Provisioning: Stop doubling the app sub path after a provisioned dashboard save - #136808
Conversation
…board save The router history is created with config.appSubUrl as its basename, so navigate() already resolves under the sub path. The provisioned save flows wrapped their redirect paths in locationUtil.assureBaseUrl first, which sent users to /<subpath>/<subpath>/d/... and a "Page not found" page when Grafana runs with serve_from_sub_path. Pass the bare paths instead in SaveProvisionedDashboardForm.handleNewDashboard and in both redirects of useImportProvisionedSave. The save response URL and the preview banner href keep the prefix: they are links, not router targets. Fixes grafana#136789
Signed commits reportAll 3 commits between |
| // Navigation for new dashboards. The router history already carries the app | ||
| // sub path as its basename, so navigate() must get the bare path: prefixing | ||
| // it here lands on /<subpath>/<subpath>/d/... (#136789). | ||
| const url = getDashboardUrl({ | ||
| uid: upsert.metadata.name, | ||
| slug: kbn.slugifyForUrl(upsert.spec.title ?? ''), | ||
| currentQueryParams: window.location.search, | ||
| }); |
There was a problem hiding this comment.
This is the same block as useImportProvisionedSave.ts:123-127, and this PR edits both. Consider one helper next to the other post-save redirect builders in utils/redirect.ts:
/** Router path of a dashboard saved through provisioning. Bare: the router basename already carries appSubUrl. */
export function getSavedDashboardPath(uid: string, title = '') {
return getDashboardUrl({ uid, slug: kbn.slugifyForUrl(title), currentQueryParams: window.location.search });
}handleNewDashboard then becomes navigate(getSavedDashboardPath(upsert.metadata.name, upsert.spec.title)), and the "why bare" lives in one doc comment. The call-site comments and the #136789 references can go: bare paths to navigate() are already the norm in this file (lines 235 and 264), and issue numbers in comments go stale.
There was a problem hiding this comment.
Done in 9a9f974: added getSavedDashboardPath(uid, title = '') to utils/redirect.ts with the one doc comment, and handleNewDashboard is now the single navigate(getSavedDashboardPath(...)) call. Both call-site comments and the issue references are gone. Added three small unit tests for the helper in redirect.test.ts (slug, no title, query params kept).
| // navigate() resolves against the router basename, which is already the | ||
| // app sub path, so the paths below must stay bare (#136789). | ||
| navigate(targetFolderUid ? `/dashboards/f/${targetFolderUid}/` : '/dashboards'); | ||
| return; | ||
| } | ||
| const url = locationUtil.assureBaseUrl( | ||
| getDashboardUrl({ | ||
| uid: dashboardUid, | ||
| slug: kbn.slugifyForUrl(title), | ||
| currentQueryParams: window.location.search, | ||
| }) | ||
| ); | ||
| const url = getDashboardUrl({ | ||
| uid: dashboardUid, | ||
| slug: kbn.slugifyForUrl(title), | ||
| currentQueryParams: window.location.search, | ||
| }); |
There was a problem hiding this comment.
Same builder as SaveProvisionedDashboardForm.tsx:216-220; see the helper suggestion there. Also, this comment sits inside the !dashboardUid branch but describes both navigate() calls. With the helper it can go.
There was a problem hiding this comment.
Switched to the helper here too, so kbn and getDashboardUrl are no longer imported in this hook, and the comment inside the !dashboardUid branch is removed.
| expect(dashboardWatcher.clearIgnoreSave).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('navigates to the new dashboard without repeating the app sub path', async () => { |
There was a problem hiding this comment.
The bug got past the existing test because line 367 uses expect.stringContaining('/d/new-dashboard'), which /grafana/d/new-dashboard also satisfies. Rather than a second copy of that test, consider setting appSubUrl: '/grafana' inside the existing test (reset in a file-level beforeEach, as DashboardPreviewBanner.test.tsx:176-185 does) and changing line 367 to the exact path. That catches the regression with fewer lines, and the stringMatching plus not.toHaveBeenCalledWith pair here becomes unnecessary.
There was a problem hiding this comment.
Agreed, that is a better test. Dropped the copy; should save a new dashboard successfully now sets appSubUrl: '/grafana' and asserts '/d/new-dashboard/new-dashboard' exactly, with the locationUtil reset in the file-level beforeEach as in DashboardPreviewBanner.test.tsx.
| expect(result.current.error).toBeUndefined(); | ||
| }); | ||
|
|
||
| describe('when Grafana is served from a sub path', () => { |
There was a problem hiding this comment.
Same idea as the form test: line 252 uses stringContaining, which also matches the prefixed path. Consider setting the sub path for the save outcomes describe and changing line 252 to the exact path. Line 274 already asserts the exact folder path, so it covers the fallback case with no new test. This describe can then go.
There was a problem hiding this comment.
Done: the save outcomes describe sets the sub path in its own beforeEach (reset at file level), line 252 asserts '/d/new-dash-uid/my-dashboard' exactly, and the extra describe is removed. The folder fallback stays covered by the existing exact assertion.
Both provisioned save flows built the same dashboard path by hand. Move it into getSavedDashboardPath next to the other post-save redirect builders, where one doc comment explains why the path stays bare, and drop the call-site comments. Tighten the existing save tests instead of adding copies: they now run with appSubUrl set and assert the exact path, which is what let the doubled sub path through before.
| // locationUtil keeps module-level config, so reset it between tests | ||
| locationUtil.initialize({ | ||
| config: { appSubUrl: '' } as GrafanaConfig, | ||
| getTimeRangeForUrl: jest.fn(), | ||
| getVariablesUrlParams: jest.fn(), | ||
| }); |
There was a problem hiding this comment.
No test in this file needs the sub path back at '': nothing asserts a prefixed href or the saveCompleted url, and Jest gives each file its own module registry, so nothing leaks across files. Consider setting /grafana once here and dropping the block in should save a new dashboard successfully. One block instead of two, and the other navigate assertions further down (/d/test-dashboard, the preview path) get sub-path coverage too. Worth a run of the file to confirm nothing else depends on the empty base.
There was a problem hiding this comment.
Done in db03585: the file-level beforeEach now sets /grafana and the block in should save a new dashboard successfully is gone, so /d/test-dashboard and the preview path assertions run under the sub path too. I checked the file for anything reading a prefixed value (no href assertions, saveCompleted is only checked with expect.anything() for the url) and found none; I could not run Jest locally, so CI is the confirmation here.
| // locationUtil keeps module-level config, so reset it between tests | ||
| locationUtil.initialize({ | ||
| config: { appSubUrl: '' } as GrafanaConfig, | ||
| getTimeRangeForUrl: jest.fn(), | ||
| getVariablesUrlParams: jest.fn(), | ||
| }); |
There was a problem hiding this comment.
Same here: set /grafana in this beforeEach and drop the one in save outcomes. The remaining tests only inspect the request body or error, so they never read the sub path.
There was a problem hiding this comment.
Done: /grafana is set in the file-level beforeEach and the save outcomes block is removed.
| it('builds the dashboard path from the uid and the slugified title', () => { | ||
| expect(getSavedDashboardPath('abc', 'My Dashboard')).toBe('/d/abc/my-dashboard'); | ||
| }); | ||
|
|
||
| it('omits the slug when there is no title', () => { | ||
| expect(getSavedDashboardPath('abc')).toBe('/d/abc'); | ||
| }); |
There was a problem hiding this comment.
nit: these two cases re-test kbn.slugifyForUrl and getDashboardUrl's slug handling. The query-param case below is the one decision this helper makes (window.location.search rather than '', which the DTO builder in the form passes). Consider keeping only that one.
There was a problem hiding this comment.
Agreed, dropped the two slug cases; only the query-param case remains.
Set appSubUrl once per file instead of per test, so every navigate() assertion in the two files runs under a sub path, and keep only the query-param case for getSavedDashboardPath: the slug cases re-tested getDashboardUrl.
…board save (#136808) * Provisioning: Stop doubling the app sub path after a provisioned dashboard save The router history is created with config.appSubUrl as its basename, so navigate() already resolves under the sub path. The provisioned save flows wrapped their redirect paths in locationUtil.assureBaseUrl first, which sent users to /<subpath>/<subpath>/d/... and a "Page not found" page when Grafana runs with serve_from_sub_path. Pass the bare paths instead in SaveProvisionedDashboardForm.handleNewDashboard and in both redirects of useImportProvisionedSave. The save response URL and the preview banner href keep the prefix: they are links, not router targets. Fixes #136789 * Provisioning: Build the saved-dashboard path in one place Both provisioned save flows built the same dashboard path by hand. Move it into getSavedDashboardPath next to the other post-save redirect builders, where one doc comment explains why the path stays bare, and drop the call-site comments. Tighten the existing save tests instead of adding copies: they now run with appSubUrl set and assert the exact path, which is what let the doubled sub path through before. * Provisioning: Run both save test files under a sub path Set appSubUrl once per file instead of per test, so every navigate() assertion in the two files runs under a sub path, and keep only the query-param case for getSavedDashboardPath: the slug cases re-tested getDashboardUrl.
What is this feature?
Fixes #136789.
When Grafana is served from a sub path (
root_url = http://host/graf1,serve_from_sub_path = true), saving a dashboard into a folder backed by a local file-provisioning repository with thewriteworkflow redirects to/graf1/graf1/d/<uid>/<slug>and shows "Page not found". The save itself succeeds.The router history is created with
config.appSubUrlas its basename (LocationService.tsx), souseNavigate()already resolves every path under the sub path. The provisioned save flows wrapped the paths they hand tonavigate()inlocationUtil.assureBaseUrl(...), which prefixes the sub path a second time:SaveProvisionedDashboardForm.handleNewDashboard(new dashboard saved to a file-provisioned folder, the case in the issue)useImportProvisionedSave(the dashboard redirect after a configured-branch import save, and the folder fallback when sync is disabled)This change passes the bare paths to
navigate()in those three places. The dashboard path is built by a newgetSavedDashboardPath(uid, title)inutils/redirect.ts, next to the other post-save redirect builders, so both flows share it and the "why bare" lives in one doc comment. Theurlfield of the save response DTO increateSaveResponseFromResourcekeepsassureBaseUrl, since that value is a link, not a router target, and the same applies to thehrefinDashboardPreviewBanner.Why do we need this feature?
Same class of bug as #129581 and #128972: anything that goes through the router must not carry the sub path itself.
Who is this feature for?
Anyone running Grafana under a sub path who saves dashboards into Git Sync / file-provisioned folders.
Which issue(s) does this PR fix?
Fixes #136789
Special notes for your reviewer:
SaveProvisionedDashboardForm.test.tsxanduseImportProvisionedSave.test.tsnow run withappSubUrl: '/grafana'and assert the exact path handed tonavigate()(/d/new-dashboard/new-dashboard,/d/new-dash-uid/my-dashboard). They usedstringContaining('/d/...')before, which a doubled sub path also satisfied.appSubUrl: '/grafana'is set in each file'sbeforeEach, so everynavigate()assertion in both files runs under a sub path.redirect.test.tscovers the one decisiongetSavedDashboardPathmakes: it keeps the current query params.Please check that: