Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Render output as a markdown table for use in github comments #156

Merged
merged 6 commits into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions cmd/osv-scanner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,15 @@ func run(args []string, stdout, stderr io.Writer) int {
Usage: "sets the output format",
Value: "table",
Action: func(context *cli.Context, s string) error {
if s != "table" && s != "json" {
return fmt.Errorf("unsupported output format \"%s\" - must be either \"table\" or \"json\"", s)
switch s {
case
"table",
"json",
"markdown":
return nil
}

return nil
return fmt.Errorf("unsupported output format \"%s\" - must be one of: \"table\", \"json\", \"markdown\"", s)
},
},
&cli.BoolFlag{
Expand Down
11 changes: 11 additions & 0 deletions cmd/osv-scanner/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,17 @@ func TestRun(t *testing.T) {
Scanned %%/fixtures/locks-many/composer.lock file and found 1 packages
`,
},
// output format: markdown table
{
name: "",
args: []string{"", "--format", "markdown", "./fixtures/locks-many/composer.lock"},
wantExitCode: 0,
wantStdout: `
Scanning dir ./fixtures/locks-many/composer.lock
Scanned %%/fixtures/locks-many/composer.lock file and found 1 packages
`,
wantStderr: "",
},
}
for _, tt := range tests {
tt := tt
Expand Down
23 changes: 23 additions & 0 deletions internal/output/markdowntable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package output

import (
"io"

"github.com/google/osv-scanner/pkg/models"

"github.com/jedib0t/go-pretty/v6/table"
)

// PrintTableResults prints the osv scan results into a human friendly table.
func PrintMarkdownTableResults(vulnResult *models.VulnerabilityResults, outputWriter io.Writer) {
deftdawg marked this conversation as resolved.
Show resolved Hide resolved
outputTable := table.NewWriter()
outputTable.SetOutputMirror(outputWriter)
outputTable.AppendHeader(table.Row{"OSV URL", "Ecosystem", "Package", "Version", "Source"})

outputTable = tableBuilder(outputTable, vulnResult, false)

if outputTable.Length() == 0 {
return
}
outputTable.RenderMarkdown()
}
2 changes: 2 additions & 0 deletions internal/output/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ func (r *Reporter) PrintResult(vulnResult *models.VulnerabilityResults) error {
switch r.format {
case "json":
return PrintJSONResults(vulnResult, r.stdout)
case "markdown":
PrintMarkdownTableResults(vulnResult, r.stdout)
case "table":
PrintTableResults(vulnResult, r.stdout)
}
Expand Down
22 changes: 15 additions & 7 deletions internal/output/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,36 @@ func PrintTableResults(vulnResult *models.VulnerabilityResults, outputWriter io.
isTerminal = true
} // Otherwise use default ascii (e.g. getting piped to a file)

outputTable = tableBuilder(outputTable, vulnResult, isTerminal)

if outputTable.Length() == 0 {
return
}
outputTable.Render()
}

func tableBuilder(outputTable table.Writer, vulnResult *models.VulnerabilityResults, addStyling bool) table.Writer {
// Working directory used to simplify path
workingDir, workingDirErr := os.Getwd()
for _, sourceRes := range vulnResult.Results {
for _, pkg := range sourceRes.Packages {
workingDir, err := os.Getwd()
source := sourceRes.Source
if err == nil {
if workingDirErr == nil {
sourcePath, err := filepath.Rel(workingDir, source.Path)
if err == nil { // Simplify the path if possible
source.Path = sourcePath
}
}

// Merge groups into the same row
for _, group := range pkg.Groups {
outputRow := table.Row{}
shouldMerge := false

var links []string

for _, vuln := range group.IDs {
if isTerminal {
if addStyling {
links = append(links, osv.BaseVulnerabilityURL+text.Bold.EscapeSeq()+vuln+text.Reset.EscapeSeq())
} else {
links = append(links, osv.BaseVulnerabilityURL+vuln)
Expand All @@ -71,8 +82,5 @@ func PrintTableResults(vulnResult *models.VulnerabilityResults, outputWriter io.
}
}

if outputTable.Length() == 0 {
return
}
outputTable.Render()
return outputTable
}