Skip to content

Commit

Permalink
Use actual items and calculate attribute diffs
Browse files Browse the repository at this point in the history
Example:


<details>
<summary><img width="14" alt="updated" src="https://raw.githubusercontent.com/overmindtech/ovm-cli/ac4feb1b9dd73b5c42c5a515d12517b551d2886b/assets/changed.png"> Deployment › api-server</summary>

```diff
--- current
+++ planned
@@ -46,7 +46,6 @@
                       value: TRYMJyCxFCWU4FycHj1oztPyGyvtiv9f
                       value_from: []
                     - name: API_KEY_CLIENT_SECRET
-                      value: REDACTED
                       value_from: []
                     - name: AUTH0_AUDIENCE
                       value: https://api.overmind.tech
```
  • Loading branch information
DavidS-ovm committed Nov 16, 2023
1 parent ac4feb1 commit e7ac8c2
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 50 deletions.
134 changes: 85 additions & 49 deletions cmd/getchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ import (

"connectrpc.com/connect"
"github.com/google/uuid"
"github.com/hexops/gotextdiff"
"github.com/hexops/gotextdiff/myers"
diffspan "github.com/hexops/gotextdiff/span"
"github.com/overmindtech/ovm-cli/tracing"
"github.com/overmindtech/sdp-go"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"gopkg.in/yaml.v3"
)

