Debian Codegraph @

sarif/run.go

raw symbols SCIP
1 package sarif
2
3 import (
4 "fmt"
5
6 "github.com/zclconf/go-cty/cty"
7 )
8
9 // RunOption ...
10 type RunOption int
11
12 // IncludeEmptyResults ...
13 const IncludeEmptyResults RunOption = iota
14
15 // Run type represents a run of a tool
16 type Run struct { // https://docs.oasis-open.org/sarif/sarif/v2.1.0/csprd01/sarif-v2.1.0-csprd01.html#_Toc10540922
17 PropertyBag
18 Tool Tool `json:"tool"`
19 Invocations []*Invocation `json:"invocations,omitempty"`
20 Artifacts []*Artifact `json:"artifacts,omitempty"`
21 Results []*Result `json:"results"`
22 Properties Properties `json:"properties,omitempty"`
23 }
24
25 // NewRun allows the creation of a new Run
26 func NewRun(toolName, informationURI string) *Run {
27 run := &Run{
28 Tool: Tool{
29 Driver: &ToolComponent{
30 Name: toolName,
31 InformationURI: &informationURI,
32 },
33 },
34 Results: []*Result{},
35 }
36
37 return run
38 }
39
40 // AddInvocation adds an invocation to the run and returns a pointer to it
41 func (run *Run) AddInvocation(executionSuccessful bool) *Invocation {
42 i := &Invocation{
43 ExecutionSuccessful: executionSuccessful,
44 }
45 run.Invocations = append(run.Invocations, i)
46 return i
47 }
48
49 // AddArtifact adds an artifact to the run and returns a pointer to it
50 func (run *Run) AddArtifact() *Artifact {
51 a := &Artifact{
52 Length: -1,
53 }
54 run.Artifacts = append(run.Artifacts, a)
55 return a
56 }
57
58 // AddDistinctArtifact will handle deduplication of simple artifact additions
59 func (run *Run) AddDistinctArtifact(uri string) *Artifact {
60 for _, artifact := range run.Artifacts {
61 if *artifact.Location.URI == uri {
62 return artifact
63 }
64 }
65
66 a := &Artifact{
67 Length: -1,
68 }
69 a.WithLocation(NewSimpleArtifactLocation(uri))
70
71 run.Artifacts = append(run.Artifacts, a)
72 return a
73 }
74
75 // AddRule returns an existing ReportingDescriptor for the ruleID or creates a new ReportingDescriptor and returns a pointer to it
76 func (run *Run) AddRule(ruleID string) *ReportingDescriptor {
77 for _, rule := range run.Tool.Driver.Rules {
78 if rule.ID == ruleID {
79 return rule
80 }
81 }
82 rule := newRule(ruleID)
83 run.Tool.Driver.Rules = append(run.Tool.Driver.Rules, rule)
84 return rule
85 }
86
87 // AddResult returns an existing Result or creates a new one and returns a pointer to it
88 func (run *Run) AddResult(ruleID string) *Result {
89 result := newRuleResult(ruleID)
90 run.Results = append(run.Results, result)
91 return result
92 }
93
94 // AttachPropertyBag ...
95 func (run *Run) AttachPropertyBag(pb *PropertyBag) {
96 run.Properties = pb.Properties
97 }
98
99 // GetRuleById finds a rule by a given rule ID and returns a pointer to it
100 func (run *Run) GetRuleById(ruleId string) (*ReportingDescriptor, error) {
101 if run.Tool.Driver != nil {
102 for _, rule := range run.Tool.Driver.Rules {
103 if rule.ID == ruleId {
104 return rule, nil
105 }
106 }
107 }
108 return nil, fmt.Errorf("couldn't find rule %s", ruleId)
109 }
110
111 // GetResultByRuleId finds the result for a ruleId and returns a pointer to it
112 func (run *Run) GetResultByRuleId(ruleId string) (*Result, error) {
113 for _, result := range run.Results {
114 if *result.RuleID == ruleId {
115 return result, nil
116 }
117 }
118 return nil, fmt.Errorf("couldn't find a result for rule %s", ruleId)
119 }
120
121 // DedupeArtifacts ...
122 func (run *Run) DedupeArtifacts() error {
123 dupes := map[*Artifact]bool{}
124 deduped := []*Artifact{}
125
126 for _, a := range run.Artifacts {
127 if _, ok := dupes[a]; !ok {
128 dupes[a] = true
129 deduped = append(deduped, a)
130 }
131 }
132 run.Artifacts = deduped
133 return nil
134 }
135
136 // AddProperty ...
137 func (run *Run) AddProperty(key string, value cty.Value) {
138 run.Properties[key] = value
139 }