|
| 1 | +package v2 |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/google/uuid" |
| 10 | + "github.com/interlynk-io/sbomqs/pkg/sbom" |
| 11 | + "sigs.k8s.io/release-utils/version" |
| 12 | +) |
| 13 | + |
| 14 | +type score struct { |
| 15 | + Category string `json:"category"` |
| 16 | + Feature string `json:"feature"` |
| 17 | + Score float64 `json:"score"` |
| 18 | + Desc string `json:"description"` |
| 19 | + Ignored bool `json:"ignored"` |
| 20 | +} |
| 21 | + |
| 22 | +type file struct { |
| 23 | + Name string `json:"file_name"` |
| 24 | + Spec string `json:"spec"` |
| 25 | + SpecVersion string `json:"spec_version"` |
| 26 | + Format string `json:"file_format"` |
| 27 | + Grade string `json:"grade"` |
| 28 | + InterlynkScore float64 `json:"interlynk_score"` |
| 29 | + Components int `json:"num_components"` |
| 30 | + CreationTime string `json:"creation_time"` |
| 31 | + Scores []*score `json:"scores"` |
| 32 | +} |
| 33 | + |
| 34 | +type creation struct { |
| 35 | + Name string `json:"name"` |
| 36 | + Version string `json:"version"` |
| 37 | + ScoringEngine string `json:"scoring_engine_version"` |
| 38 | + Vendor string `json:"vendor"` |
| 39 | +} |
| 40 | + |
| 41 | +type jsonReport struct { |
| 42 | + RunID string `json:"run_id"` |
| 43 | + TimeStamp string `json:"timestamp"` |
| 44 | + CreationInfo creation `json:"creation_info"` |
| 45 | + Files []file `json:"files"` |
| 46 | +} |
| 47 | + |
| 48 | +func newJSONReport() *jsonReport { |
| 49 | + return &jsonReport{ |
| 50 | + RunID: uuid.New().String(), |
| 51 | + TimeStamp: time.Now().UTC().Format(time.RFC3339), |
| 52 | + CreationInfo: creation{ |
| 53 | + Name: "sbomqs", |
| 54 | + Version: version.GetVersionInfo().GitVersion, |
| 55 | + // ScoringEngine: scorer.EngineVersion, |
| 56 | + Vendor: "Interlynk (support@interlynk.io)", |
| 57 | + }, |
| 58 | + Files: []file{}, |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func (r *Reporter) jsonReport() (string, error) { |
| 63 | + fmt.Println("JSON SCORE") |
| 64 | + jr := newJSONReport() |
| 65 | + |
| 66 | + for _, r := range r.Results { |
| 67 | + f := file{} |
| 68 | + f.InterlynkScore = r.InterlynkScore |
| 69 | + f.Grade = r.Grade |
| 70 | + f.Components = r.Meta.NumComponents |
| 71 | + f.Format = r.Meta.FileFormat |
| 72 | + f.Name = r.Meta.Filename |
| 73 | + f.Spec = r.Meta.Spec |
| 74 | + f.CreationTime = r.Meta.CreationTime |
| 75 | + |
| 76 | + if r.Meta.Spec == string(sbom.SBOMSpecSPDX) { |
| 77 | + version := strings.Replace(r.Meta.SpecVersion, "SPDX-", "", 1) |
| 78 | + f.SpecVersion = version |
| 79 | + } |
| 80 | + |
| 81 | + for _, cat := range r.Comprehensive.Categories { |
| 82 | + for _, feat := range cat.Features { |
| 83 | + ns := new(score) |
| 84 | + ns.Category = cat.Name |
| 85 | + ns.Feature = feat.Key |
| 86 | + ns.Score = feat.Score |
| 87 | + ns.Desc = feat.Desc |
| 88 | + ns.Ignored = feat.Ignored |
| 89 | + f.Scores = append(f.Scores, ns) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + jr.Files = append(jr.Files, f) |
| 94 | + } |
| 95 | + |
| 96 | + o, err := json.MarshalIndent(jr, "", " ") |
| 97 | + if err != nil { |
| 98 | + return "", err |
| 99 | + } |
| 100 | + |
| 101 | + if true { |
| 102 | + fmt.Println(string(o)) |
| 103 | + } |
| 104 | + |
| 105 | + return string(o), nil |
| 106 | +} |
0 commit comments