Skip to content

Commit 5c83405

Browse files
tdruezVarshaUN
authored andcommitted
Display matched snippets details in "Resource viewer" (aboutcode-org#1693)
* Display matched snippets details in "Resource viewer" aboutcode-org#1688 Signed-off-by: tdruez <tdruez@nexb.com> * Remove print statements used for debugging aboutcode-org#1688 Signed-off-by: tdruez <tdruez@nexb.com> --------- Signed-off-by: tdruez <tdruez@nexb.com>
1 parent ecf3a21 commit 5c83405

File tree

3 files changed

+41
-18
lines changed

3 files changed

+41
-18
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ Changelog
44
v34.12.0 (unreleased)
55
---------------------
66

7+
- Display matched snippets details in "Resource viewer", including the package,
8+
resource, and similarity values.
9+
https://github.com/aboutcode-org/scancode.io/issues/1688
10+
711
- Add filtering by label and pipeline in the ``flush-projects`` management command.
812
Also, a new ``--dry-run`` option is available to test the filters before applying
913
the deletion.

scanpipe/tests/test_views.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1339,7 +1339,17 @@ def test_scanpipe_views_codebase_resource_details_get_matched_snippet_annotation
13391339
resource1.save()
13401340
resource1.refresh_from_db()
13411341
results = CodebaseResourceDetailsView.get_matched_snippet_annotations(resource1)
1342-
expected_results = [{"start_line": 1, "end_line": 6}]
1342+
expected_results = [
1343+
{
1344+
"start_line": 1,
1345+
"end_line": 6,
1346+
"text": (
1347+
"package: pkg:github/isaacs/inherits@v2.0.3\n"
1348+
"resource: inherits-2.0.3/inherits.js\n"
1349+
"similarity: 1.0\n"
1350+
),
1351+
}
1352+
]
13431353
self.assertEqual(expected_results, results)
13441354

13451355
def test_project_packages_export_json(self):

scanpipe/views.py

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2006,24 +2006,33 @@ def get_license_annotations(self, field_name):
20062006

20072007
@staticmethod
20082008
def get_matched_snippet_annotations(resource):
2009-
# convert qspan from list of ints to Spans
2010-
matched_snippet_annotations = []
20112009
matched_snippets = resource.extra_data.get("matched_snippets")
2012-
if matched_snippets:
2013-
line_by_pos = resource.extra_data.get("line_by_pos")
2014-
for matched_snippet in matched_snippets:
2015-
match_detections = matched_snippet["match_detections"]
2016-
qspan = Span(match_detections)
2017-
for span in qspan.subspans():
2018-
# line_by_pos is stored as JSON and keys in JSON are always
2019-
# strings
2020-
matched_snippet_annotations.append(
2021-
{
2022-
"start_line": line_by_pos[str(span.start)],
2023-
"end_line": line_by_pos[str(span.end)],
2024-
}
2025-
)
2026-
return matched_snippet_annotations
2010+
line_by_pos = resource.extra_data.get("line_by_pos")
2011+
if not matched_snippets:
2012+
return []
2013+
2014+
snippet_annotations = []
2015+
for snippet in matched_snippets:
2016+
package = snippet.get("package", "")
2017+
resource = snippet.get("resource", "")
2018+
similarity = snippet.get("similarity", 0)
2019+
text = (
2020+
f"package: {package}\nresource: {resource}\nsimilarity: {similarity}\n"
2021+
)
2022+
2023+
# convert qspan from list of ints to Spans
2024+
qspan = Span(snippet["match_detections"])
2025+
for span in qspan.subspans():
2026+
# line_by_pos is stored as JSON and keys in JSON are always strings
2027+
snippet_annotations.append(
2028+
{
2029+
"start_line": line_by_pos[str(span.start)],
2030+
"end_line": line_by_pos[str(span.end)],
2031+
"text": text,
2032+
}
2033+
)
2034+
2035+
return snippet_annotations
20272036

20282037
def get_context_data(self, **kwargs):
20292038
context = super().get_context_data(**kwargs)

0 commit comments

Comments
 (0)