From 0cf973b6f2375a1a25b1125e8ad22f3d4c558556 Mon Sep 17 00:00:00 2001 From: Aloento <11802769+Aloento@users.noreply.github.com> Date: Tue, 1 Dec 2026 11:37:21 +0100 Subject: [PATCH] [Fix] Re-read incident components before moving a component --- internal/api/v2/v2.go | 33 ++++++++- internal/api/v2/v2_component_test.go | 105 +++++++++++++++++++++++++++ internal/db/db.go | 23 ++++++ 3 files changed, 159 insertions(+), 2 deletions(-) diff --git a/internal/api/v2/v2.go b/internal/api/v2/v2.go index ac8eaa5..051904b 100644 --- a/internal/api/v2/v2.go +++ b/internal/api/v2/v2.go @@ -874,6 +874,15 @@ func shouldSkipIncident(inc *db.Incident) bool { return inc.Type == event.TypeInformation || inc.Type == event.TypeMaintenance } +// incidentStore is the subset of the database facade needed to move a +// component between incidents; tests replace it with a stub. +type incidentStore interface { + GetIncidentComponents(ctx context.Context, incidentID int) ([]db.Component, error) + MoveComponentFromOldToAnotherIncident( + ctx context.Context, comp *db.Component, incOld, incNew *db.Incident, closeOld bool, + ) (*db.Incident, error) +} + // tryMoveComponentIfFound attempts to move a component if it's found in the given incident. func tryMoveComponentIfFound( ctx context.Context, @@ -883,13 +892,33 @@ func tryMoveComponentIfFound( inc *db.Incident, incIn *db.Incident, compResult *ProcessComponentResp, +) (bool, error) { + return moveComponentIfFound(ctx, dbInst, log, comp, inc, incIn, compResult) +} + +func moveComponentIfFound( + ctx context.Context, + store incidentStore, + log *zap.Logger, + comp *db.Component, + inc *db.Incident, + incIn *db.Incident, + compResult *ProcessComponentResp, ) (bool, error) { for _, incComp := range inc.Components { if comp.ID == incComp.ID { log.Info("found the component in the opened incident", zap.Any("component", comp), zap.Any("incident", inc)) - closeInc := len(inc.Components) == 1 - incident, err := dbInst.MoveComponentFromOldToAnotherIncident(ctx, comp, inc, incIn, closeInc) + // inc.Components is the snapshot fetched before this request created + // its incident; reload so the close decision reflects the current + // component list. + components, err := store.GetIncidentComponents(ctx, int(inc.ID)) + if err != nil { + return false, err + } + + closeInc := len(components) == 1 + incident, err := store.MoveComponentFromOldToAnotherIncident(ctx, comp, inc, incIn, closeInc) if err != nil { return false, err } diff --git a/internal/api/v2/v2_component_test.go b/internal/api/v2/v2_component_test.go index f57a3cf..75b30c7 100644 --- a/internal/api/v2/v2_component_test.go +++ b/internal/api/v2/v2_component_test.go @@ -1,11 +1,14 @@ package v2 import ( + "context" "encoding/json" + "errors" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "go.uber.org/zap" "github.com/stackmon/otc-status-dashboard/internal/db" ) @@ -47,3 +50,105 @@ func TestToAPIComponentMapsAttributes(t *testing.T) { {Name: "type", Value: "cce"}, }, comp.Attributes) } + +type stubIncidentStore struct { + components []db.Component + getErr error + moveErr error + closeOld bool + moves int +} + +func (s *stubIncidentStore) GetIncidentComponents(context.Context, int) ([]db.Component, error) { + if s.getErr != nil { + return nil, s.getErr + } + return s.components, nil +} + +func (s *stubIncidentStore) MoveComponentFromOldToAnotherIncident( + _ context.Context, _ *db.Component, _, incNew *db.Incident, closeOld bool, +) (*db.Incident, error) { + s.moves++ + s.closeOld = closeOld + if s.moveErr != nil { + return nil, s.moveErr + } + return incNew, nil +} + +// The close decision must come from the component list re-read before the move, +// not from the opened-incident snapshot this request started with. +func TestMoveComponentIfFoundReloadsComponents(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + snapshot []db.Component + fresh []db.Component + wantClose bool + }{ + { + name: "stale single component now has a peer", + snapshot: []db.Component{{ID: 7}}, + fresh: []db.Component{{ID: 7}, {ID: 8}}, + wantClose: false, + }, + { + name: "component is the last one", + snapshot: []db.Component{{ID: 7}, {ID: 8}}, + fresh: []db.Component{{ID: 7}}, + wantClose: true, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + + store := &stubIncidentStore{components: tc.fresh} + inc := &db.Incident{ID: 10, Components: tc.snapshot} + result := &ProcessComponentResp{ComponentID: 7} + + moved, err := moveComponentIfFound( + context.Background(), store, zap.NewNop(), &db.Component{ID: 7}, inc, &db.Incident{ID: 20}, result, + ) + require.NoError(t, err) + assert.True(t, moved) + require.Equal(t, 1, store.moves) + assert.Equal(t, tc.wantClose, store.closeOld) + assert.Equal(t, 20, result.IncidentID) + }) + } +} + +func TestMoveComponentIfFoundSkipsUnknownComponent(t *testing.T) { + t.Parallel() + + store := &stubIncidentStore{components: []db.Component{{ID: 7}}} + inc := &db.Incident{ID: 10, Components: []db.Component{{ID: 7}}} + + moved, err := moveComponentIfFound( + context.Background(), store, zap.NewNop(), &db.Component{ID: 8}, inc, &db.Incident{ID: 20}, + &ProcessComponentResp{ComponentID: 8}, + ) + require.NoError(t, err) + assert.False(t, moved) + assert.Zero(t, store.moves) +} + +func TestMoveComponentIfFoundStopsOnReloadError(t *testing.T) { + t.Parallel() + + reloadErr := errors.New("reload failed") + store := &stubIncidentStore{getErr: reloadErr} + inc := &db.Incident{ID: 10, Components: []db.Component{{ID: 7}}} + + moved, err := moveComponentIfFound( + context.Background(), store, zap.NewNop(), &db.Component{ID: 7}, inc, &db.Incident{ID: 20}, + &ProcessComponentResp{ComponentID: 7}, + ) + require.ErrorIs(t, err, reloadErr) + assert.False(t, moved) + assert.Zero(t, store.moves) +} diff --git a/internal/db/db.go b/internal/db/db.go index 12d2ceb..dbe52b6 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -655,6 +655,29 @@ func (db *DB) GetComponent(ctx context.Context, id int) (*Component, error) { return &comp, nil } +// GetIncidentComponents returns the components currently linked to the incident. +func (db *DB) GetIncidentComponents(ctx context.Context, incidentID int) ([]Component, error) { + e, err := db.e.Incident.Query(). + Where(incident.IDEQ(incidentID)). + WithComponents(func(q *ent.ComponentQuery) { + q.Select(component.FieldID, component.FieldName) + }). + First(ctx) + if err != nil { + if ent.IsNotFound(err) { + return nil, ErrDBIncidentDSNotExist + } + return nil, err + } + + components := make([]Component, 0, len(e.Edges.Components)) + for _, c := range e.Edges.Components { + components = append(components, componentFromEnt(c)) + } + + return components, nil +} + func (db *DB) GetComponentsAsMap(ctx context.Context) (map[int]*Component, error) { rows, err := db.e.Component.Query().All(ctx) if err != nil {