From 075d0b56dbebd93bc730b05732eceb0c197c6108 Mon Sep 17 00:00:00 2001 From: JSON Date: Wed, 27 Nov 2024 14:06:49 +0000 Subject: [PATCH] fix: make ResultProperties.Policy nilable (#64) --- internal/analysis/analysis_test.go | 14 ++++++++++++++ internal/analysis/fake.json | 9 +++++++-- sarif/sarif_types.go | 14 ++++++++------ 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/internal/analysis/analysis_test.go b/internal/analysis/analysis_test.go index 0513f8a..65811de 100644 --- a/internal/analysis/analysis_test.go +++ b/internal/analysis/analysis_test.go @@ -20,6 +20,7 @@ import ( "context" _ "embed" "fmt" + "github.com/snyk/code-client-go/sarif" "io" "net/http" "strconv" @@ -347,6 +348,19 @@ func TestAnalysis_RunAnalysis(t *testing.T) { require.NoError(t, err) assert.Equal(t, "scripts/db/migrations/20230811153738_add_generated_grouping_columns_to_collections_table.ts", actual.Sarif.Runs[0].Results[0].Locations[0].PhysicalLocation.ArtifactLocation.URI) + + t.Run("should return policy details when provided", func(t *testing.T) { + expectedPolicy := sarif.SnykPolicyV1{ + OriginalLevel: "warning", + OriginalSeverity: "critical", + Severity: "high", + } + assert.Equal(t, &expectedPolicy, actual.Sarif.Runs[0].Results[5].Properties.Policy) + }) + + t.Run("should default policy to nil when not provided", func(t *testing.T) { + assert.Nil(t, actual.Sarif.Runs[0].Results[0].Properties.Policy) + }) } func TestAnalysis_RunAnalysis_TriggerFunctionError(t *testing.T) { diff --git a/internal/analysis/fake.json b/internal/analysis/fake.json index fd01270..a163600 100644 --- a/internal/analysis/fake.json +++ b/internal/analysis/fake.json @@ -806,7 +806,12 @@ "type": "fixExamples" } ], - "isAutofixable": false + "isAutofixable": false, + "snykPolicy/v1": { + "originalLevel": "warning", + "originalSeverity": "critical", + "severity": "high" + } } } ], @@ -834,4 +839,4 @@ } } ] -} \ No newline at end of file +} diff --git a/sarif/sarif_types.go b/sarif/sarif_types.go index 0980715..50cc44c 100644 --- a/sarif/sarif_types.go +++ b/sarif/sarif_types.go @@ -89,18 +89,20 @@ type Fingerprints struct { Identity string `json:"identity"` } +type SnykPolicyV1 struct { + OriginalLevel string `json:"originalLevel"` + OriginalSeverity string `json:"originalSeverity"` + Severity string `json:"severity"` +} + type ResultProperties struct { PriorityScore int `json:"priorityScore"` PriorityScoreFactors []struct { Label bool `json:"label"` Type string `json:"type"` } `json:"priorityScoreFactors"` - IsAutofixable bool `json:"isAutofixable"` - Policy struct { - OriginalLevel string `json:"originalLevel"` - OriginalSeverity string `json:"originalSeverity"` - Severity string `json:"severity"` - } `json:"snykPolicy/v1"` + IsAutofixable bool `json:"isAutofixable"` + Policy *SnykPolicyV1 `json:"snykPolicy/v1,omitempty"` } type Result struct {