Search: Fix empty folder details for nested folder items (#76504)
* Introduce dashboard.folder_uid column * Add data migration * Search: Fix empty folder details for nested folders * Set `dashboard.folder_uid` and update tests * Add unique index * lint Ignore cyclomatic complexity of func `(*DashboardServiceImpl).BuildSaveDashboardCommand * Fix search by folder UID
This commit is contained in:
+10
-3
@@ -412,10 +412,13 @@ func (hs *HTTPServer) postDashboard(c *contextmodel.ReqContext, cmd dashboards.S
|
||||
|
||||
cmd.OrgID = c.SignedInUser.GetOrgID()
|
||||
cmd.UserID = userID
|
||||
if cmd.FolderUID != "" {
|
||||
// nolint:staticcheck
|
||||
if cmd.FolderUID != "" || cmd.FolderID != 0 {
|
||||
folder, err := hs.folderService.Get(ctx, &folder.GetFolderQuery{
|
||||
OrgID: c.SignedInUser.GetOrgID(),
|
||||
UID: &cmd.FolderUID,
|
||||
OrgID: c.SignedInUser.GetOrgID(),
|
||||
UID: &cmd.FolderUID,
|
||||
// nolint:staticcheck
|
||||
ID: &cmd.FolderID,
|
||||
SignedInUser: c.SignedInUser,
|
||||
})
|
||||
if err != nil {
|
||||
@@ -424,7 +427,9 @@ func (hs *HTTPServer) postDashboard(c *contextmodel.ReqContext, cmd dashboards.S
|
||||
}
|
||||
return response.Error(http.StatusInternalServerError, "Error while checking folder ID", err)
|
||||
}
|
||||
// nolint:staticcheck
|
||||
cmd.FolderID = folder.ID
|
||||
cmd.FolderUID = folder.UID
|
||||
}
|
||||
|
||||
dash := cmd.GetDashboardModel()
|
||||
@@ -1073,7 +1078,9 @@ func (hs *HTTPServer) RestoreDashboardVersion(c *contextmodel.ReqContext) respon
|
||||
saveCmd.Dashboard.Set("version", dash.Version)
|
||||
saveCmd.Dashboard.Set("uid", dash.UID)
|
||||
saveCmd.Message = fmt.Sprintf("Restored from version %d", version.Version)
|
||||
// nolint:staticcheck
|
||||
saveCmd.FolderID = dash.FolderID
|
||||
saveCmd.FolderUID = dash.FolderUID
|
||||
|
||||
return hs.postDashboard(c, saveCmd)
|
||||
}
|
||||
|
||||
@@ -394,6 +394,7 @@ func TestDashboardAPIEndpoint(t *testing.T) {
|
||||
// This tests that a valid request returns correct response
|
||||
t.Run("Given a correct request for creating a dashboard", func(t *testing.T) {
|
||||
const folderID int64 = 3
|
||||
folderUID := "Folder"
|
||||
const dashID int64 = 2
|
||||
|
||||
cmd := dashboards.SaveDashboardCommand{
|
||||
@@ -404,15 +405,19 @@ func TestDashboardAPIEndpoint(t *testing.T) {
|
||||
}),
|
||||
Overwrite: true,
|
||||
FolderID: folderID,
|
||||
FolderUID: folderUID,
|
||||
IsFolder: false,
|
||||
Message: "msg",
|
||||
}
|
||||
|
||||
dashboardService := dashboards.NewFakeDashboardService(t)
|
||||
dashboardService.On("SaveDashboard", mock.Anything, mock.AnythingOfType("*dashboards.SaveDashboardDTO"), mock.AnythingOfType("bool")).
|
||||
Return(&dashboards.Dashboard{ID: dashID, UID: "uid", Title: "Dash", Slug: "dash", Version: 2}, nil)
|
||||
Return(&dashboards.Dashboard{ID: dashID, UID: "uid", Title: "Dash", Slug: "dash", Version: 2, FolderUID: folderUID, FolderID: folderID}, nil)
|
||||
mockFolderService := &foldertest.FakeService{
|
||||
ExpectedFolder: &folder.Folder{ID: 1, UID: folderUID, Title: "Folder"},
|
||||
}
|
||||
|
||||
postDashboardScenario(t, "When calling POST on", "/api/dashboards", "/api/dashboards", cmd, dashboardService, nil, func(sc *scenarioContext) {
|
||||
postDashboardScenario(t, "When calling POST on", "/api/dashboards", "/api/dashboards", cmd, dashboardService, mockFolderService, func(sc *scenarioContext) {
|
||||
callPostDashboardShouldReturnSuccess(sc)
|
||||
|
||||
result := sc.ToJSON()
|
||||
@@ -664,6 +669,12 @@ func TestDashboardAPIEndpoint(t *testing.T) {
|
||||
Data: fakeDash.Data,
|
||||
}}
|
||||
mockSQLStore := dbtest.NewFakeDB()
|
||||
origNewGuardian := guardian.New
|
||||
guardian.MockDashboardGuardian(&guardian.FakeDashboardGuardian{CanSaveValue: true})
|
||||
t.Cleanup(func() {
|
||||
guardian.New = origNewGuardian
|
||||
})
|
||||
|
||||
restoreDashboardVersionScenario(t, "When calling POST on", "/api/dashboards/id/1/restore",
|
||||
"/api/dashboards/id/:dashboardId/restore", dashboardService, fakeDashboardVersionService, cmd, func(sc *scenarioContext) {
|
||||
sc.dashboardVersionService = fakeDashboardVersionService
|
||||
@@ -1084,6 +1095,9 @@ func restoreDashboardVersionScenario(t *testing.T, desc string, url string, rout
|
||||
cmd dtos.RestoreDashboardVersionCommand, fn scenarioFunc, sqlStore db.DB) {
|
||||
t.Run(fmt.Sprintf("%s %s", desc, url), func(t *testing.T) {
|
||||
cfg := setting.NewCfg()
|
||||
folderSvc := foldertest.NewFakeService()
|
||||
folderSvc.ExpectedFolder = &folder.Folder{}
|
||||
|
||||
hs := HTTPServer{
|
||||
Cfg: cfg,
|
||||
ProvisioningService: provisioning.NewProvisioningServiceMock(context.Background()),
|
||||
@@ -1097,6 +1111,7 @@ func restoreDashboardVersionScenario(t *testing.T, desc string, url string, rout
|
||||
dashboardVersionService: fakeDashboardVersionService,
|
||||
Kinds: corekind.NewBase(nil),
|
||||
accesscontrolService: actest.FakeService{},
|
||||
folderService: folderSvc,
|
||||
}
|
||||
|
||||
sc := setupScenarioContext(t, url)
|
||||
|
||||
+1
-1
@@ -224,7 +224,7 @@ func (hs *HTTPServer) MoveFolder(c *contextmodel.ReqContext) response.Response {
|
||||
cmd.SignedInUser = c.SignedInUser
|
||||
theFolder, err := hs.folderService.Move(c.Req.Context(), &cmd)
|
||||
if err != nil {
|
||||
return response.Error(http.StatusInternalServerError, "move folder failed", err)
|
||||
return response.ErrOrFallback(http.StatusInternalServerError, "move folder failed", err)
|
||||
}
|
||||
|
||||
folderDTO, err := hs.newToFolderDto(c, theFolder)
|
||||
|
||||
Reference in New Issue
Block a user