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