From 41381816e04365c886ae3dc156615aead7cebfcf Mon Sep 17 00:00:00 2001 From: Thomas Druez Date: Fri, 7 Apr 2023 15:59:48 +0400 Subject: [PATCH] Add basic diff view to compare codebase resources #659 Signed-off-by: Thomas Druez --- .../templates/scanpipe/codebase_relation.html | 4 +++- scanpipe/urls.py | 5 +++++ scanpipe/views.py | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/scanpipe/templates/scanpipe/codebase_relation.html b/scanpipe/templates/scanpipe/codebase_relation.html index 653235378..eaa0cd22e 100644 --- a/scanpipe/templates/scanpipe/codebase_relation.html +++ b/scanpipe/templates/scanpipe/codebase_relation.html @@ -41,6 +41,7 @@ {{ relation.match_type }} {% if relation.match_type == "path" %} (score: {{ relation.extra_data.path_score }}) + diff {% endif %} @@ -54,7 +55,7 @@ - {{ resource.path }}{% if resource.is_dir %}/{% endif %} + {{ resource.path }} @@ -94,6 +95,7 @@ {{ relation.match_type }} {% if relation.match_type == "path" %} (score: {{ relation.extra_data.path_score }}) + diff {% endif %} diff --git a/scanpipe/urls.py b/scanpipe/urls.py index b634835ff..b3305975d 100644 --- a/scanpipe/urls.py +++ b/scanpipe/urls.py @@ -31,6 +31,11 @@ views.CodebaseResourceRawView.as_view(), name="resource_raw", ), + path( + "project//resources/diff/", + views.codebase_resource_diff_view, + name="resource_diff", + ), path( "project//resources//", views.CodebaseResourceDetailsView.as_view(), diff --git a/scanpipe/views.py b/scanpipe/views.py index 8a2ccd405..3aed75604 100644 --- a/scanpipe/views.py +++ b/scanpipe/views.py @@ -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 @@ -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 @@ -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,