diff --git a/pkg/scan/flat.go b/pkg/scan/flat.go index 5e7b9d566..00c075ac2 100755 --- a/pkg/scan/flat.go +++ b/pkg/scan/flat.go @@ -20,6 +20,7 @@ type FlatResult struct { Warning bool `json:"warning"` Status Status `json:"status"` Resource string `json:"resource"` + Occurrences []Occurrence `json:"occurrences,omitempty"` Location FlatRange `json:"location"` } @@ -60,6 +61,7 @@ func (r *Result) Flatten() FlatResult { Severity: r.rule.Severity, Status: r.status, Resource: resMetadata.Reference(), + Occurrences: r.Occurrences(), Warning: r.IsWarning(), Location: FlatRange{ Filename: rng.GetFilename(), diff --git a/pkg/scan/result.go b/pkg/scan/result.go index 0caf9bd4b..2e112bba7 100755 --- a/pkg/scan/result.go +++ b/pkg/scan/result.go @@ -336,3 +336,37 @@ func rawToString(raw interface{}) string { return "?" } } + +type Occurrence struct { + Resource string `json:"resource"` + Filename string `json:"filename"` + StartLine int `json:"start_line"` + EndLine int `json:"end_line"` +} + +func (r *Result) Occurrences() []Occurrence { + var occurrences []Occurrence + + mod := &r.metadata + prevFileName := mod.Range().GetFilename() + + for { + mod = mod.Parent() + if mod == nil { + break + } + parentRange := mod.Range() + fileName := parentRange.GetFilename() + if fileName == prevFileName { + continue + } + prevFileName = fileName + occurrences = append(occurrences, Occurrence{ + Resource: mod.Reference(), + Filename: parentRange.GetFilename(), + StartLine: parentRange.GetStartLine(), + EndLine: parentRange.GetEndLine(), + }) + } + return occurrences +}