Skip to content

Commit c6379fc

Browse files
authored
Update match to matchcode (#1365)
* Use correct function in poll_matching_results Signed-off-by: Jono Yang <jyang@nexb.com> * Only check for success or failure in poll_until_success Signed-off-by: Jono Yang <jyang@nexb.com> * Properly collect match results in MatchToMatchCode Signed-off-by: Jono Yang <jyang@nexb.com> * Update CHANGELOG.rst Signed-off-by: Jono Yang <jyang@nexb.com> --------- Signed-off-by: Jono Yang <jyang@nexb.com>
1 parent 0920ff4 commit c6379fc

File tree

5 files changed

+46
-39
lines changed

5 files changed

+46
-39
lines changed

CHANGELOG.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ v34.8.0 (2024-08-15)
5151
a project.
5252
https://github.com/nexB/scancode.io/issues/1182
5353

54+
- Fix issues in ``match_to_matchcode`` where the incorrect polling function was
55+
used and match results were not properly collected.
56+
5457
v34.7.1 (2024-07-15)
5558
--------------------
5659

scanpipe/pipelines/match_to_matchcode.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,15 @@ def check_matchcode_service_availability(self):
7070

7171
def send_project_json_to_matchcode(self):
7272
"""Create a JSON scan of the project Codebase and send it to MatchCode.io."""
73-
self.run_url = matchcode.send_project_json_to_matchcode(self.project)
73+
self.match_url, self.run_url = matchcode.send_project_json_to_matchcode(
74+
self.project
75+
)
7476

7577
def poll_matching_results(self):
7678
"""Wait until the match results are ready by polling the match run status."""
77-
matchcode.poll_until_success(self.run_url)
79+
matchcode.poll_run_url_status(self.run_url)
7880

7981
def create_packages_from_match_results(self):
8082
"""Create DiscoveredPackages from match results."""
81-
match_results = matchcode.get_match_results(self.run_url)
83+
match_results = matchcode.get_match_results(self.match_url)
8284
matchcode.create_packages_from_match_results(self.project, match_results)

scanpipe/pipes/__init__.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -442,27 +442,18 @@ def poll_until_success(check, sleep=10, **kwargs):
442442
function.
443443
"""
444444
run_status = AbstractTaskFieldsModel.Status
445-
# Continue looping if the run instance has the following statuses
446-
CONTINUE_STATUSES = [
447-
run_status.NOT_STARTED,
448-
run_status.QUEUED,
449-
run_status.RUNNING,
450-
]
451445
# Return False if the run instance has the following statuses
452446
FAIL_STATUSES = [
453447
run_status.FAILURE,
454448
run_status.STOPPED,
455449
run_status.STALE,
456450
]
457-
458451
while True:
459452
status = check(**kwargs)
453+
460454
if status == run_status.SUCCESS:
461455
return True
462456

463-
if status in CONTINUE_STATUSES:
464-
continue
465-
466457
if status in FAIL_STATUSES:
467458
return False
468459

scanpipe/pipes/matchcode.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,9 @@ def send_project_json_to_matchcode(
266266
timeout=timeout,
267267
files=files,
268268
)
269+
match_url = response["url"]
269270
run_url = response["runs"][0]["url"]
270-
return run_url
271+
return match_url, run_url
271272

272273

273274
def get_run_url_status(run_url, **kwargs):
@@ -299,19 +300,26 @@ def poll_run_url_status(run_url, sleep=10):
299300
raise MatchCodeIOException(msg)
300301

301302

302-
def get_match_results(run_url):
303+
def create_match_results_url(match_url):
303304
"""
304-
Given the `run_url` for a pipeline running the matchcode matching pipeline,
305-
return the match results for that run.
305+
Given the `match_url` for a project running the matchcode matching pipeline,
306+
return the match results URL from `match_url`.
306307
"""
307-
response = request_get(run_url)
308-
project_url = response["project"]
309308
# `project_url` can have params, such as "?format=json"
310-
if "?" in project_url:
311-
project_url, _ = project_url.split("?")
312-
project_url = project_url.rstrip("/")
313-
results_url = project_url + "/results/"
314-
return request_get(results_url)
309+
if "?" in match_url:
310+
match_url, _ = match_url.split("?")
311+
match_url = match_url.rstrip("/")
312+
match_url = match_url + "/results/"
313+
return match_url
314+
315+
316+
def get_match_results(match_url):
317+
"""
318+
Given the `match_url` for a project running the matchcode matching pipeline,
319+
return the match results.
320+
"""
321+
match_results_url = create_match_results_url(match_url)
322+
return request_get(match_results_url)
315323

316324

317325
def map_match_results(match_results):

scanpipe/tests/pipes/test_matchcode.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,14 @@ def mock_request_post_return(url, files, timeout):
7676

7777
mock_request_post.side_effect = mock_request_post_return
7878

79-
run_url = matchcode.send_project_json_to_matchcode(self.project1)
79+
match_url, run_url = matchcode.send_project_json_to_matchcode(self.project1)
80+
expected_match_url = (
81+
"http://192.168.1.12/api/matching/65bf1e6d-6bff-4841-9c9b-db5cf25edfa7/"
82+
)
8083
expected_run_url = (
8184
"http://192.168.1.12/api/runs/52b2930d-6e85-4b3e-ba3e-17dd9a618650/"
8285
)
86+
self.assertEqual(expected_match_url, match_url)
8387
self.assertEqual(expected_run_url, run_url)
8488

8589
@mock.patch("scanpipe.pipes.matchcode.request_get")
@@ -280,22 +284,21 @@ def test_scanpipe_pipes_matchcode_create_packages_from_match_results(self):
280284
# This resource should not have a Package match
281285
self.assertFalse(0, len(r2.for_packages))
282286

287+
def test_scanpipe_pipes_matchcode_create_match_results_url(self):
288+
match_url = (
289+
"http://192.168.1.12/api/matching/65bf1e6d-6bff-4841-9c9b-db5cf25edfa7/"
290+
)
291+
expected_match_url = "http://192.168.1.12/api/matching/65bf1e6d-6bff-4841-9c9b-db5cf25edfa7/results/"
292+
self.assertEqual(
293+
expected_match_url, matchcode.create_match_results_url(match_url)
294+
)
295+
283296
@mock.patch("scanpipe.pipes.matchcode.request_get")
284297
@mock.patch("scanpipe.pipes.matchcode.is_available")
285298
def test_scanpipe_pipes_matchcode_get_match_results(
286299
self, mock_is_available, mock_request_get
287300
):
288301
mock_is_available.return_value = True
289-
290-
request_get_check_response_loc = (
291-
self.data
292-
/ "matchcode"
293-
/ "match_to_matchcode"
294-
/ "request_get_check_response.json"
295-
)
296-
with open(request_get_check_response_loc) as f:
297-
mock_request_get_check_return = json.load(f)
298-
299302
request_get_results_response_loc = (
300303
self.data
301304
/ "matchcode"
@@ -305,13 +308,13 @@ def test_scanpipe_pipes_matchcode_get_match_results(
305308
with open(request_get_results_response_loc) as f:
306309
mock_request_get_results_return = json.load(f)
307310
mock_request_get.side_effect = [
308-
mock_request_get_check_return,
309311
mock_request_get_results_return,
310312
]
311313

312-
run_url = "http://192.168.1.12/api/runs/52b2930d-6e85-4b3e-ba3e-17dd9a618650/"
313-
match_results = matchcode.get_match_results(run_url)
314-
314+
match_url = (
315+
"http://192.168.1.12/api/matching/65bf1e6d-6bff-4841-9c9b-db5cf25edfa7/"
316+
)
317+
match_results = matchcode.get_match_results(match_url)
315318
self.assertEqual(mock_request_get_results_return, match_results)
316319

317320
def test_scanpipe_pipes_matchcode_fingerprint_codebase_resources(self):

0 commit comments

Comments
 (0)