Annotations: Fix EpochEnd being zero for Alert-generated annotations (#60931)

* Revert linter suggestion

* Re-add nolint

* Work in terms of pointer rather than copy

* Add tests covering validation

* Add comment
This commit is contained in:
Alexander Weaver 2023-01-04 10:16:54 -06:00 committed by GitHub
parent 0aeee7f265
commit 1381fb6dfc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 4 deletions

View File

@ -78,7 +78,10 @@ func (r *xormRepositoryImpl) AddMany(ctx context.Context, items []annotations.It
hasTags := make([]annotations.Item, 0)
hasNoTags := make([]annotations.Item, 0)
for i, item := range items {
for i := range items {
// The validation logic needs to work in terms of pointers.
// So, force everything else to work in terms of pointers too, to avoid any implicit extra copying.
item := &items[i]
tags := tag.ParseTagPairs(item.Tags)
item.Tags = tag.JoinTagPairs(tags)
item.Created = timeNow().UnixNano() / int64(time.Millisecond)
@ -86,14 +89,14 @@ func (r *xormRepositoryImpl) AddMany(ctx context.Context, items []annotations.It
if item.Epoch == 0 {
item.Epoch = item.Created
}
if err := r.validateItem(&items[i]); err != nil {
if err := r.validateItem(item); err != nil {
return err
}
if len(item.Tags) > 0 {
hasTags = append(hasTags, item)
hasTags = append(hasTags, *item)
} else {
hasNoTags = append(hasNoTags, item)
hasNoTags = append(hasNoTags, *item)
}
}

View File

@ -185,6 +185,11 @@ func TestIntegrationAnnotations(t *testing.T) {
inserted, err := repo.Get(context.Background(), query)
require.NoError(t, err)
assert.Len(t, inserted, count)
for _, ins := range inserted {
require.Equal(t, int64(12), ins.Time)
require.Equal(t, int64(12), ins.TimeEnd)
require.Equal(t, ins.Created, ins.Updated)
}
})
t.Run("Can batch-insert annotations with tags", func(t *testing.T) {