Skip to content

Commit 6db89a4

Browse files
committed
Add docs, changelog, and refine the implementation
Signed-off-by: tdruez <[email protected]>
1 parent ff20951 commit 6db89a4

File tree

6 files changed

+39
-31
lines changed

6 files changed

+39
-31
lines changed

CHANGELOG.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
Changelog
22
=========
33

4+
v35.1.0 (unreleased)
5+
--------------------
6+
7+
- Add a ``--fail-on-vulnerabilities`` option in ``check-compliance`` management command.
8+
When this option is enabled, the command will exit with a non-zero status if known
9+
vulnerabilities are detected in discovered packages and dependencies.
10+
Requires the ``find_vulnerabilities`` pipeline to be executed beforehand.
11+
https://github.com/aboutcode-org/scancode.io/pull/1702
12+
413
v35.0.0 (2025-06-23)
514
--------------------
615

docs/command-line-interface.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,10 @@ Optional arguments:
497497
- ``--fail-level {ERROR,WARNING,MISSING}`` Compliance alert level that will cause the
498498
command to exit with a non-zero status. Default is ERROR.
499499

500+
- ``--fail-on-vulnerabilities`` Exit with a non-zero status if known vulnerabilities
501+
are detected in discovered packages and dependencies.
502+
Requires the ``find_vulnerabilities`` pipeline to be executed beforehand.
503+
500504
`$ scanpipe archive-project --project PROJECT`
501505
----------------------------------------------
502506

scanpipe/management/commands/check-compliance.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,21 +86,10 @@ def check_compliance(self, fail_level):
8686
return count > 0
8787

8888
def check_vulnerabilities(self):
89-
# TODO: Remove duplication with scanpipe.pipes.output.add_vulnerabilities_sheet
90-
vulnerable_packages_queryset = (
91-
self.project.discoveredpackages.vulnerable()
92-
.only_package_url_fields(extra=["affected_by_vulnerabilities"])
93-
.order_by_package_url()
94-
)
95-
vulnerable_dependencies_queryset = (
96-
self.project.discovereddependencies.vulnerable()
97-
.only_package_url_fields(extra=["affected_by_vulnerabilities"])
98-
.order_by_package_url()
99-
)
89+
packages = self.project.discoveredpackages.vulnerable_ordered()
90+
dependencies = self.project.discovereddependencies.vulnerable_ordered()
10091

101-
vulnerable_records = list(vulnerable_packages_queryset) + list(
102-
vulnerable_dependencies_queryset
103-
)
92+
vulnerable_records = list(packages) + list(dependencies)
10493
count = len(vulnerable_records)
10594

10695
if self.verbosity > 0:

scanpipe/models.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3150,6 +3150,13 @@ class VulnerabilityQuerySetMixin:
31503150
def vulnerable(self):
31513151
return self.filter(~Q(affected_by_vulnerabilities__in=EMPTY_VALUES))
31523152

3153+
def vulnerable_ordered(self):
3154+
return (
3155+
self.vulnerable()
3156+
.only_package_url_fields(extra=["affected_by_vulnerabilities"])
3157+
.order_by_package_url()
3158+
)
3159+
31533160

31543161
class DiscoveredPackageQuerySet(
31553162
VulnerabilityQuerySetMixin,

scanpipe/pipes/output.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -567,21 +567,11 @@ def to_xlsx(project):
567567

568568

569569
def add_vulnerabilities_sheet(workbook, project):
570-
vulnerable_packages_queryset = (
571-
DiscoveredPackage.objects.project(project)
572-
.vulnerable()
573-
.only_package_url_fields(extra=["affected_by_vulnerabilities"])
574-
.order_by_package_url()
575-
)
576-
vulnerable_dependencies_queryset = (
577-
DiscoveredDependency.objects.project(project)
578-
.vulnerable()
579-
.only_package_url_fields(extra=["affected_by_vulnerabilities"])
580-
.order_by_package_url()
581-
)
570+
vulnerable_packages = project.discoveredpackages.vulnerable_ordered()
571+
vulnerable_dependencies = project.discovereddependencies.vulnerable_ordered()
582572
vulnerable_querysets = [
583-
vulnerable_packages_queryset,
584-
vulnerable_dependencies_queryset,
573+
vulnerable_packages,
574+
vulnerable_dependencies,
585575
]
586576

587577
vulnerability_fields = [

scanpipe/tests/test_commands.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
from scanpipe.pipes import flag
5050
from scanpipe.pipes import purldb
5151
from scanpipe.tests import filter_warnings
52+
from scanpipe.tests import make_dependency
5253
from scanpipe.tests import make_mock_response
5354
from scanpipe.tests import make_package
5455
from scanpipe.tests import make_project
@@ -1224,8 +1225,12 @@ def test_scanpipe_management_command_check_compliance_vulnerabilities(self):
12241225
out_value = out.getvalue().strip()
12251226
self.assertEqual("No vulnerabilities found", out_value)
12261227

1227-
package1.update(
1228-
affected_by_vulnerabilities=[{"vulnerability_id": "VCID-cah8-awtr-aaad"}]
1228+
vulnerability_data = [{"vulnerability_id": "VCID-cah8-awtr-aaad"}]
1229+
package1.update(affected_by_vulnerabilities=vulnerability_data)
1230+
make_dependency(
1231+
project,
1232+
dependency_uid="dependency1",
1233+
affected_by_vulnerabilities=vulnerability_data,
12291234
)
12301235
out = StringIO()
12311236
options = ["--project", project.name, "--fail-on-vulnerabilities"]
@@ -1234,7 +1239,11 @@ def test_scanpipe_management_command_check_compliance_vulnerabilities(self):
12341239
self.assertEqual(cm.exception.code, 1)
12351240
out_value = out.getvalue().strip()
12361241
expected = (
1237-
"1 vulnerable records found:\npkg:generic/[email protected]\n > VCID-cah8-awtr-aaad"
1242+
"2 vulnerable records found:\n"
1243+
"pkg:generic/[email protected]\n"
1244+
" > VCID-cah8-awtr-aaad\n"
1245+
"dependency1\n"
1246+
" > VCID-cah8-awtr-aaad"
12381247
)
12391248
self.assertEqual(expected, out_value)
12401249

0 commit comments

Comments
 (0)