//go:embed comment.md
Expand Down Expand Up @@ -158,59 +162,91 @@ func GetChange(ctx context.Context, ready chan bool) int {
BlastEdges int
Risks []TemplateRisk
}
data := TemplateData{
ChangeUrl: fmt.Sprintf("%v/changes/%v", viper.GetString("frontend"), changeUuid.String()),
ExpectedChanges: []TemplateItem{
{
StatusAlt: "updated",
StatusIcon: "https://github.com/overmindtech/ovm-cli/assets/8799341/db3e59a9-a560-4ea9-b38b-854d88ab2cd6",
Type: "Deployment",
Title: "api-server",
Diff: " Once again a diff here",
},
status := map[sdp.ItemDiffStatus]TemplateItem{
sdp.ItemDiffStatus_ITEM_DIFF_STATUS_UNSPECIFIED: {
StatusAlt: "unspecified",
StatusIcon: "",
},
sdp.ItemDiffStatus_ITEM_DIFF_STATUS_UNCHANGED: {
StatusAlt: "unchanged",
StatusIcon: "https://raw.githubusercontent.com/overmindtech/ovm-cli/ac4feb1b9dd73b5c42c5a515d12517b551d2886b/assets/item.png",
},
sdp.ItemDiffStatus_ITEM_DIFF_STATUS_CREATED: {
StatusAlt: "created",
StatusIcon: "https://raw.githubusercontent.com/overmindtech/ovm-cli/ac4feb1b9dd73b5c42c5a515d12517b551d2886b/assets/created.png",
},
sdp.ItemDiffStatus_ITEM_DIFF_STATUS_UPDATED: {
StatusAlt: "updated",
StatusIcon: "https://raw.githubusercontent.com/overmindtech/ovm-cli/ac4feb1b9dd73b5c42c5a515d12517b551d2886b/assets/changed.png",
},
UnmappedChanges: []TemplateItem{
{
StatusAlt: "created",
StatusIcon: "https://github.com/overmindtech/ovm-cli/assets/8799341/2fc6cb63-9ee1-4e15-91ea-b234a92edddb",
Type: "auth0_action",
Title: "add_to_crm",
Diff: " This should be diff with everything",
}, {
StatusAlt: "updated",
StatusIcon: "https://github.com/overmindtech/ovm-cli/assets/8799341/db3e59a9-a560-4ea9-b38b-854d88ab2cd6",
Type: "auth0_action",
Title: "create_account",
Diff: ` code: |
const { createPromiseClient } = require( "@connectrpc/connect")
const { createConnectTransport } = require( "@connectrpc/connect-node")
- const { Auth0Support,Auth0CreateUserRequest } = require('@overmindtech/sdp-js')
+ const { Auth0Support} = require('@overmindtech/sdp-js')
const ClientOAuth2 = require('client-oauth2')
const transport = createConnectTransport({
---
api.accessToken.setCustomClaim('https://api.overmind.tech/account-name', event.user.app_metadata.account_name)
// wake up all sources
- const res = await client.keepaliveSources(
+ await client.keepaliveSources(
{
account: event.user.app_metadata.account_name,
},
`,
},
sdp.ItemDiffStatus_ITEM_DIFF_STATUS_DELETED: {
StatusAlt: "deleted",
StatusIcon: "https://raw.githubusercontent.com/overmindtech/ovm-cli/ac4feb1b9dd73b5c42c5a515d12517b551d2886b/assets/deleted.png",
},
BlastItems: 75,
BlastEdges: 97,
Risks: []TemplateRisk{
{
SeverityAlt: "high",
SeverityIcon: "https://github.com/overmindtech/ovm-cli/assets/8799341/76a34fd0-7699-4636-9a4c-3cdabdf7783e",
SeverityText: "high",
Title: "Impact on Target Groups",
Description: `The various target groups including \"944651592624.eu-west-2.elbv2-target-group.k8s-default-nats-4650f3a363\", \"944651592624.eu-west-2.elbv2-target-group.k8s-default-smartloo-fd2416d9f8\", etc., that work alongside the load balancer for traffic routing may be indirectly affected if the security group change causes networking issues. This is especially important if these target groups rely on different ports other than 8080 for health checks or for directing incoming requests to backend services.`,
},
sdp.ItemDiffStatus_ITEM_DIFF_STATUS_REPLACED: {
StatusAlt: "replaced",
StatusIcon: "https://raw.githubusercontent.com/overmindtech/ovm-cli/ac4feb1b9dd73b5c42c5a515d12517b551d2886b/assets/replaced.png",
},
}
data := TemplateData{
ChangeUrl: fmt.Sprintf("%v/changes/%v", viper.GetString("frontend"), changeUuid.String()),
ExpectedChanges: []TemplateItem{},
UnmappedChanges: []TemplateItem{},
BlastItems: 75,
BlastEdges: 97,
Risks: []TemplateRisk{},
}

for _, item := range changeRes.Msg.Change.Properties.PlannedChanges {
var before, after string
if item.Before != nil {
bb, err := yaml.Marshal(item.Before.Attributes.AttrStruct.AsMap())
if err != nil {
log.WithContext(ctx).WithError(err).Error("error marshalling 'before' attributes")
before = ""
} else {
before = string(bb)
}
}
if item.After != nil {
ab, err := yaml.Marshal(item.After.Attributes.AttrStruct.AsMap())
if err != nil {
log.WithContext(ctx).WithError(err).Error("error marshalling 'after' attributes")
after = ""
} else {
after = string(ab)
}
}
edits := myers.ComputeEdits(diffspan.URIFromPath("current"), before, after)
diff := fmt.Sprint(gotextdiff.ToUnified("current", "planned", before, edits))

if item.Item != nil {
data.ExpectedChanges = append(data.ExpectedChanges, TemplateItem{
StatusAlt: status[item.Status].StatusAlt,
StatusIcon: status[item.Status].StatusIcon,
Type: item.Item.Type,
Title: item.Item.UniqueAttributeValue,
Diff: diff,
})
} else {
var typ, title string
if item.After != nil {
typ = item.After.Type
title = item.After.UniqueAttributeValue()
} else if item.Before != nil {
typ = item.Before.Type
title = item.Before.UniqueAttributeValue()
}
data.UnmappedChanges = append(data.ExpectedChanges, TemplateItem{
StatusAlt: status[item.Status].StatusAlt,
StatusIcon: status[item.Status].StatusIcon,
Type: typ,
Title: title,
Diff: diff,
})
}
}

tmpl, err := template.New("comment").Parse(commentTemplate)
if err != nil {
log.WithContext(ctx).WithError(err).Error("error parsing comment template")
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
connectrpc.com/connect v1.12.0
github.com/getsentry/sentry-go v0.25.0
github.com/google/uuid v1.4.0
github.com/hexops/gotextdiff v1.0.3
github.com/jedib0t/go-pretty/v6 v6.4.9
github.com/mattn/go-isatty v0.0.20
github.com/overmindtech/sdp-go v0.57.0
Expand All @@ -26,6 +27,7 @@ require (
go.opentelemetry.io/otel/trace v1.19.0
golang.org/x/oauth2 v0.14.0
google.golang.org/protobuf v1.31.0
gopkg.in/yaml.v3 v3.0.1
)

require (
Expand Down Expand Up @@ -76,6 +78,5 @@ require (
google.golang.org/grpc v1.58.3 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/square/go-jose.v2 v2.6.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
nhooyr.io/websocket v1.8.10 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
Expand Down

0 comments on commit e7ac8c2

Please sign in to comment.