PublicDashboards: fix annotations when Target is nil (#65744)

This commit is contained in:
Ezequiel Victorero
2023-03-31 19:13:06 -03:00
committed by GitHub
parent aba91d3053
commit 63998b0ad2
2 changed files with 66 additions and 7 deletions

View File

@@ -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
}
}

View File

@@ -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) {