From 38995493c5e37288dfc55a2d637b18efceab7d3d Mon Sep 17 00:00:00 2001 From: Aditya Thebe Date: Thu, 3 Aug 2023 17:02:12 +0545 Subject: [PATCH] fix: config change summary from patch --- db/update.go | 7 +++++++ scrapers/runscrapers.go | 10 ++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/db/update.go b/db/update.go index ff3f6fca..034f2668 100644 --- a/db/update.go +++ b/db/update.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "strings" "time" "github.com/aws/smithy-go/ptr" @@ -316,6 +317,11 @@ func generateConfigChange(newConf, prev models.ConfigItem) (*dutyModels.ConfigCh return nil, fmt.Errorf("failed to create merge patch: %w", err) } + var patchJSON map[string]any + if err := json.Unmarshal(patch, &patchJSON); err != nil { + return nil, fmt.Errorf("failed to unmarshal patch: %w", err) + } + return &dutyModels.ConfigChange{ ConfigID: newConf.ID, ChangeType: "diff", @@ -323,6 +329,7 @@ func generateConfigChange(newConf, prev models.ConfigItem) (*dutyModels.ConfigCh ID: ulid.MustNew().AsUUID(), Diff: diff, Patches: string(patch), + Summary: strings.Join(utils.ExtractLeafNodesAndCommonParents(patchJSON), ", "), }, nil } diff --git a/scrapers/runscrapers.go b/scrapers/runscrapers.go index 9033b18b..8288fcdf 100644 --- a/scrapers/runscrapers.go +++ b/scrapers/runscrapers.go @@ -74,12 +74,18 @@ func summarizeChanges(changes []v1.ChangeResult) []v1.ChangeResult { continue } - paths := utils.ExtractLeafNodesAndCommonParents(change.AsMap()) + var patch map[string]any + if err := json.Unmarshal([]byte(change.Patches), &patch); err != nil { + logger.Errorf("failed to unmarshal patches as map[string]any: %v %v", change.Patches, err) + continue + } + + paths := utils.ExtractLeafNodesAndCommonParents(patch) if len(paths) == 0 { continue } - changes[i].Summary += strings.Join(paths, ",") + changes[i].Summary += strings.Join(paths, ", ") } return changes