Skip to content

Commit

Permalink
Add basic diff view to compare codebase resources #659
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Druez <[email protected]>
  • Loading branch information
tdruez committed Apr 7, 2023
1 parent 0b081f6 commit 4138181
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
4 changes: 3 additions & 1 deletion scanpipe/templates/scanpipe/codebase_relation.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
{{ relation.match_type }}
{% if relation.match_type == "path" %}
(score: {{ relation.extra_data.path_score }})
<a href="/project/{{ project.uuid }}/resources/diff/?pk_a={{ resource.path }}&pk_b={{ relation.to_resource.path }}" target="_blank">diff</a>
{% endif %}
</td>
<td class="break-all" style="min-width: 200px;">
Expand All @@ -54,7 +55,7 @@
<a class="modal-button is-black-link" data-target="file-content-modal" data-raw_url="{{ resource.get_raw_url }}" aria-haspopup="true">
<i class="far fa-file"></i>
</a>
{{ resource.path }}{% if resource.is_dir %}/{% endif %}
{{ resource.path }}
</td>
<td>
</td>
Expand Down Expand Up @@ -94,6 +95,7 @@
{{ relation.match_type }}
{% if relation.match_type == "path" %}
(score: {{ relation.extra_data.path_score }})
<a href="/project/{{ project.uuid }}/resources/diff/?pk_a={{ resource.path }}&pk_b={{ relation.from_resource.path }}" target="_blank">diff</a>
{% endif %}
</td>
<td class="break-all" style="min-width: 200px;">
Expand Down
5 changes: 5 additions & 0 deletions scanpipe/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
views.CodebaseResourceRawView.as_view(),
name="resource_raw",
),
path(
"project/<uuid:uuid>/resources/diff/",
views.codebase_resource_diff_view,
name="resource_diff",
),
path(
"project/<uuid:uuid>/resources/<path:path>/",
views.CodebaseResourceDetailsView.as_view(),
Expand Down
19 changes: 19 additions & 0 deletions scanpipe/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# ScanCode.io is a free software code scanning tool from nexB Inc. and others.
# Visit https://github.com/nexB/scancode.io for support and download.

import difflib
import io
import json
from collections import Counter
Expand All @@ -31,6 +32,7 @@
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import FileResponse
from django.http import Http404
from django.http import HttpResponse
from django.http import JsonResponse
from django.shortcuts import get_object_or_404
from django.shortcuts import redirect
Expand Down Expand Up @@ -1023,6 +1025,23 @@ def get_context_data(self, **kwargs):
return context


@conditional_login_required
def codebase_resource_diff_view(request, uuid):
project = get_object_or_404(Project, uuid=uuid)

project_files = project.codebaseresources.files()
pk_a = request.GET.get("pk_a")
pk_b = request.GET.get("pk_b")
resource_a = get_object_or_404(project_files, path=pk_a)
resource_b = get_object_or_404(project_files, path=pk_b)

text_a = resource_a.location_path.read_text()
text_b = resource_b.location_path.read_text()
html = difflib.HtmlDiff().make_file(text_a.split("\n"), text_b.split("\n"))

return HttpResponse(html)


class DiscoveredPackageDetailsView(
ConditionalLoginRequired,
ProjectRelatedViewMixin,
Expand Down

0 comments on commit 4138181

Please sign in to comment.