[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:
ying-jeanne 2022-12-26 22:53:52 +08:00 committed by GitHub
parent 746c7839dc
commit 12c292fd44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 63 additions and 11 deletions

View File

@ -278,6 +278,11 @@ func (hs *HTTPServer) UpdateAnnotation(c *models.ReqContext) response.Response {
EpochEnd: cmd.TimeEnd,
Text: cmd.Text,
Tags: cmd.Tags,
Data: annotation.Data,
}
if cmd.Data != nil {
item.Data = cmd.Data
}
if err := hs.annotationsRepo.Update(c.Req.Context(), &item); err != nil {
@ -328,6 +333,7 @@ func (hs *HTTPServer) PatchAnnotation(c *models.ReqContext) response.Response {
EpochEnd: annotation.TimeEnd,
Text: annotation.Text,
Tags: annotation.Tags,
Data: annotation.Data,
}
if cmd.Tags != nil {
@ -346,6 +352,10 @@ func (hs *HTTPServer) PatchAnnotation(c *models.ReqContext) response.Response {
existing.EpochEnd = cmd.TimeEnd
}
if cmd.Data != nil {
existing.Data = cmd.Data
}
if err := hs.annotationsRepo.Update(c.Req.Context(), &existing); err != nil {
return response.ErrOrFallback(500, "Failed to update annotation", err)
}

View File

@ -15,19 +15,21 @@ type PostAnnotationsCmd struct {
}
type UpdateAnnotationsCmd struct {
Id int64 `json:"id"`
Time int64 `json:"time"`
TimeEnd int64 `json:"timeEnd,omitempty"` // Optional
Text string `json:"text"`
Tags []string `json:"tags"`
Id int64 `json:"id"`
Time int64 `json:"time"`
TimeEnd int64 `json:"timeEnd,omitempty"` // Optional
Text string `json:"text"`
Tags []string `json:"tags"`
Data *simplejson.Json `json:"data"`
}
type PatchAnnotationsCmd struct {
Id int64 `json:"id"`
Time int64 `json:"time"`
TimeEnd int64 `json:"timeEnd,omitempty"` // Optional
Text string `json:"text"`
Tags []string `json:"tags"`
Id int64 `json:"id"`
Time int64 `json:"time"`
TimeEnd int64 `json:"timeEnd,omitempty"` // Optional
Text string `json:"text"`
Tags []string `json:"tags"`
Data *simplejson.Json `json:"data"`
}
type MassDeleteAnnotationsCmd struct {

View File

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

View File

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