Debian Codegraph @

v2/sarif/sarif.go

raw
1 package sarif
2
3 import (
4 "encoding/json"
5 "fmt"
6 "io"
7 "os"
8 )
9
10 // Version is the version of Sarif to use
11 type Version string
12
13 // Version210 represents Version210 of Sarif
14 const (
15 Version210 Version = "2.1.0"
16 // @Deprecated - use Version210 instead
17 Version210RTM5 Version = "2.1.0-rtm.5"
18 )
19
20 var versions = map[Version]string{
21 Version210: "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/main/sarif-2.1/schema/sarif-schema-2.1.0.json",
22 // keeping this for backwards support but marked as deprecated
23 Version210RTM5: "https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/schemas/sarif-schema-2.1.0.json",
24 }
25
26 // Report is the encapsulating type representing a Sarif Report
27 type Report struct {
28 PropertyBag
29 InlineExternalProperties []*ExternalProperties `json:"inlineExternalProperties,omitempty"`
30 Version string `json:"version"`
31 Schema string `json:"$schema,omitempty"`
32 Runs []*Run `json:"runs"`
33 }
34
35 // New Creates a new Report or returns an error
36 func New(version Version, includeSchema ...bool) (*Report, error) {
37 schema := ""
38
39 if len(includeSchema) == 0 || includeSchema[0] {
40 var err error
41
42 schema, err = getVersionSchema(version)
43 if err != nil {
44 return nil, err
45 }
46 }
47 return &Report{
48 Version: string(version),
49 Schema: schema,
50 Runs: []*Run{},
51 }, nil
52 }
53
54 // Open loads a Report from a file
55 func Open(filename string) (*Report, error) {
56 if _, err := os.Stat(filename); err != nil && os.IsNotExist(err) {
57 return nil, fmt.Errorf("the provided file path doesn't have a file")
58 }
59
60 content, err := os.ReadFile(filename)
61 if err != nil {
62 return nil, fmt.Errorf("the provided filepath could not be opened. %w", err)
63 }
64 return FromBytes(content)
65 }
66
67 // FromString loads a Report from string content
68 func FromString(content string) (*Report, error) {
69 return FromBytes([]byte(content))
70 }
71
72 // FromBytes loads a Report from a byte array
73 func FromBytes(content []byte) (*Report, error) {
74 var report Report
75 if err := json.Unmarshal(content, &report); err != nil {
76 return nil, err
77 }
78 return &report, nil
79 }
80
81 // AddRun allows adding run information to the current report
82 func (sarif *Report) AddRun(run *Run) {
83 sarif.Runs = append(sarif.Runs, run)
84 }
85
86 func getVersionSchema(version Version) (string, error) {
87 for ver, schema := range versions {
88 if ver == version {
89 return schema, nil
90 }
91 }
92 return "", fmt.Errorf("version [%s] is not supported", version)
93 }
94
95 // WriteFile will write the report to a file using a pretty formatter
96 func (sarif *Report) WriteFile(filename string) error {
97 file, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, 0644)
98 if err != nil {
99 return err
100 }
101 defer func() { _ = file.Close() }()
102 return sarif.PrettyWrite(file)
103 }
104
105 // Write writes the JSON as a string with no formatting
106 func (sarif *Report) Write(w io.Writer) error {
107 for _, run := range sarif.Runs {
108 run.DedupeArtifacts()
109 }
110 marshal, err := json.Marshal(sarif)
111 if err != nil {
112 return err
113 }
114 _, err = w.Write(marshal)
115 return err
116 }
117
118 // PrettyWrite writes the JSON output with indentation
119 func (sarif *Report) PrettyWrite(w io.Writer) error {
120 marshal, err := json.MarshalIndent(sarif, "", " ")
121 if err != nil {
122 return err
123 }
124 _, err = w.Write(marshal)
125 return err
126 }