From 7430a18bd3a5a6cc80c5784e39d22185e5701dc5 Mon Sep 17 00:00:00 2001 From: Stephanie Hingtgen Date: Tue, 29 Apr 2025 20:23:52 -0600 Subject: [PATCH] Dashboards: Fix missing folder info in /search for dashboards (#104666) Dashboards: add missing folder info to /search --- .../dashboards/service/dashboard_service.go | 27 +++++++++++++++---- .../service/dashboard_service_test.go | 9 +++++++ 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/pkg/services/dashboards/service/dashboard_service.go b/pkg/services/dashboards/service/dashboard_service.go index cffd14ccca0..9256c615779 100644 --- a/pkg/services/dashboards/service/dashboard_service.go +++ b/pkg/services/dashboards/service/dashboard_service.go @@ -1541,6 +1541,13 @@ func (dr *DashboardServiceImpl) FindDashboards(ctx context.Context, query *dashb finalResults := make([]dashboards.DashboardSearchProjection, len(response.Hits)) for i, hit := range response.Hits { + folderTitle := "" + folderID := int64(0) + if f, ok := folderNames[hit.Folder]; ok { + folderTitle = f.Title + folderID = f.ID + } + result := dashboards.DashboardSearchProjection{ ID: hit.Field.GetNestedInt64(resource.SEARCH_FIELD_LEGACY_ID), UID: hit.Name, @@ -1549,7 +1556,9 @@ func (dr *DashboardServiceImpl) FindDashboards(ctx context.Context, query *dashb Slug: slugify.Slugify(hit.Title), IsFolder: false, FolderUID: hit.Folder, - FolderTitle: folderNames[hit.Folder], + FolderTitle: folderTitle, + FolderID: folderID, + FolderSlug: slugify.Slugify(folderTitle), Tags: hit.Tags, } @@ -1574,7 +1583,12 @@ func (dr *DashboardServiceImpl) FindDashboards(ctx context.Context, query *dashb return dr.dashboardStore.FindDashboards(ctx, query) } -func (dr *DashboardServiceImpl) fetchFolderNames(ctx context.Context, query *dashboards.FindPersistedDashboardsQuery, hits []dashboardv0.DashboardHit) (map[string]string, error) { +type folderRes struct { + Title string + ID int64 +} + +func (dr *DashboardServiceImpl) fetchFolderNames(ctx context.Context, query *dashboards.FindPersistedDashboardsQuery, hits []dashboardv0.DashboardHit) (map[string]folderRes, error) { // call this with elevated permissions so we can get folder names where user does not have access // some dashboards are shared directly with user, but the folder is not accessible via the folder permissions serviceCtx, serviceIdent := identity.WithServiceIdentity(ctx, query.OrgId) @@ -1589,9 +1603,12 @@ func (dr *DashboardServiceImpl) fetchFolderNames(ctx context.Context, query *das return nil, folder.ErrInternal.Errorf("failed to fetch parent folders: %w", err) } - folderNames := make(map[string]string) + folderNames := make(map[string]folderRes) for _, f := range folders { - folderNames[f.UID] = f.Title + folderNames[f.UID] = folderRes{ + Title: f.Title, + ID: f.ID, + } } return folderNames, nil } @@ -1671,7 +1688,7 @@ func makeQueryResult(query *dashboards.FindPersistedDashboardsQuery, res []dashb } // nolint:staticcheck - if item.FolderID > 0 { + if item.FolderID > 0 || item.FolderUID != "" { hit.FolderURL = dashboards.GetFolderURL(item.FolderUID, item.FolderSlug) } diff --git a/pkg/services/dashboards/service/dashboard_service_test.go b/pkg/services/dashboards/service/dashboard_service_test.go index a6607d36800..ba0407dc932 100644 --- a/pkg/services/dashboards/service/dashboard_service_test.go +++ b/pkg/services/dashboards/service/dashboard_service_test.go @@ -1586,6 +1586,8 @@ func TestSearchDashboards(t *testing.T) { }, FolderTitle: "testing-folder-1", FolderUID: "f1", + FolderID: 1, + FolderURL: "/dashboards/f/f1/testing-folder-1", }, { UID: "uid2", @@ -1597,6 +1599,8 @@ func TestSearchDashboards(t *testing.T) { Tags: []string{}, FolderTitle: "testing-folder-1", FolderUID: "f1", + FolderID: 1, + FolderURL: "/dashboards/f/f1/testing-folder-1", }, } query := dashboards.FindPersistedDashboardsQuery{ @@ -1612,7 +1616,9 @@ func TestSearchDashboards(t *testing.T) { Title: "Dashboard 1", Tags: []string{"tag1", "tag2"}, FolderTitle: "testing-folder-1", + FolderSlug: "testing-folder-1", FolderUID: "f1", + FolderID: 1, }, { UID: "uid2", @@ -1620,7 +1626,9 @@ func TestSearchDashboards(t *testing.T) { OrgID: 1, Title: "Dashboard 2", FolderTitle: "testing-folder-1", + FolderSlug: "testing-folder-1", FolderUID: "f1", + FolderID: 1, }, }, nil).Once() result, err := service.SearchDashboards(context.Background(), &query) @@ -1635,6 +1643,7 @@ func TestSearchDashboards(t *testing.T) { { UID: "f1", Title: "testing-folder-1", + ID: 1, }, } fakeFolders.ExpectedHitList = expectedFolders