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

fix(output): ensure that vulnerabilities are sorted by ID across groups in table output #1598

Merged
merged 2 commits into from
Feb 27, 2025

Conversation

G-Rath
Copy link
Collaborator

@G-Rath G-Rath commented Feb 9, 2025

While prototyping some changes to help with #1567, I discovered that the table output format is not explicitly sorting vulnerabilities across their groups, meaning we're assumingly using the order that the API gives us, and means our current output does not actually follow a predictable order.

To address this, I've modified the table outputter to sort vulnerability groups by the first ID in each group as a group by definition will always have at least one ID and the first ID should be the one primary one since we already sort ids within each group as part of building the general results

@@ -207,6 +209,11 @@ func tableBuilderInner(vulnResult *models.VulnerabilityResults, calledVulns bool
source.Path = sourcePath
}

// Ensure that groups are sorted consistently using the first ID in each group
slices.SortFunc(pkg.Groups, func(a, b models.GroupInfo) int {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

note that this is actually modifying the value of pkg.Groups on the struct, which applies outside of this function - I actually thought Go didn't work like this, but apparently it does.

I have somewhat mixed feelings about doing it, as generally this is considered bad practice (especially as it's not obvious), but it saves us a few lines of code + a variable, and any bug that it causes I would expect to be because whatever is being impacted should itself be doing a sort if it cares about the order...

Copy link
Contributor

Choose a reason for hiding this comment

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

I think the best approach here is probably to replace everything with Output/Result, which is already sorted and contains all the data we need to display (even extras). I should probably update it for consistency (since container scanning result uses Output/Result).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've had an initial go at this over in #1609 which seemed promising but still has some work to go, so I'd like to land this PR in the short-term if that's ok as it greatly reduces the noise in some of the other work I'm doing with the test suite

Copy link
Collaborator

Choose a reason for hiding this comment

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

Should this sorting be done for the VulnerabilityResults rather than in the output?

@codecov-commenter
Copy link

codecov-commenter commented Feb 9, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 64.60%. Comparing base (40a466d) to head (8400000).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1598   +/-   ##
=======================================
  Coverage   64.59%   64.60%           
=======================================
  Files         156      156           
  Lines       16011    16014    +3     
=======================================
+ Hits        10343    10346    +3     
  Misses       4986     4986           
  Partials      682      682           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@G-Rath G-Rath force-pushed the consistent-sort2 branch 2 times, most recently from 5c75853 to fe32229 Compare February 18, 2025 01:12
@another-rex
Copy link
Collaborator

Hmm looks like the merge I did didn't work, need to run go formatter on it.

@another-rex another-rex merged commit ab8123c into google:main Feb 27, 2025
13 checks passed
@another-rex another-rex deleted the consistent-sort2 branch February 27, 2025 04:56
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.

4 participants