diff --git a/swh/web/browse/views/content.py b/swh/web/browse/views/content.py --- a/swh/web/browse/views/content.py +++ b/swh/web/browse/views/content.py @@ -333,7 +333,7 @@ language=content_data["language"], licenses=content_data["licenses"], root_directory=root_dir, - path=f"/{path}" if path else "/", + path=f"/{path}" if path else "", filename=filename or "", directory=directory_id, directory_url=directory_url, @@ -345,6 +345,7 @@ swhids_info = get_swhids_info( [SWHObjectInfo(object_type=CONTENT, object_id=content_checksums["sha1_git"])], + snapshot_context, extra_context=content_metadata, ) diff --git a/swh/web/common/identifiers.py b/swh/web/common/identifiers.py --- a/swh/web/common/identifiers.py +++ b/swh/web/common/identifiers.py @@ -289,7 +289,7 @@ ) path = None if extra_context and "path" in extra_context: - path = extra_context["path"] + path = extra_context["path"] or "/" if "filename" in extra_context and object_type == CONTENT: path += extra_context["filename"] if path: diff --git a/swh/web/tests/browse/views/test_identifiers.py b/swh/web/tests/browse/views/test_identifiers.py --- a/swh/web/tests/browse/views/test_identifiers.py +++ b/swh/web/tests/browse/views/test_identifiers.py @@ -3,10 +3,22 @@ # License: GNU Affero General Public License version 3, or any later version # See top-level LICENSE file for more information +import random + from hypothesis import given +from swh.model.identifiers import CONTENT, REVISION, SNAPSHOT +from swh.web.common.identifiers import get_swh_persistent_id from swh.web.common.utils import reverse -from swh.web.tests.strategies import content, directory, revision, release, snapshot +from swh.web.tests.django_asserts import assert_contains +from swh.web.tests.strategies import ( + content, + directory, + origin, + revision, + release, + snapshot, +) swh_id_prefix = "swh:1:" @@ -158,3 +170,37 @@ url = reverse("browse-swh-id", url_args={"swh_id": swh_id}) resp = client.get(url) assert resp.status_code == 400 + + +@given(origin()) +def test_legacy_swhid_browse(archive_data, client, origin): + snapshot = archive_data.snapshot_get_latest(origin["url"]) + revision = archive_data.snapshot_get_head(snapshot) + directory = archive_data.revision_get(revision)["directory"] + directory_content = archive_data.directory_ls(directory) + directory_file = random.choice( + [e for e in directory_content if e["type"] == "file"] + ) + legacy_swhid = get_swh_persistent_id( + CONTENT, + directory_file["checksums"]["sha1_git"], + metadata={"origin": origin["url"]}, + ) + + url = reverse("browse-swh-id", url_args={"swh_id": legacy_swhid}) + resp = client.get(url) + assert resp.status_code == 302 + + resp = client.get(resp["location"]) + + swhid = get_swh_persistent_id( + CONTENT, + directory_file["checksums"]["sha1_git"], + metadata={ + "origin": origin["url"], + "visit": get_swh_persistent_id(SNAPSHOT, snapshot), + "anchor": get_swh_persistent_id(REVISION, revision), + }, + ) + + assert_contains(resp, swhid)