From 63998b0ad27a26f11fc23577b390b6845e8e35da Mon Sep 17 00:00:00 2001 From: Ezequiel Victorero Date: Fri, 31 Mar 2023 19:13:06 -0300 Subject: [PATCH] PublicDashboards: fix annotations when Target is nil (#65744) --- .../publicdashboards/service/query.go | 14 +++-- .../publicdashboards/service/query_test.go | 59 ++++++++++++++++++- 2 files changed, 66 insertions(+), 7 deletions(-) diff --git a/pkg/services/publicdashboards/service/query.go b/pkg/services/publicdashboards/service/query.go index 5bf29ae6a17..35180debcfb 100644 --- a/pkg/services/publicdashboards/service/query.go +++ b/pkg/services/publicdashboards/service/query.go @@ -46,14 +46,16 @@ func (pd *PublicDashboardServiceImpl) FindAnnotations(ctx context.Context, reqDT OrgID: dash.OrgID, DashboardID: dash.ID, DashboardUID: dash.UID, - Limit: anno.Target.Limit, - MatchAny: anno.Target.MatchAny, SignedInUser: anonymousUser, } - if anno.Target.Type == "tags" { - annoQuery.DashboardID = 0 - annoQuery.Tags = anno.Target.Tags + if anno.Target != nil { + annoQuery.Limit = anno.Target.Limit + annoQuery.MatchAny = anno.Target.MatchAny + if anno.Target.Type == "tags" { + annoQuery.DashboardID = 0 + annoQuery.Tags = anno.Target.Tags + } } annotationItems, err := pd.AnnotationsRepo.Find(ctx, annoQuery) @@ -82,7 +84,7 @@ func (pd *PublicDashboardServiceImpl) FindAnnotations(ctx context.Context, reqDT // We want events from tag queries to overwrite existing events _, has := uniqueEvents[event.Id] - if !has || (has && anno.Target.Type == "tags") { + if !has || (has && anno.Target != nil && anno.Target.Type == "tags") { uniqueEvents[event.Id] = event } } diff --git a/pkg/services/publicdashboards/service/query_test.go b/pkg/services/publicdashboards/service/query_test.go index bf26a3c80cb..1d8ccf2e8bd 100644 --- a/pkg/services/publicdashboards/service/query_test.go +++ b/pkg/services/publicdashboards/service/query_test.go @@ -493,7 +493,7 @@ func TestGetQueryDataResponse(t *testing.T) { }) } -func TestGetAnnotations(t *testing.T) { +func TestFindAnnotations(t *testing.T) { color := "red" name := "annoName" t.Run("will build anonymous user with correct permissions to get annotations", func(t *testing.T) { @@ -820,6 +820,63 @@ func TestGetAnnotations(t *testing.T) { require.Error(t, err) require.Nil(t, items) }) + + t.Run("Test find annotations does not panics when Target in datasource is nil", func(t *testing.T) { + dash := dashboards.NewDashboard("test") + grafanaAnnotation := DashAnnotation{ + Datasource: CreateDatasource("grafana", "grafana"), + Enable: true, + Name: &name, + IconColor: &color, + Type: "dashboard", + Target: nil, + } + + annos := []DashAnnotation{grafanaAnnotation} + dashboard := AddAnnotationsToDashboard(t, dash, annos) + + annotationsRepo := annotations.FakeAnnotationsRepo{} + fakeStore := FakePublicDashboardStore{} + service := &PublicDashboardServiceImpl{ + log: log.New("test.logger"), + store: &fakeStore, + AnnotationsRepo: &annotationsRepo, + } + pubdash := &PublicDashboard{Uid: "uid1", IsEnabled: true, OrgId: 1, DashboardUid: dashboard.UID, AnnotationsEnabled: true} + + fakeStore.On("FindByAccessToken", mock.Anything, mock.AnythingOfType("string")).Return(pubdash, nil) + fakeStore.On("FindDashboard", mock.Anything, mock.Anything, mock.AnythingOfType("string")).Return(dashboard, nil) + + annotationsRepo.On("Find", mock.Anything, mock.Anything).Return([]*annotations.ItemDTO{ + { + ID: 1, + DashboardID: 1, + PanelID: 1, + Tags: []string{"tag1"}, + TimeEnd: 2, + Time: 2, + Text: "this is an annotation", + }, + }, nil).Maybe() + + items, err := service.FindAnnotations(context.Background(), AnnotationsQueryDTO{}, "abc123") + + expected := AnnotationEvent{ + Id: 1, + DashboardId: 1, + PanelId: 1, + Tags: []string{"tag1"}, + IsRegion: false, + Text: "this is an annotation", + Color: color, + Time: 2, + TimeEnd: 2, + Source: grafanaAnnotation, + } + require.NoError(t, err) + assert.Len(t, items, 1) + assert.Equal(t, expected, items[0]) + }) } func TestGetMetricRequest(t *testing.T) {