Skip to content

Commit 42f3ca1

Browse files
committed
Add command to sync pipelines
Signed-off-by: Keshav Priyadarshi <git@keshav.space>
1 parent 76d83db commit 42f3ca1

2 files changed

Lines changed: 88 additions & 2 deletions

File tree

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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

tests/test_utils.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,12 @@ def test_full_reverse():
7474

7575

7676
def test_full_resolve():
77-
result_args, result_url_name = full_resolve(f"https://127.0.0.1:8000/notes/7e676ad1-995d-405c-a829-cb39813c74e5")
78-
assert "7e676ad1-995d-405c-a829-cb39813c74e5" == str(result_args['uuid'])
77+
result_args, result_url_name = full_resolve(
78+
f"https://127.0.0.1:8000/notes/7e676ad1-995d-405c-a829-cb39813c74e5"
79+
)
80+
assert "7e676ad1-995d-405c-a829-cb39813c74e5" == str(result_args["uuid"])
7981
assert "note-page" == result_url_name
8082

83+
8184
def test_check_purl_actor():
8285
assert check_purl_actor("pkg:maven/org.apache.logging")

0 commit comments

Comments
 (0)