From 7940e5ef86cdd8d88498eea767c7cbff03427f75 Mon Sep 17 00:00:00 2001 From: Jay Mundrawala Date: Wed, 11 Dec 2024 15:32:54 -0600 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Use=20100=20for=20the=20default?= =?UTF-8?q?=20value=20of=20impact=20(#1514)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It's possible for impact to be be nil, in which case we should use the default value which is 100. Using the score value doesn't make sense. It would mean that the category would change based on if it passed or failed in cases where impact was not defined. --- policy/score_calculator.go | 4 ++-- policy/score_calculator_test.go | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/policy/score_calculator.go b/policy/score_calculator.go index afaa325f..27f3eb03 100644 --- a/policy/score_calculator.go +++ b/policy/score_calculator.go @@ -500,8 +500,8 @@ func (c *bandedScoreCalculator) Add(score *Score, impact *explorer.Impact) { c.scoreCompletion += score.ScoreCompletion if score.ScoreCompletion != 0 && score.Weight != 0 { - category := score.Value - if impact != nil { + category := uint32(0) + if impact != nil && impact.Value != nil { category = 100 - uint32(impact.Value.Value) } diff --git a/policy/score_calculator_test.go b/policy/score_calculator_test.go index 4238b6f1..f1558220 100644 --- a/policy/score_calculator_test.go +++ b/policy/score_calculator_test.go @@ -276,6 +276,23 @@ func TestBandedScores(t *testing.T) { }, out: &Score{Value: 9, ScoreCompletion: 100, DataCompletion: 66, Weight: 20, Type: ScoreType_Result}, }, + { + in: []*Score{ + // 10 critical checks (9ok, 1not) + {Value: 0, ScoreCompletion: 100, DataCompletion: 80, DataTotal: 5, Weight: 1, Type: ScoreType_Result}, + {Value: 100, ScoreCompletion: 100, DataCompletion: 100, DataTotal: 1, Weight: 9, Type: ScoreType_Result}, + // 10 high checks (ok) + {Value: 100, ScoreCompletion: 100, DataCompletion: 33, DataTotal: 3, Weight: 10, Type: ScoreType_Result}, + }, + impacts: []*explorer.Impact{ + // 10 critical checks + {Value: &explorer.ImpactValue{Value: 100}}, + {Value: nil}, + // 10 high checks + {Value: &explorer.ImpactValue{Value: 80}}, + }, + out: &Score{Value: 45, ScoreCompletion: 100, DataCompletion: 66, Weight: 20, Type: ScoreType_Result}, + }, }) }