mirror of
https://github.com/grafana/grafana.git
synced 2026-08-12 06:05:02 -05:00
[Bug] Fix annotations update/patch (#60385)
* fix annotations update/patch * verify that when update doesn't contain data, we will not update data
This commit is contained in:
@@ -162,6 +162,10 @@ func (r *xormRepositoryImpl) Update(ctx context.Context, item *annotations.Item)
|
||||
existing.EpochEnd = item.EpochEnd
|
||||
}
|
||||
|
||||
if item.Data != nil {
|
||||
existing.Data = item.Data
|
||||
}
|
||||
|
||||
if item.Tags != nil {
|
||||
tags, err := r.tagService.EnsureTagsExist(ctx, tag.ParseTagPairs(item.Tags))
|
||||
if err != nil {
|
||||
@@ -183,7 +187,7 @@ func (r *xormRepositoryImpl) Update(ctx context.Context, item *annotations.Item)
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = sess.Table("annotation").ID(existing.Id).Cols("epoch", "text", "epoch_end", "updated", "tags").Update(existing)
|
||||
_, err = sess.Table("annotation").ID(existing.Id).Cols("epoch", "text", "epoch_end", "updated", "tags", "data").Update(existing)
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
@@ -90,6 +90,7 @@ func TestIntegrationAnnotations(t *testing.T) {
|
||||
Type: "alert",
|
||||
Epoch: 10,
|
||||
Tags: []string{"outage", "error", "type:outage", "server:server-1"},
|
||||
Data: simplejson.NewFromAny(map[string]interface{}{"data1": "I am a cool data", "data2": "I am another cool data"}),
|
||||
}
|
||||
err = repo.Add(context.Background(), annotation)
|
||||
require.NoError(t, err)
|
||||
@@ -321,6 +322,9 @@ func TestIntegrationAnnotations(t *testing.T) {
|
||||
assert.Equal(t, annotationId, items[0].Id)
|
||||
assert.Empty(t, items[0].Tags)
|
||||
assert.Equal(t, "something new", items[0].Text)
|
||||
data, err := items[0].Data.Map()
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, data, map[string]interface{}{"data1": "I am a cool data", "data2": "I am another cool data"})
|
||||
})
|
||||
|
||||
t.Run("Can update annotation with new tags", func(t *testing.T) {
|
||||
@@ -352,6 +356,38 @@ func TestIntegrationAnnotations(t *testing.T) {
|
||||
assert.Greater(t, items[0].Updated, items[0].Created)
|
||||
})
|
||||
|
||||
t.Run("Can update annotations with data", func(t *testing.T) {
|
||||
query := &annotations.ItemQuery{
|
||||
OrgId: 1,
|
||||
DashboardId: 1,
|
||||
From: 0,
|
||||
To: 15,
|
||||
SignedInUser: testUser,
|
||||
}
|
||||
items, err := repo.Get(context.Background(), query)
|
||||
require.NoError(t, err)
|
||||
|
||||
annotationId := items[0].Id
|
||||
data := simplejson.NewFromAny(map[string]interface{}{"data": "I am a data", "data2": "I am also a data"})
|
||||
err = repo.Update(context.Background(), &annotations.Item{
|
||||
Id: annotationId,
|
||||
OrgId: 1,
|
||||
Text: "something new",
|
||||
Tags: []string{"newtag1", "newtag2"},
|
||||
Data: data,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
items, err = repo.Get(context.Background(), query)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, annotationId, items[0].Id)
|
||||
assert.Equal(t, []string{"newtag1", "newtag2"}, items[0].Tags)
|
||||
assert.Equal(t, "something new", items[0].Text)
|
||||
assert.Greater(t, items[0].Updated, items[0].Created)
|
||||
assert.Equal(t, data, items[0].Data)
|
||||
})
|
||||
|
||||
t.Run("Can delete annotation", func(t *testing.T) {
|
||||
query := &annotations.ItemQuery{
|
||||
OrgId: 1,
|
||||
|
||||
Reference in New Issue
Block a user