-
Notifications
You must be signed in to change notification settings - Fork 37
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
add JUnit output format #929
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,9 +7,11 @@ import ( | |
"fmt" | ||
"io" | ||
"os" | ||
"sort" | ||
"strings" | ||
|
||
"github.com/fatih/color" | ||
"github.com/jstemmer/go-junit-report/v2/junit" | ||
"github.com/olekukonko/tablewriter" | ||
"github.com/owenrumney/go-sarif/v2/sarif" | ||
|
||
|
@@ -53,6 +55,12 @@ type SarifReporter struct { | |
out io.Writer | ||
} | ||
|
||
// JUnitReporter reports violations in the JUnit XML format | ||
// (https://github.com/junit-team/junit5/blob/main/platform-tests/src/test/resources/jenkins-junit.xsd). | ||
type JUnitReporter struct { | ||
out io.Writer | ||
} | ||
|
||
// NewPrettyReporter creates a new PrettyReporter. | ||
func NewPrettyReporter(out io.Writer) PrettyReporter { | ||
return PrettyReporter{out: out} | ||
|
@@ -83,6 +91,11 @@ func NewSarifReporter(out io.Writer) SarifReporter { | |
return SarifReporter{out: out} | ||
} | ||
|
||
// NewJUnitReporter creates a new JUnitReporter. | ||
func NewJUnitReporter(out io.Writer) JUnitReporter { | ||
return JUnitReporter{out: out} | ||
} | ||
|
||
// Publish prints a pretty report to the configured output. | ||
func (tr PrettyReporter) Publish(_ context.Context, r report.Report) error { | ||
table := buildPrettyViolationsTable(r.Violations) | ||
|
@@ -423,3 +436,49 @@ func getUniqueViolationURLs(violations []report.Violation) map[string]string { | |
|
||
return urls | ||
} | ||
|
||
// Publish prints a JUnit XML report to the configured output. | ||
func (tr JUnitReporter) Publish(_ context.Context, r report.Report) error { | ||
testSuites := junit.Testsuites{ | ||
Name: "regal", | ||
} | ||
|
||
// group by file & sort by file | ||
files := make([]string, 0) | ||
violationsPerFile := map[string][]report.Violation{} | ||
|
||
for _, violation := range r.Violations { | ||
files = append(files, violation.Location.File) | ||
violationsPerFile[violation.Location.File] = append(violationsPerFile[violation.Location.File], violation) | ||
} | ||
|
||
sort.Strings(files) | ||
|
||
for _, file := range files { | ||
testsuite := junit.Testsuite{ | ||
Name: file, | ||
} | ||
|
||
for _, violation := range violationsPerFile[file] { | ||
testsuite.AddTestcase(junit.Testcase{ | ||
Name: fmt.Sprintf("%s/%s: %s", violation.Category, violation.Title, violation.Description), | ||
Classname: violation.Location.String(), | ||
Failure: &junit.Result{ | ||
Message: fmt.Sprintf("%s. To learn more, see: %s", violation.Description, getDocumentationURL(violation)), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most notable omission here would be the category... and understandably so given that it's a test result format after all. If there isn't some clever meta attribute we can use for that, perhaps we could format the Knowing the category helps with prioritization of fixing the issues reported, so it'd be good to have it in there somewhere, but again, if there's some other place to put it, I'm open to suggestions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh yeah good idea! What would I think we can add any kind of additional metadata to the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks great! Where does the category come from there? You've updated your code to show that? I don't see it in the PR, but that looks great to me, so please do include it! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. er yeah I pushed it to another branch so not to pollute this PR. I also just realized that GitLab no longer shows the failure message which included the link to the documentation. So the latest version now includes the documentation link and its output is aligned with the pretty printer. It looks like this: It's kinda sad that GitLab formats this output as a code block so links are not clickable but it think there is no way around that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, but not much to do about it I suppose. GitHub doesn't make it a link either, even if it's not in a codeblock. In that context we can at least display real markdown links in the summary though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks great though! |
||
Type: violation.Level, | ||
Data: fmt.Sprintf("Rule: %s\nDescription: %s\nCategory: %s\nLocation: %s\nText: %s\nDocumentation: %s", | ||
violation.Title, | ||
violation.Description, | ||
violation.Category, | ||
violation.Location.String(), | ||
strings.TrimSpace(*violation.Location.Text), | ||
getDocumentationURL(violation)), | ||
}, | ||
}) | ||
} | ||
|
||
testSuites.AddSuite(testsuite) | ||
} | ||
|
||
return testSuites.WriteXML(tr.out) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks :)