diff --git a/pkg/services/ngalert/schedule/compat.go b/pkg/services/ngalert/schedule/compat.go index 14c87ae663a..18ac062c176 100644 --- a/pkg/services/ngalert/schedule/compat.go +++ b/pkg/services/ngalert/schedule/compat.go @@ -50,7 +50,10 @@ func stateToPostableAlert(alertState *state.State, appURL *url.URL) *models.Post } if alertState.Image != nil { - nA[alertingModels.ImageTokenAnnotation] = generateImageURI(alertState.Image) + imageURI := generateImageURI(alertState.Image) + if imageURI != "" { + nA[alertingModels.ImageTokenAnnotation] = imageURI + } } if alertState.StateReason != "" { @@ -175,5 +178,9 @@ func generateImageURI(image *ngModels.Image) string { if image.URL != "" { return image.URL } - return "token://" + image.Token + if image.Token != "" { + return "token://" + image.Token + } + + return "" } diff --git a/pkg/services/ngalert/schedule/compat_test.go b/pkg/services/ngalert/schedule/compat_test.go index 7e61fd3d055..f36e7b3e5e6 100644 --- a/pkg/services/ngalert/schedule/compat_test.go +++ b/pkg/services/ngalert/schedule/compat_test.go @@ -134,6 +134,21 @@ func Test_stateToPostableAlert(t *testing.T) { require.Equal(t, expected, result.Annotations) }) + + t.Run("don't add __alertImageToken__ if there's no image token", func(t *testing.T) { + alertState := randomState(tc.state) + alertState.Annotations = randomMapOfStrings() + alertState.Image = &ngModels.Image{} + + result := stateToPostableAlert(alertState, appURL) + + expected := make(models.LabelSet, len(alertState.Annotations)+1) + for k, v := range alertState.Annotations { + expected[k] = v + } + + require.Equal(t, expected, result.Annotations) + }) }) t.Run("should add state reason annotation if not empty", func(t *testing.T) {