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

add JUnit output format #929

Merged
merged 1 commit into from
Jul 22, 2024
Merged

add JUnit output format #929

merged 1 commit into from
Jul 22, 2024

Conversation

sebhoss
Copy link
Contributor

@sebhoss sebhoss commented Jul 21, 2024

fixes #928

The output groups violations by files in a testsuite and expresses each violation inside a file as a testcase. The files are sorted so that we have stable output during tests and each failure message contains the documentation link for a failed rule.

I'm not entirely sure whether notices should be part of the output as well. I've skipped them because most other reporters do not output them either.

Copy link
Member

@anderseknert anderseknert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing work on such a short notice! Just a suggestion to consider / discuss, but once that's resolved we'll have this merged. Thanks!

Name: violation.Title,
Classname: violation.Location.String(),
Failure: &junit.Result{
Message: fmt.Sprintf("%s. To learn more, see: %s", violation.Description, getDocumentationURL(violation)),
Copy link
Member

Choose a reason for hiding this comment

The 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 Name like $category/$title: $message, or perhaps have the Message include it?

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yeah good idea! What would $message be though? Maybe just the description of a violation and have the Message contain both description and link to the docs?

I think we can add any kind of additional metadata to the Data field of a junit.Result. It's currently not used in the code but we could write all the violation details in there. I'll try it out on our instance and post screenshots to further the discussion

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shows the test report widget inside a merge request:

2024-07-22T10:15:42,541965099+02:00

that's the popup you get when clicking on View details:

2024-07-22T10:15:59,187202303+02:00

and this is what the Full report looks like

2024-07-22T10:16:15,244480379+02:00

when you click on View details in the full report, you get the same popup as in the merge request

Copy link
Member

Choose a reason for hiding this comment

The 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!

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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:

2024-07-22T12:50:15,296173773+02:00

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

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great though!

cmd/lint.go Outdated
// currently, JSON and SARIF will get the same generic JSON error format
if format == formatJSON || format == formatSarif {
// currently, JSON, SARIF, and JUnit will get the same generic JSON error format
if format == formatJSON || format == formatSarif || format == formatJunit {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would probably have expected the errors to be reported as "JUnit errors"... but I guess we'd need to update the reporters to allow for that. We'll deal with that later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK thanks for pinging me about this - I wasn't really sure why this code exists. In my understanding, this function is only called if the lint command itself failed. As a user I would not expect to get any kind of formatted report for that but rather a harsh error message telling me to fix my setup before regal can work properly.

That said, I could add proper JUnit XML output here as well. Besides Failure it does contain an Error field that could be used here..

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but the lint command can fail for reasons like parser errors too. Ideally, other tools like opa test would have failed the build before linting in that case, but still. In most of the contexts where e.g. SARIF is used, like in CI/CD pipelines, users would likely want to find the report in the same place regardless of what made it fail, but yeah, there's a reason we've not exactly made this our highest priority too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK OK sounds good - will add JUnit output here as well!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If non-trivial to pull off, it's alright to defer this to later. We should look into better error reporting altogether sometime soon anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the latest version is a reasonable start - it basically just creates a single testsuite with a single testcase that contains the given error string.

@anderseknert
Copy link
Member

anderseknert commented Jul 22, 2024

Sorry, I didn't think of this before, but can you please update this section in the README too?

And I suppose this guide could use an update too so that GitLab users can benefit from this.


## OPA Check and Strict Mode

Linting with Regal assumes syntactically correct Rego. If there are errors parsing any files during linting, the
process is aborted and any parser errors are logged similarly to OPA. OPA itself provides a "linter" of sorts,
via the `opa check` comand and its `--strict` flag. This checks the provided Rego files not only for syntax errors,
via the `opa check` command and its `--strict` flag. This checks the provided Rego files not only for syntax errors,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks :)

Copy link
Member

@anderseknert anderseknert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot!

@charlieegan3 take a look if you want, or feel free to merge directly.

@anderseknert
Copy link
Member

Apologies for the flaky tests, @sebhoss. We'll fix that this week, but do know it's not from your change.

Signed-off-by: Sebastian Hoß <seb@xn--ho-hia.de>
@sebhoss
Copy link
Contributor Author

sebhoss commented Jul 22, 2024

OK I rebased on latest main, fixed all linting errors, and squashed all commits. Good to go from my side ^^

@anderseknert anderseknert merged commit 2a29cd2 into StyraInc:main Jul 22, 2024
3 checks passed
srenatus pushed a commit to srenatus/regal that referenced this pull request Oct 1, 2024
Signed-off-by: Sebastian Hoß <seb@xn--ho-hia.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

support JUnit XML output format
2 participants