From d7c16735a46d0ea78f0fa48b16f66dc30bab006d Mon Sep 17 00:00:00 2001
From: Jayesh Velayudhan <jayesh@softwareheritage.org>
Date: Mon, 11 Oct 2021 15:19:26 +0200
Subject: [PATCH] Fix navigation to directory(code) in snapshot view.

Add an extra parameter called directory_url in the snapshot_context.
To fix the missing URL to the code (directory) view in case the
snapshot is missing an origin context.

Fixes T3644
---
 swh/web/browse/snapshot_context.py               | 4 +++-
 swh/web/common/typing.py                         | 2 ++
 swh/web/templates/includes/snapshot-context.html | 4 ++--
 swh/web/tests/browse/test_snapshot_context.py    | 5 ++++-
 4 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/swh/web/browse/snapshot_context.py b/swh/web/browse/snapshot_context.py
index 1439d468e..b01bc9488 100644
--- a/swh/web/browse/snapshot_context.py
+++ b/swh/web/browse/snapshot_context.py
@@ -480,7 +480,7 @@ def get_snapshot_context(
             )
 
         visit_url = reverse("browse-origin-directory", query_params=query_params)
-        visit_info["url"] = visit_url
+        visit_info["url"] = directory_url = visit_url
 
         branches_url = reverse("browse-origin-branches", query_params=query_params)
 
@@ -489,6 +489,7 @@ def get_snapshot_context(
         assert snapshot_id is not None
         branches, releases, aliases = get_snapshot_content(snapshot_id)
         url_args = {"snapshot_id": snapshot_id}
+        directory_url = reverse("browse-snapshot-directory", url_args=url_args)
         branches_url = reverse("browse-snapshot-branches", url_args=url_args)
 
         releases_url = reverse("browse-snapshot-releases", url_args=url_args)
@@ -611,6 +612,7 @@ def get_snapshot_context(
                 revision_info["message_header"] = ""
 
     snapshot_context = SnapshotContext(
+        directory_url=directory_url,
         branch=branch_name,
         branch_alias=branch_name in aliases,
         branches=branches,
diff --git a/swh/web/common/typing.py b/swh/web/common/typing.py
index 04e1616a9..40fd10d10 100644
--- a/swh/web/common/typing.py
+++ b/swh/web/common/typing.py
@@ -131,6 +131,8 @@ class SnapshotContext(TypedDict):
     """common URL arguments when browsing snapshot content"""
     visit_info: Optional[OriginVisitInfo]
     """optional origin visit info associated to the snapshot"""
+    directory_url: Optional[str]
+    """optional root directory URL associated to the snapshot"""
 
 
 class SWHObjectInfo(TypedDict):
diff --git a/swh/web/templates/includes/snapshot-context.html b/swh/web/templates/includes/snapshot-context.html
index c5e790ea2..0a33640dd 100644
--- a/swh/web/templates/includes/snapshot-context.html
+++ b/swh/web/templates/includes/snapshot-context.html
@@ -37,7 +37,7 @@ See top-level LICENSE file for more information
 
 <ul class="nav nav-tabs" id="swh-snapshot-context-nav" style="padding-left: 5px;">
   <li class="nav-item">
-    <a class="nav-link" id="swh-browse-code-nav-link" href="{{ snapshot_context.visit_info.url }}">
+    <a class="nav-link" id="swh-browse-code-nav-link" href="{{ snapshot_context.directory_url }}">
       <i class="mdi mdi-code-tags mdi-fw" aria-hidden="true"></i>
       Code
     </a>
@@ -84,4 +84,4 @@ See top-level LICENSE file for more information
 
 <script>
   swh.browse.initBrowseNavbar();
-</script>
\ No newline at end of file
+</script>
diff --git a/swh/web/tests/browse/test_snapshot_context.py b/swh/web/tests/browse/test_snapshot_context.py
index 54861cac8..6a5b60cdd 100644
--- a/swh/web/tests/browse/test_snapshot_context.py
+++ b/swh/web/tests/browse/test_snapshot_context.py
@@ -130,6 +130,7 @@ def test_get_snapshot_context_no_origin(archive_data, snapshot):
 
         branches_url = reverse("browse-snapshot-branches", url_args=url_args)
         releases_url = reverse("browse-snapshot-releases", url_args=url_args)
+        directory_url = reverse("browse-snapshot-directory", url_args=url_args)
         is_empty = not branches and not releases
         snapshot_swhid = gen_swhid(ObjectType.SNAPSHOT, snapshot)
         snapshot_sizes = archive_data.snapshot_count_branches(snapshot)
@@ -156,6 +157,7 @@ def test_get_snapshot_context_no_origin(archive_data, snapshot):
             snapshot_swhid=snapshot_swhid,
             url_args=url_args,
             visit_info=None,
+            directory_url=directory_url,
         )
 
         if revision_id:
@@ -227,7 +229,7 @@ def test_get_snapshot_context_with_origin(archive_data, origin):
         snapshot_swhid = gen_swhid(ObjectType.SNAPSHOT, snapshot)
         snapshot_sizes = archive_data.snapshot_count_branches(snapshot)
 
-        visit_info["url"] = reverse(
+        visit_info["url"] = directory_url = reverse(
             "browse-origin-directory", query_params=query_params
         )
         visit_info["formatted_date"] = format_utc_iso_date(visit_info["date"])
@@ -257,6 +259,7 @@ def test_get_snapshot_context_with_origin(archive_data, origin):
             snapshot_swhid=snapshot_swhid,
             url_args={},
             visit_info=visit_info,
+            directory_url=directory_url,
         )
 
         if revision_id:
-- 
GitLab