|
| 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 django.core.management.base import BaseCommand |
| 11 | +from django.core.management.base import CommandError |
| 12 | + |
| 13 | +from fedcode.pipelines import sync_scancode_scans |
| 14 | +from fedcode.pipelines import sync_vulnerablecode |
| 15 | + |
| 16 | +SYNC_REGISTRY = [ |
| 17 | + sync_scancode_scans.SyncScanCodeScans, |
| 18 | + sync_vulnerablecode.SyncVulnerableCode, |
| 19 | +] |
| 20 | + |
| 21 | +SYNC_REGISTRY = {x.pipeline_id: x for x in SYNC_REGISTRY} |
| 22 | + |
| 23 | + |
| 24 | +class Command(BaseCommand): |
| 25 | + help = "Sync metadata from git repository" |
| 26 | + |
| 27 | + def add_arguments(self, parser): |
| 28 | + parser.add_argument( |
| 29 | + "--list", |
| 30 | + action="store_true", |
| 31 | + help="List available pipelines", |
| 32 | + ) |
| 33 | + parser.add_argument("--all", action="store_true", help="Sync all repo data.") |
| 34 | + |
| 35 | + parser.add_argument("pipelines", nargs="*", help="Pipeline ID") |
| 36 | + |
| 37 | + def handle(self, *args, **options): |
| 38 | + try: |
| 39 | + if options["list"]: |
| 40 | + self.list_pipelines() |
| 41 | + elif options["all"]: |
| 42 | + self.import_data(pipelines=SYNC_REGISTRY.values()) |
| 43 | + else: |
| 44 | + pipelines = options["pipelines"] |
| 45 | + if not pipelines: |
| 46 | + raise CommandError( |
| 47 | + 'Please provide at least one pipeline to execute or use "--all".' |
| 48 | + ) |
| 49 | + self.import_data(validate_pipelines(pipelines)) |
| 50 | + except KeyboardInterrupt: |
| 51 | + raise CommandError("Keyboard interrupt received. Stopping...") |
| 52 | + |
| 53 | + def list_pipelines(self): |
| 54 | + self.stdout.write("Metadata can be synced from the following pipelines:") |
| 55 | + self.stdout.write("\n".join(SYNC_REGISTRY)) |
| 56 | + |
| 57 | + def import_data(self, pipelines): |
| 58 | + """Execute the given ``pipeline``.""" |
| 59 | + failed_pipelines = [] |
| 60 | + |
| 61 | + for pipeline in pipelines: |
| 62 | + self.stdout.write(f"Syncing data using {pipeline.pipeline_id}") |
| 63 | + status, error = pipeline().execute() |
| 64 | + if status != 0: |
| 65 | + self.stdout.write(error) |
| 66 | + failed_pipelines.append(pipeline.pipeline_id) |
| 67 | + |
| 68 | + if failed_pipelines: |
| 69 | + raise CommandError(f"{len(failed_pipelines)} failed!: {','.join(failed_pipelines)}") |
| 70 | + |
| 71 | + |
| 72 | +def validate_pipelines(pipelines): |
| 73 | + validated_pipelines = [] |
| 74 | + unknown_pipelines = [] |
| 75 | + for pipeline in pipelines: |
| 76 | + try: |
| 77 | + validated_pipelines.append(SYNC_REGISTRY[pipeline]) |
| 78 | + except KeyError: |
| 79 | + unknown_pipelines.append(pipeline) |
| 80 | + if unknown_pipelines: |
| 81 | + raise CommandError(f"Unknown pipelines: {unknown_pipelines}") |
| 82 | + |
| 83 | + return validated_pipelines |
0 commit comments