Skip to content

Commit f33a0ce

Browse files
committed
Refactor code and fix bugs
Signed-off-by: ziad hany <[email protected]>
1 parent 7e1ae32 commit f33a0ce

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed

minecode_pipelines/miners/cargo.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
#
99
from datetime import datetime
1010

11-
from minecode_pipelines.pipes import fetch_checkpoint_from_github
11+
from minecode_pipelines.pipes import get_checkpoint_from_file
1212
from minecode_pipelines.pipes import get_commit_at_distance_ahead
1313
from minecode_pipelines.pipes import update_checkpoints_in_github
14-
from minecode_pipelines.pipes import MINECODE_PIPELINES_CONFIG_REPO
1514
from minecode_pipelines.pipes import get_changed_files
1615
from minecode_pipelines.pipes.cargo import store_cargo_packages
1716
from scanpipe.pipes.federatedcode import commit_changes
@@ -38,15 +37,19 @@ def process_cargo_packages(cargo_index_repo, cloned_data_repo, config_repo, logg
3837
base_path = Path(cargo_index_repo.working_tree_dir)
3938

4039
while True:
41-
cargo_checkpoints = fetch_checkpoint_from_github(
42-
config_repo=MINECODE_PIPELINES_CONFIG_REPO, checkpoint_path=CARGO_CHECKPOINT_PATH
40+
cargo_checkpoints = get_checkpoint_from_file(
41+
cloned_repo=config_repo, path=CARGO_CHECKPOINT_PATH
4342
)
4443

4544
checkpoints_last_commit = cargo_checkpoints.get("last_commit")
4645

47-
next_commit = get_commit_at_distance_ahead(
48-
cargo_index_repo, checkpoints_last_commit, num_commits_ahead=10, branch_name="master"
49-
)
46+
try:
47+
next_commit = get_commit_at_distance_ahead(
48+
cargo_index_repo, checkpoints_last_commit, num_commits_ahead=COMMIT_BATCH_SIZE, branch_name="master"
49+
)
50+
except ValueError as e:
51+
logger(str(e))
52+
break
5053

5154
if next_commit == checkpoints_last_commit:
5255
logger("No new commits to mine")
@@ -75,19 +78,25 @@ def process_cargo_packages(cargo_index_repo, cloned_data_repo, config_repo, logg
7578
with open(file_path, encoding="utf-8") as f:
7679
for line in f:
7780
if line.strip():
78-
packages.append(json.loads(line))
81+
try:
82+
packages.append(json.loads(line))
83+
except json.JSONDecodeError as e:
84+
logger(f"Skipping invalid JSON in {file_path}: {e}")
7985

8086
file_counter += 1
8187

8288
# Commit and push after each full batch or when processing the last file
8389
commit_and_push = (file_counter % PACKAGE_BATCH_SIZE == 0) or (
84-
idx == len(changed_files)
90+
idx == len(changed_files) - 1
8591
)
86-
purl_file, base_purl = store_cargo_packages(packages, cloned_data_repo)
87-
logger(f"writing packageURLs for package: {base_purl} at: {purl_file}")
8892

89-
purl_files.append(purl_file)
90-
purls.append(str(base_purl))
93+
result_store = store_cargo_packages(packages, cloned_data_repo)
94+
if result_store:
95+
purl_file, base_purl = result_store
96+
logger(f"writing packageURLs for package: {base_purl} at: {purl_file}")
97+
98+
purl_files.append(purl_file)
99+
purls.append(str(base_purl))
91100

92101
if not commit_and_push:
93102
continue

minecode_pipelines/pipes/cargo.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
from pathlib import Path
2+
13
from aboutcode import hashid
24
from packageurl import PackageURL
35
from aboutcode.hashid import get_core_purl
46

5-
from minecode_pipelines.pipes import write_packageurls_to_file
7+
from minecode_pipelines.pipes import write_data_to_yaml_file
68

79

810
def store_cargo_packages(packages, repo):
@@ -25,4 +27,6 @@ def store_cargo_packages(packages, repo):
2527
updated_purls.append(purl)
2628

2729
ppath = hashid.get_package_purls_yml_file_path(base_purl)
28-
return write_packageurls_to_file(repo, ppath, updated_purls), base_purl
30+
purl_file_full_path = Path(repo.working_dir) / ppath
31+
write_data_to_yaml_file(path=purl_file_full_path, data=updated_purls)
32+
return purl_file_full_path, base_purl

minecode_pipelines/tests/pipes/test_cargo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414

1515
class CargoPipelineTests(TestCase):
16-
@patch("minecode_pipelines.pipes.cargo.write_packageurls_to_file")
16+
@patch("minecode_pipelines.pipes.cargo.write_data_to_yaml_file")
1717
def test_collect_packages_from_cargo_calls_write(self, mock_write):
1818
packages_file = DATA_DIR / "c5store"
1919
expected_file = DATA_DIR / "c5store-expected.yaml"

0 commit comments

Comments
 (0)