forked from Checkmarx/kics
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HTML Report Checkmarx#2125 (Checkmarx#2414)
- Loading branch information
1 parent
049b2d4
commit fc25956
Showing
17 changed files
with
618 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package report | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"html/template" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
"strings" | ||
|
||
"github.com/rs/zerolog/log" | ||
"github.com/tdewolff/minify/v2" | ||
minifyCSS "github.com/tdewolff/minify/v2/css" | ||
minifyHtml "github.com/tdewolff/minify/v2/html" | ||
) | ||
|
||
const ( | ||
templateFile = "report.tmpl" | ||
) | ||
|
||
var templatePath = "" | ||
|
||
func includeSVG(name string) template.HTML { | ||
svg, err := os.ReadFile(filepath.Join(templatePath, name)) | ||
if err != nil { | ||
log.Err(err).Msgf("failed to open svg: %s", name) | ||
return "" | ||
} | ||
return template.HTML(string(svg)) //nolint | ||
} | ||
|
||
func includeCSS(name string) template.HTML { | ||
css, err := os.ReadFile(filepath.Join(templatePath, name)) | ||
if err != nil { | ||
log.Err(err).Msgf("failed to open svg: %s", name) | ||
return "" | ||
} | ||
minifier := minify.New() | ||
minifier.AddFunc("text/css", minifyCSS.Minify) | ||
cssMinified, err := minifier.Bytes("text/css", css) | ||
if err != nil { | ||
return "" | ||
} | ||
return template.HTML("<style>" + string(cssMinified) + "</style>") //nolint | ||
} | ||
|
||
// PrintHTMLReport creates a report file on HTML format | ||
func PrintHTMLReport(path, filename string, body interface{}) error { | ||
if !strings.HasSuffix(filename, ".html") { | ||
filename += ".html" | ||
} | ||
|
||
_, templatePathFromStack, _, ok := runtime.Caller(0) | ||
if !ok { | ||
return fmt.Errorf("report error: Report template not found") | ||
} | ||
templatePath = templatePathFromStack | ||
templateFuncs["includeSVG"] = includeSVG | ||
templateFuncs["includeCSS"] = includeCSS | ||
|
||
fullPath := filepath.Join(path, filename) | ||
templatePath = filepath.Join(filepath.Dir(templatePath), "template", "html") | ||
t := template.Must(template.New(templateFile).Funcs(templateFuncs).ParseFiles(filepath.Join(templatePath, templateFile))) | ||
|
||
_ = os.MkdirAll(path, os.ModePerm) | ||
f, err := os.OpenFile(filepath.Clean(fullPath), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.ModePerm) | ||
if err != nil { | ||
return err | ||
} | ||
defer closeFile(fullPath, filename, f) | ||
var buffer bytes.Buffer | ||
|
||
err = t.Execute(&buffer, body) | ||
if err != nil { | ||
return err | ||
} | ||
minifier := minify.New() | ||
minifier.AddFunc("text/html", minifyHtml.Minify) | ||
minifier.Add("text/html", &minifyHtml.Minifier{ | ||
KeepDocumentTags: true, | ||
KeepEndTags: true, | ||
KeepQuotes: true, | ||
}) | ||
|
||
minifierWriter := minifier.Writer("text/html", f) | ||
defer minifierWriter.Close() | ||
|
||
_, err = minifierWriter.Write(buffer.Bytes()) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package report | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/Checkmarx/kics/pkg/model" | ||
"github.com/Checkmarx/kics/test" | ||
"github.com/stretchr/testify/require" | ||
"golang.org/x/net/html" | ||
) | ||
|
||
var htmlTests = []struct { | ||
caseTest jsonCaseTest | ||
expectedResult model.Summary | ||
}{ | ||
{ | ||
caseTest: jsonCaseTest{ | ||
summary: test.SummaryMock, | ||
path: "./testdir", | ||
filename: "testout", | ||
}, | ||
expectedResult: test.SummaryMock, | ||
}, | ||
} | ||
|
||
// TestPrintHTMLReport tests the functions [PrintHTMLReport()] and all the methods called by them | ||
func TestPrintHTMLReport(t *testing.T) { | ||
for idx, test := range htmlTests { | ||
t.Run(fmt.Sprintf("HTML File test case %d", idx), func(t *testing.T) { | ||
err := PrintHTMLReport(test.caseTest.path, test.caseTest.filename, test.caseTest.summary) | ||
require.NoError(t, err) | ||
require.FileExists(t, filepath.Join(test.caseTest.path, test.caseTest.filename+".html")) | ||
htmlString, err := os.ReadFile(filepath.Join(test.caseTest.path, test.caseTest.filename+".html")) | ||
require.NoError(t, err) | ||
valid, err := html.Parse(strings.NewReader(string(htmlString))) | ||
require.NoError(t, err) | ||
require.NotNil(t, valid) | ||
os.RemoveAll(test.caseTest.path) | ||
}) | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.