Skip to content

Commit b9b3fe1

Browse files
committed
Add support for Reference Fix Commits improver
Signed-off-by: ziad hany <ziadhany2016@gmail.com>
1 parent 6698ab0 commit b9b3fe1

File tree

4 files changed

+90
-301
lines changed

4 files changed

+90
-301
lines changed

vulnerabilities/importers/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
from vulnerabilities.pipelines.v2_importers import ubuntu_osv_importer as ubuntu_osv_importer_v2
7878
from vulnerabilities.pipelines.v2_importers import vulnrichment_importer as vulnrichment_importer_v2
7979
from vulnerabilities.pipelines.v2_importers import xen_importer as xen_importer_v2
80+
from vulnerabilities.pipelines.v2_improvers import reference_collect_commits
8081
from vulnerabilities.utils import create_registry
8182

8283
IMPORTERS_REGISTRY = create_registry(
@@ -118,6 +119,7 @@
118119
nginx_importer.NginxImporterPipeline,
119120
pysec_importer.PyPIImporterPipeline,
120121
fireeye_importer_v2.FireeyeImporterPipeline,
122+
reference_collect_commits.CollectReferencesFixCommitsPipeline,
121123
apache_tomcat.ApacheTomcatImporter,
122124
postgresql.PostgreSQLImporter,
123125
debian.DebianImporter,

vulnerabilities/pipelines/v2_improvers/collect_commits.py

Lines changed: 0 additions & 247 deletions
This file was deleted.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#
2+
# Copyright (c) nexB Inc. and others. All rights reserved.
3+
# VulnerableCode is a trademark of nexB Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
6+
# See https://github.com/aboutcode-org/vulnerablecode for support or download.
7+
# See https://aboutcode.org for more information about nexB OSS projects.
8+
#
9+
10+
from aboutcode.pipeline import LoopProgress
11+
from packageurl.contrib.purl2url import purl2url
12+
from packageurl.contrib.url2purl import url2purl
13+
14+
from aboutcode.federated import get_core_purl
15+
from vulnerabilities.models import AdvisoryV2
16+
from vulnerabilities.models import PackageCommitPatch
17+
from vulnerabilities.pipelines import VulnerableCodeBaseImporterPipelineV2
18+
from vulnerabilities.pipes.advisory import VCS_URLS_SUPPORTED_TYPES
19+
from vulnerabilities.utils import is_commit
20+
21+
22+
class CollectReferencesFixCommitsPipeline(VulnerableCodeBaseImporterPipelineV2):
23+
"""
24+
Improver pipeline to scout References and create PackageCommitPatch entries.
25+
"""
26+
27+
pipeline_id = "collect_fix_commits_v2"
28+
license_expression = None
29+
30+
@classmethod
31+
def steps(cls):
32+
return (cls.collect_and_store_fix_commits,)
33+
34+
def collect_and_store_fix_commits(self):
35+
affected_advisories = (
36+
AdvisoryV2.objects.filter(impacted_packages__affecting_packages__isnull=False)
37+
.prefetch_related("impacted_packages__affecting_packages", "references")
38+
.distinct()
39+
)
40+
41+
self.log(f"Processing {affected_advisories.count():,d} references to collect fix commits.")
42+
43+
package_commit_patch_count = 0
44+
progress = LoopProgress(total_iterations=affected_advisories.count(), logger=self.log)
45+
for adv in progress.iter(affected_advisories.paginated(per_page=500)):
46+
for reference in adv.references.all():
47+
purl = url2purl(reference.url)
48+
if not purl or (purl.type not in VCS_URLS_SUPPORTED_TYPES) or not is_commit(purl.version):
49+
continue
50+
51+
base_purl = get_core_purl(purl)
52+
base_purl_str = base_purl.to_string()
53+
vcs_url = purl2url(base_purl_str)
54+
commit_hash = purl.version
55+
56+
if not vcs_url:
57+
continue
58+
59+
package_commit_patch, created = PackageCommitPatch.objects.get_or_create(
60+
vcs_url=vcs_url,
61+
commit_hash=commit_hash,
62+
)
63+
64+
if created:
65+
for impact in adv.impacted_packages.all():
66+
impact.fixed_by_package_commit_patches.add(package_commit_patch)
67+
package_commit_patch_count += 1
68+
69+
self.log(
70+
f"Successfully created {package_commit_patch_count:,d} PackageCommitPatch entries."
71+
)

0 commit comments

Comments
 (0)