Skip to content

Commit 3cb1a2c

Browse files
Improve detail views with links in UI
This commit improves license, package and resource details views with detection tabs and links to each other. Signed-off-by: Ayan Sinha Mahapatra <ayansmahapatra@gmail.com>
1 parent b20ce50 commit 3cb1a2c

File tree

10 files changed

+357
-67
lines changed

10 files changed

+357
-67
lines changed

scanpipe/models.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3640,18 +3640,20 @@ def create_from_data(cls, project, detection_data):
36403640
discovered_license.save(save_error=False, capture_exception=False)
36413641
return discovered_license
36423642

3643-
def update_with_file_region(self, file_region):
3643+
def update_with_file_region(self, file_region, count_detection):
36443644
"""
36453645
If the `file_region` is a new file region, include it in the
36463646
`file_regions` list and increase the `detection_count` by 1.
36473647
"""
36483648
file_region_data = file_region.to_dict()
36493649
if file_region_data not in self.file_regions:
36503650
self.file_regions.append(file_region_data)
3651-
if not self.detection_count:
3652-
self.detection_count = 1
3653-
else:
3654-
self.detection_count += 1
3651+
if count_detection:
3652+
if not self.detection_count:
3653+
self.detection_count = 1
3654+
else:
3655+
self.detection_count += 1
3656+
36553657
self.save(update_fields=["detection_count", "file_regions"])
36563658

36573659

