diff --git a/vulnerabilities/api.py b/vulnerabilities/api.py index dbc26b2ea..3fba3c6ff 100644 --- a/vulnerabilities/api.py +++ b/vulnerabilities/api.py @@ -106,6 +106,7 @@ def to_representation(self, instance): fixing_vulnerabilities = MinimalVulnerabilitySerializer( many=True, source="resolved_to", read_only=True ) + fixed_packages = MinimalPackageSerializer(many=True, read_only=True) class Meta: model = Package @@ -119,6 +120,7 @@ class Meta: "qualifiers", "subpath", "affected_by_vulnerabilities", + "fixed_packages", "fixing_vulnerabilities", ] diff --git a/vulnerabilities/models.py b/vulnerabilities/models.py index cf60fabfd..51203797a 100644 --- a/vulnerabilities/models.py +++ b/vulnerabilities/models.py @@ -193,6 +193,23 @@ def resolved_to(self): """ return self.vulnerabilities.filter(packagerelatedvulnerability__fix=True) + @property + def fixed_packages(self): + """ + Returns vulnerabilities which are affecting this package. + """ + return ( + Package.objects.filter( + name=self.name, + namespace=self.namespace, + type=self.type, + qualifiers=self.qualifiers, + subpath=self.subpath, + ) + .filter(packagerelatedvulnerability__fix=True) + .distinct() + ) + def set_package_url(self, package_url): """ Set each field values to the values of the provided `package_url` string diff --git a/vulnerabilities/tests/test_fix_api.py b/vulnerabilities/tests/test_fix_api.py index 61fd0ff08..90efd46c0 100644 --- a/vulnerabilities/tests/test_fix_api.py +++ b/vulnerabilities/tests/test_fix_api.py @@ -91,6 +91,7 @@ def test_api_with_single_vulnerability(self): "unresolved_vulnerabilities": [], "qualifiers": {}, "subpath": "", + "fixed_packages": [], "affected_by_vulnerabilities": [], "fixing_vulnerabilities": [], }