|
| 1 | +# |
| 2 | +# Copyright (c) nexB Inc. and others. All rights reserved. |
| 3 | +# FederatedCode 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/nexB/federatedcode for support or download. |
| 7 | +# See https://aboutcode.org for more information about AboutCode.org OSS projects. |
| 8 | +# |
| 9 | + |
| 10 | +from pathlib import Path |
| 11 | +from traceback import format_exc as traceback_format_exc |
| 12 | + |
| 13 | +from aboutcode.pipeline import LoopProgress |
| 14 | + |
| 15 | +from fedcode.models import Package |
| 16 | +from fedcode.models import Repository |
| 17 | +from fedcode.pipelines import FederatedCodePipeline |
| 18 | +from fedcode.pipes import utils |
| 19 | + |
| 20 | + |
| 21 | +class SyncScanCodeScans(FederatedCodePipeline): |
| 22 | + """Sync Package scans from FederatedCode git repositories.""" |
| 23 | + |
| 24 | + pipeline_id = "sync_scancode_scans" |
| 25 | + |
| 26 | + @classmethod |
| 27 | + def steps(cls): |
| 28 | + return ( |
| 29 | + cls.get_git_repos, |
| 30 | + cls.sync_scan_repositories, |
| 31 | + ) |
| 32 | + |
| 33 | + def get_git_repos(self): |
| 34 | + self.git_repos = Repository.objects.all() |
| 35 | + |
| 36 | + def sync_scan_repositories(self): |
| 37 | + repositories_count = self.git_repos.count() |
| 38 | + self.log(f"Syncing package scans from {repositories_count:,d} repositories") |
| 39 | + |
| 40 | + synced_package_scan_count = 0 |
| 41 | + progress = LoopProgress(total_iterations=repositories_count, logger=self.log) |
| 42 | + for repository in progress.iter(self.git_repos.iterator(chunk_size=2000)): |
| 43 | + repository.git_repo_obj.remotes.origin.pull() |
| 44 | + synced_package_scan_count += sync_scancodeio_scan( |
| 45 | + repository=repository, |
| 46 | + logger=self.log, |
| 47 | + ) |
| 48 | + |
| 49 | + self.log(f"Successfully synced {synced_package_scan_count:,d} package scans") |
| 50 | + |
| 51 | + |
| 52 | +def sync_scancodeio_scan(repository, logger): |
| 53 | + repo = repository.git_repo_obj |
| 54 | + latest_commit_hash = repo.head.commit.hexsha |
| 55 | + latest_commit = repo.commit(latest_commit_hash) |
| 56 | + |
| 57 | + if last_commit_hash := repository.last_imported_commit: |
| 58 | + last_imported_commit = repo.commit(last_commit_hash) |
| 59 | + diffs = last_imported_commit.diff(latest_commit) |
| 60 | + scans = [item for item in diffs if item.a_path.endswith("scancodeio.json")] |
| 61 | + scan_count = sync_scan_from_diff(diffs=scans, repository=repository, logger=logger) |
| 62 | + else: |
| 63 | + scan_count = sync_all_scan(repository=repository, logger=logger) |
| 64 | + |
| 65 | + repository.last_imported_commit = latest_commit_hash |
| 66 | + repository.save() |
| 67 | + |
| 68 | + return scan_count |
| 69 | + |
| 70 | + |
| 71 | +def sync_scan_from_diff(diffs, repository, logger): |
| 72 | + scans = [ |
| 73 | + item |
| 74 | + for item in diffs |
| 75 | + if item.a_path.endswith("scancodeio.json") or item.b_path.endswith("scancodeio.json") |
| 76 | + ] |
| 77 | + scan_count = len(scans) |
| 78 | + |
| 79 | + logger(f"Syncing {scan_count:,d} package scan from {repository.url}") |
| 80 | + progress = LoopProgress(total_iterations=scan_count, logger=logger) |
| 81 | + for scan in progress.iter(scans): |
| 82 | + change_type = scan.change_type |
| 83 | + if change_type in ("A", "M", "R"): |
| 84 | + scan_path = scan.b_path |
| 85 | + action = utils.create_note |
| 86 | + elif change_type == "D": |
| 87 | + scan_path = scan.a_path |
| 88 | + action = utils.delete_note |
| 89 | + |
| 90 | + purl = utils.package_metadata_path_to_purl(path=Path(scan_path), version=False) |
| 91 | + package, _ = Package.objects.get_or_create(purl=str(purl), service=repository.admin) |
| 92 | + note = utils.get_scan_note(path=Path(scan_path)) |
| 93 | + action(pkg=package, note_dict=note) |
| 94 | + return scan_count |
| 95 | + |
| 96 | + |
| 97 | +def sync_all_scan(repository, logger): |
| 98 | + repo = repository.git_repo_obj |
| 99 | + root = Path(repo.working_dir) |
| 100 | + scan_count = sum(1 for _ in root.rglob("scancodeio.json")) |
| 101 | + |
| 102 | + scans = root.rglob("scancodeio.json") |
| 103 | + logger(f"Syncing {scan_count:,d} package scan from {repo.remotes.origin.url}") |
| 104 | + |
| 105 | + progress = LoopProgress(total_iterations=scan_count, logger=logger) |
| 106 | + for scan in progress.iter(scans): |
| 107 | + relative_path = scan.relative_to(root) |
| 108 | + purl = utils.package_metadata_path_to_purl(relative_path, version=False) |
| 109 | + package, _ = Package.objects.get_or_create(purl=str(purl), service=repository.admin) |
| 110 | + note = utils.get_scan_note(path=relative_path) |
| 111 | + utils.create_note(pkg=package, note_dict=note) |
| 112 | + return scan_count |
0 commit comments