Skip to content

Commit

Permalink
fix(report): relative filepaths in report.json
Browse files Browse the repository at this point in the history
closes #3676
  • Loading branch information
rogeriopeixotocx committed Jun 16, 2021
1 parent 5816e3a commit 5e8784f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
13 changes: 13 additions & 0 deletions pkg/report/commons.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"html/template"
"os"
"path/filepath"
"strings"
"time"

Expand Down Expand Up @@ -62,3 +63,15 @@ func getPlatforms(queries model.VulnerableQuerySlice) string {
}
return strings.Join(platforms, ", ")
}

func getRelativePath(basePath, filePath string) string {
var rtn string
relativePath, err := filepath.Rel(basePath, filePath)
if err != nil {
log.Error().Msgf("Cannot make %s relative to %s", filePath, basePath)
rtn = filePath
} else {
rtn = relativePath
}
return rtn
}
25 changes: 24 additions & 1 deletion pkg/report/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"os"
"path/filepath"
"strings"

"github.com/Checkmarx/kics/pkg/model"
)

const jsonExtension = ".json"
Expand All @@ -21,10 +23,31 @@ func PrintJSONReport(path, filename string, body interface{}) error {
return err
}

var summary model.Summary
result, err := json.Marshal(body)
if err != nil {
return err
}
if err := json.Unmarshal(result, &summary); err != nil {
return err
}

basePath, err := os.Getwd()
if err != nil {
return err
}

for i := range summary.Queries {
query := summary.Queries[i]
for j := range query.Files {
query.Files[j].FileName = getRelativePath(basePath, query.Files[j].FileName)
}
}

defer closeFile(fullPath, filename, f)

encoder := json.NewEncoder(f)
encoder.SetIndent("", "\t")

return encoder.Encode(body)
return encoder.Encode(summary)
}
9 changes: 1 addition & 8 deletions pkg/report/pdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,8 @@ func createResultsTable(m pdf.Maroto, query *model.VulnerableQuery, basePath str
} else {
m.SetBackgroundColor(color.NewWhite())
}
var filePath string
relativePath, err := filepath.Rel(basePath, query.Files[idx].FileName)
if err != nil {
log.Error().Msgf("Cannot make %s relative to %s", query.Files[idx].FileName, basePath)
filePath = query.Files[idx].FileName
} else {
filePath = relativePath
}

filePath := getRelativePath(basePath, query.Files[idx].FileName)
fileLine := fmt.Sprintf("%s:%s", filePath, fmt.Sprint(query.Files[idx].Line))
m.Row(colFive, func() {
m.Col(colFullPage, func() {
Expand Down

0 comments on commit 5e8784f

Please sign in to comment.