scanpipe/pipes/__init__.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ def update_or_create_license_detection(
251251
detection_data,
252252
resource_path=None,
253253
from_package=False,
254+
count_detection=True,
254255
):
255256
"""
256257
Get, update or create a DiscoveredLicense object then return it.
@@ -290,7 +291,10 @@ def update_or_create_license_detection(
290291
detection_data=detection_data,
291292
resource_path=resource_path,
292293
)
293-
license_detection.update_with_file_region(file_region)
294+
license_detection.update_with_file_region(
295+
file_region=file_region,
296+
count_detection=count_detection,
297+
)
294298

295299
license_detection.from_package = from_package
296300
return license_detection
@@ -301,6 +305,16 @@ def _clean_license_detection_data(detection_data):
301305
if "reference_matches" in detection_data:
302306
matches = detection_data.pop("reference_matches")
303307
detection_data["matches"] = matches
308+
309+
updated_matches = []
310+
for match_data in detection_data["matches"]:
311+
from_file_path = match_data["from_file"]
312+
if from_file_path:
313+
match_data["from_file"] = from_file_path.removeprefix("codebase/")
314+
315+
updated_matches.append(match_data)
316+
317+
detection_data["matches"] = updated_matches
304318
return detection_data
305319

306320

scanpipe/pipes/scancode.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -647,10 +647,28 @@ def create_codebase_resources(project, scanned_codebase):
647647
license_detections = getattr(scanned_resource, "license_detections", [])
648648
for detection_data in license_detections:
649649
detection_identifier = detection_data.get("identifier")
650-
license_detection = project.discoveredlicenses.get_or_none(
651-
identifier=detection_identifier
650+
pipes.update_or_create_license_detection(
651+
project=project,
652+
detection_data=detection_data,
653+
resource_path=resource_path,
654+
count_detection=False,
652655
)
653-
logger.debug(f"Add {codebase_resource} to {license_detection}")
656+
logger.debug(f"Add {codebase_resource} to {detection_identifier}")
657+
658+
packages = getattr(scanned_resource, "package_data", [])
659+
for package_data in packages:
660+
license_detections = package_data.get("license_detections", [])
661+
license_detections.extend(package_data.get("other_license_detections", []))
662+
for detection_data in license_detections:
663+
detection_identifier = detection_data.get("identifier")
664+
pipes.update_or_create_license_detection(
665+
project=project,
666+
detection_data=detection_data,
667+
resource_path=resource_path,
668+
count_detection=False,
669+
from_package=True,
670+
)
671+
logger.debug(f"Add {codebase_resource} to {detection_identifier}")
654672

655673

656674
def create_discovered_packages(project, scanned_codebase):
@@ -661,6 +679,16 @@ def create_discovered_packages(project, scanned_codebase):
661679
if hasattr(scanned_codebase.attributes, "packages"):
662680
for package_data in scanned_codebase.attributes.packages:
663681
pipes.update_or_create_package(project, package_data)
682+
license_detections = package_data.get("license_detections", [])
683+
license_detections.extend(package_data.get("other_license_detections", []))
684+
685+
for license_detection in license_detections:
686+
pipes.update_or_create_license_detection(
687+
project=project,
688+
detection_data=license_detection,
689+
from_package=True,
690+
count_detection=False,
691+
)
664692

665693

666694
def create_discovered_dependencies(

scanpipe/templates/scanpipe/panels/resource_license_summary.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<div class="column is-half">
44
<nav class="panel is-info">
55
<p class="panel-heading py-2 is-size-6">
6-
Top 10 detected licenses in files
6+
Top 10 detected license expressions for files
77
</p>
88
{% for license_expression, count in resource_license_summary.items %}
99
<a class="panel-block is-align-items-flex-start break-word" href="{{ project_resources_url }}?type=file&detected_license_expression={{ license_expression|default:'_EMPTY_' }}" target="_blank">

scanpipe/templates/scanpipe/tabset/tab_detections.html

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<div class="content">
2+
<table class="table is-bordered is-striped is-narrow is-hoverable is-fullwidth">
3+
<thead>
4+
<tr>
5+
<th>License Expression</th>
6+
<th>Origin Resource Path</th>
7+
<th>Rule URL</th>
8+
<th>Score</th>
9+
<th>Matcher</th>
10+
<th>Match Length</th>
11+
<th>Match Coverage</th>
12+
<th>Rule Relevance</th>
13+
</tr>
14+
</thead>
15+
<tbody>
16+
{% for match in tab_data.fields.matches.value %}
17+
<tr>
18+
<td class="break-all">
19+
{{ match.license_expression }}
20+
</td>
21+
<td class="break-all">
22+
<a href="{% url 'resource_detail' project.slug match.from_file %}">{{ match.from_file }}</a>
23+
</td>
24+
<td class="break-all">
25+
{{ match.rule_url }}
26+
</td>
27+
<td class="break-all">
28+
{{ match.score }}
29+
</td>
30+
<td class="break-all">
31+
{{ match.matcher }}
32+
</td>
33+
<td class="break-all">
34+
{{ match.matched_length }}
35+
</td>
36+
<td class="break-all">
37+
{{ match.match_coverage }}
38+
</td>
39+
<td class="break-all">
40+
{{ match.rule_relevance }}
41+
</td>
42+
</tr>
43+
{% endfor %}
44+
</tbody>
45+
</table>
46+
{% if tab_data.fields.detection_log.value %}
47+
<table class="table is-bordered is-striped is-narrow is-hoverable is-fullwidth">
48+
<thead>
49+
<tr>
50+
<th>Detection Log</th>
51+
</tr>
52+
</thead>
53+
<tbody>
54+
{% for log_entry in tab_data.fields.detection_log.value %}
55+
<tr>
56+
<td class="break-all">
57+
{{ log_entry }}
58+
</td>
59+
</tr>
60+
{% endfor %}
61+
</tbody>
62+
</table>
63+
{% endif %}
64+
<table class="table is-bordered is-striped is-narrow is-hoverable is-fullwidth">
65+
<thead>
66+
<tr>
67+
<th>Resource Path</th>
68+
<th>Start Line</th>
69+
<th>End Line</th>
70+
</tr>
71+
</thead>
72+
<tbody>
73+
{% for file_region in tab_data.fields.file_regions.value %}
74+
<tr>
75+
<td class="break-all">
76+
<a href="{% url 'resource_detail' project.slug file_region.path %}">{{ file_region.path }}</a>
77+
</td>
78+
<td class="break-all">
79+
{{ file_region.start_line }}
80+
</td>
81+
<td class="break-all">
82+
{{ file_region.end_line }}
83+
</td>
84+
</tr>
85+
{% endfor %}
86+
</tbody>
87+
</table>
88+
</div>
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<div class="content">
2+
<table class="table is-bordered is-striped is-narrow is-hoverable is-fullwidth">
3+
<thead>
4+
<tr>
5+
<th>Datafile Paths</th>
6+
</tr>
7+
</thead>
8+
<tbody>
9+
{% for path in tab_data.fields.datafile_paths.value %}
10+
<tr>
11+
<td class="break-all">
12+
<a href="{% url 'resource_detail' project.slug path %}">{{ path }}</a>
13+
</td>
14+
</tr>
15+
{% endfor %}
16+
</tbody>
17+
</table>
18+
<table class="table is-bordered is-striped is-narrow is-hoverable is-fullwidth">
19+
<thead>
20+
<tr>
21+
<th>Datasource IDs</th>
22+
</tr>
23+
</thead>
24+
<tbody>
25+
{% for id in tab_data.fields.datasource_ids.value %}
26+
<tr>
27+
<td class="break-all">
28+
{{ id }}
29+
</td>
30+
</tr>
31+
{% endfor %}
32+
</tbody>
33+
</table>
34+
{% if tab_data.fields.license_detections.value %}
35+
<table class="table is-bordered is-striped is-narrow is-hoverable is-fullwidth">
36+
<thead>
37+
<tr>
38+
<th>License Detections</th>
39+
<th>License Expression</th>
40+
<th>License Expression SPDX</th>
41+
</tr>
42+
</thead>
43+
<tbody>
44+
{% for detection in tab_data.fields.license_detections.value %}
45+
<tr>
46+
<td class="break-all">
47+
<a href="{% url 'license_detail' project.slug detection.identifier %}">{{ detection.identifier }}</a>
48+
</td>
49+
<td class="break-all">
50+
{{ detection.license_expression }}
51+
</td>
52+
<td class="break-all">
53+
{{ detection.license_expression_spdx }}
54+
</td>
55+
</tr>
56+
{% endfor %}
57+
</tbody>
58+
</table>
59+
{% endif %}
60+
{% if tab_data.fields.other_license_detections.value %}
61+
<table class="table is-bordered is-striped is-narrow is-hoverable is-fullwidth">
62+
<thead>
63+
<tr>
64+
<th>License Detections</th>
65+
<th>License Expression</th>
66+
<th>License Expression SPDX</th>
67+
</tr>
68+
</thead>
69+
<tbody>
70+
{% for detection in tab_data.fields.other_license_detections.value %}
71+
<tr>
72+
<td class="break-all">
73+
<a href="{% url 'license_detail' project.slug detection.identifier %}">{{ detection.identifier }}</a>
74+
75+
</td>
76+
<td class="break-all">
77+
{{ detection.license_expression }}
78+
</td>
79+
<td class="break-all">
80+
{{ detection.license_expression_spdx }}
81+
</td>
82+
</tr>
83+
{% endfor %}
84+
</tbody>
85+
</table>
86+
{% endif %}
87+
</div>

0 commit comments

Comments
 (0)