Skip to content

Commit f3f08f3

Browse files
authored
Merge pull request #525 from overmindtech/risk-levels
Add `get-change --risk-levels` flag to filter risks that get displayed
2 parents 5769487 + 2e04b60 commit f3f08f3

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

cmd/changes_get_change.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"os"
8+
"slices"
89
"strings"
910
"text/template"
1011
"time"
@@ -42,6 +43,26 @@ const assetVersion = "17c7fd2c365d4f4cdd8e414ca5148f825fa4febd"
4243
func GetChange(cmd *cobra.Command, args []string) error {
4344
ctx := cmd.Context()
4445

46+
riskLevels := []sdp.Risk_Severity{}
47+
for _, level := range viper.GetStringSlice("risk-levels") {
48+
switch level {
49+
case "high":
50+
riskLevels = append(riskLevels, sdp.Risk_SEVERITY_HIGH)
51+
case "medium":
52+
riskLevels = append(riskLevels, sdp.Risk_SEVERITY_MEDIUM)
53+
case "low":
54+
riskLevels = append(riskLevels, sdp.Risk_SEVERITY_LOW)
55+
default:
56+
return flagError{fmt.Sprintf("invalid --risk-levels value '%v', allowed values are 'high', 'medium', 'low'", level)}
57+
}
58+
}
59+
slices.Sort(riskLevels)
60+
riskLevels = slices.Compact(riskLevels)
61+
62+
if len(riskLevels) == 0 {
63+
riskLevels = []sdp.Risk_Severity{sdp.Risk_SEVERITY_HIGH, sdp.Risk_SEVERITY_MEDIUM, sdp.Risk_SEVERITY_LOW}
64+
}
65+
4566
ctx, oi, _, err := login(ctx, cmd, []string{"changes:read"})
4667
if err != nil {
4768
return err
@@ -133,6 +154,19 @@ fetch:
133154
"change-description": changeRes.Msg.GetChange().GetProperties().GetDescription(),
134155
}).Info("found change")
135156

157+
// in parsing the risks, we have ensured that there is only unique values in
158+
// `riskLevels`, so if there are 3 values, then we don't need to filter
159+
if len(riskLevels) != 3 {
160+
log.WithContext(ctx).WithFields(log.Fields{
161+
"risk-levels": renderRiskFilter(riskLevels),
162+
}).Info("filtering risks")
163+
164+
md := riskRes.Msg.GetChangeRiskMetadata()
165+
if md != nil {
166+
md.Risks = filterRisks(md.GetRisks(), riskLevels)
167+
}
168+
}
169+
136170
switch viper.GetString("format") {
137171
case "json":
138172
jsonStruct := struct {
@@ -142,6 +176,7 @@ fetch:
142176
Change: changeRes.Msg.GetChange(),
143177
RiskMetadata: riskRes.Msg.GetChangeRiskMetadata(),
144178
}
179+
145180
b, err := json.MarshalIndent(jsonStruct, "", " ")
146181
if err != nil {
147182
lf["input"] = fmt.Sprintf("%#v", jsonStruct)
@@ -320,6 +355,35 @@ fetch:
320355
return nil
321356
}
322357

358+
func filterRisks(risks []*sdp.Risk, levels []sdp.Risk_Severity) []*sdp.Risk {
359+
filteredRisks := make([]*sdp.Risk, 0)
360+
361+
for _, risk := range risks {
362+
if slices.Contains(levels, risk.GetSeverity()) {
363+
filteredRisks = append(filteredRisks, risk)
364+
}
365+
}
366+
367+
return filteredRisks
368+
}
369+
370+
func renderRiskFilter(levels []sdp.Risk_Severity) string {
371+
result := make([]string, 0, len(levels))
372+
for _, level := range levels {
373+
switch level {
374+
case sdp.Risk_SEVERITY_HIGH:
375+
result = append(result, "high")
376+
case sdp.Risk_SEVERITY_MEDIUM:
377+
result = append(result, "medium")
378+
case sdp.Risk_SEVERITY_LOW:
379+
result = append(result, "low")
380+
case sdp.Risk_SEVERITY_UNSPECIFIED:
381+
continue
382+
}
383+
}
384+
return strings.Join(result, ", ")
385+
}
386+
323387
func init() {
324388
changesCmd.AddCommand(getChangeCmd)
325389

@@ -328,4 +392,5 @@ func init() {
328392

329393
getChangeCmd.PersistentFlags().String("frontend", "https://app.overmind.tech/", "The frontend base URL")
330394
getChangeCmd.PersistentFlags().String("format", "json", "How to render the change. Possible values: json, markdown")
395+
getChangeCmd.PersistentFlags().StringSlice("risk-levels", []string{"high", "medium", "low"}, "Only show changes with the specified risk levels. Allowed values: high, medium, low")
331396
}

0 commit comments

Comments
 (0)