Skip to content

Commit

Permalink
🐛 Fix nils (#3750)
Browse files Browse the repository at this point in the history
* 🐛 Fix nils

- Fixed potential nils.

Signed-off-by: naveensrinivasan <172697+naveensrinivasan@users.noreply.github.com>

* Fixed code review comments.

Signed-off-by: naveensrinivasan <172697+naveensrinivasan@users.noreply.github.com>

---------

Signed-off-by: naveensrinivasan <172697+naveensrinivasan@users.noreply.github.com>
  • Loading branch information
naveensrinivasan committed Dec 28, 2023
1 parent 6a226ce commit 2bad4e9
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
5 changes: 5 additions & 0 deletions pkg/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ type jsonScorecardV2 struct {

type jsonFloatScore float64

var errNoDoc = errors.New("doc is nil")

func (s jsonFloatScore) MarshalJSON() ([]byte, error) {
// Note: for integers, this will show as X.0.
return []byte(fmt.Sprintf("%.1f", s)), nil
Expand Down Expand Up @@ -147,6 +149,9 @@ func (r *ScorecardResult) AsJSON2(showDetails bool,
if e != nil {
return sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("GetCheck: %s: %v", checkResult.Name, e))
}
if doc == nil {
return sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("GetCheck: %s: %v", checkResult.Name, errNoDoc))
}

tmpResult := jsonCheckResultV2{
Name: checkResult.Name,
Expand Down
7 changes: 6 additions & 1 deletion pkg/sarif.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,9 @@ func computeCategory(checkName string, repos []string) (string, error) {
}
}

// createSARIFRuns takes a map of runs and returns a sorted slice of runs.
// It sorts the keys of the map, iterates over them, and appends the corresponding run to the result slice.
// If the run is nil, it is skipped.
func createSARIFRuns(runs map[string]*run) []run {
res := []run{}
// Sort keys.
Expand All @@ -561,7 +564,9 @@ func createSARIFRuns(runs map[string]*run) []run {

// Iterate over keys.
for _, k := range keys {
res = append(res, *runs[k])
if runs[k] != nil {
res = append(res, *runs[k])
}
}
return res
}
Expand Down
77 changes: 77 additions & 0 deletions pkg/sarif_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"bytes"
"fmt"
"os"
"reflect"
"testing"
"time"

Expand Down Expand Up @@ -852,3 +853,79 @@ func TestSARIFOutput(t *testing.T) {
})
}
}

func Test_createSARIFRuns(t *testing.T) {
t.Parallel()
type args struct {
runs map[string]*run
}
tests := []struct {
name string
args args
want []run
}{
{
name: "empty runs",
args: args{
runs: map[string]*run{},
},
want: []run{},
},
{
name: "nil runs are skipped",
args: args{
runs: map[string]*run{
"run1": nil,
"run2": {
Tool: tool{
Driver: driver{
Name: "name",
},
},
},
},
},
want: []run{
{
Tool: tool{
Driver: driver{
Name: "name",
},
},
},
},
},
{
name: "one run",
args: args{
runs: map[string]*run{
"run1": {
Tool: tool{
Driver: driver{
Name: "name",
},
},
},
},
},
want: []run{
{
Tool: tool{
Driver: driver{
Name: "name",
},
},
},
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if got := createSARIFRuns(tt.args.runs); !reflect.DeepEqual(got, tt.want) {
t.Errorf("createSARIFRuns() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 2bad4e9

Please sign in to comment.