Skip to content

Commit

Permalink
feat: add the occurrences field
Browse files Browse the repository at this point in the history
  • Loading branch information
nikpivkin committed Jul 11, 2023
1 parent 3b9a08c commit aa18860
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/scan/flat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down Expand Up @@ -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(),
Expand Down
34 changes: 34 additions & 0 deletions pkg/scan/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit aa18860

Please sign in to comment.