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 @@ -3,7 +3,7 @@ # License: GNU Affero General Public License version 3, or any later version # See top-level LICENSE file for more information -from typing import Any, Dict, Iterable, List, Optional, cast +from typing import Any, Dict, Iterable, List, Optional from urllib.parse import quote, unquote from typing_extensions import TypedDict @@ -20,8 +20,9 @@ REVISION, SNAPSHOT, SWHID, + ObjectType, + QualifiedSWHID, parse_swhid, - swhid, ) from swh.web.common import archive from swh.web.common.exc import BadInputExc @@ -63,11 +64,21 @@ generate a valid identifier """ try: - obj_swhid = swhid( - object_type, - object_id, - scheme_version, - cast(Dict[str, Any], {k: v for k, v in metadata.items() if v is not None}), + decoded_object_type = ObjectType[object_type.upper()] + except KeyError: + raise BadInputExc("Invalid object type (%s) for SWHID." % (object_type,)) + try: + decoded_object_id = hash_to_bytes(object_id) + except ValueError: + raise BadInputExc("Invalid object_id (%s) for SWHID." % (object_id,)) + try: + obj_swhid = str( + QualifiedSWHID( + object_type=decoded_object_type, + object_id=decoded_object_id, + scheme_version=scheme_version, + **metadata, + ) ) except ValidationError as e: raise BadInputExc("Invalid object (%s) for SWHID. %s" % (object_id, e)) diff --git a/swh/web/tests/browse/views/test_content.py b/swh/web/tests/browse/views/test_content.py --- a/swh/web/tests/browse/views/test_content.py +++ b/swh/web/tests/browse/views/test_content.py @@ -448,7 +448,7 @@ directory_file["checksums"]["sha1_git"], metadata={ "origin": origin["url"], - "visit": gen_swhid(SNAPSHOT, snapshot), + "visit": gen_swhid(SNAPSHOT, snapshot["id"]), "anchor": gen_swhid(REVISION, branch_info["revision"]), "path": f"/{directory_file['name']}", }, @@ -460,7 +460,7 @@ directory, metadata={ "origin": origin["url"], - "visit": gen_swhid(SNAPSHOT, snapshot), + "visit": gen_swhid(SNAPSHOT, snapshot["id"]), "anchor": gen_swhid(REVISION, branch_info["revision"]), }, ) @@ -469,11 +469,16 @@ rev_swhid = gen_swhid( REVISION, branch_info["revision"], - metadata={"origin": origin["url"], "visit": gen_swhid(SNAPSHOT, snapshot),}, + metadata={ + "origin": origin["url"], + "visit": gen_swhid(SNAPSHOT, snapshot["id"]), + }, ) assert_contains(resp, rev_swhid) - snp_swhid = gen_swhid(SNAPSHOT, snapshot, metadata={"origin": origin["url"],},) + snp_swhid = gen_swhid( + SNAPSHOT, snapshot["id"], metadata={"origin": origin["url"],}, + ) assert_contains(resp, snp_swhid) @@ -517,7 +522,7 @@ directory_file["checksums"]["sha1_git"], metadata={ "origin": origin["url"], - "visit": gen_swhid(SNAPSHOT, snapshot), + "visit": gen_swhid(SNAPSHOT, snapshot["id"]), "anchor": gen_swhid(RELEASE, release_info["id"]), "path": f"/{directory_file['name']}", }, @@ -529,7 +534,7 @@ release_info["directory"], metadata={ "origin": origin["url"], - "visit": gen_swhid(SNAPSHOT, snapshot), + "visit": gen_swhid(SNAPSHOT, snapshot["id"]), "anchor": gen_swhid(RELEASE, release_info["id"]), }, ) @@ -538,18 +543,26 @@ rev_swhid = gen_swhid( REVISION, release_info["target"], - metadata={"origin": origin["url"], "visit": gen_swhid(SNAPSHOT, snapshot),}, + metadata={ + "origin": origin["url"], + "visit": gen_swhid(SNAPSHOT, snapshot["id"]), + }, ) assert_contains(resp, rev_swhid) rel_swhid = gen_swhid( RELEASE, release_info["id"], - metadata={"origin": origin["url"], "visit": gen_swhid(SNAPSHOT, snapshot),}, + metadata={ + "origin": origin["url"], + "visit": gen_swhid(SNAPSHOT, snapshot["id"]), + }, ) assert_contains(resp, rel_swhid) - snp_swhid = gen_swhid(SNAPSHOT, snapshot, metadata={"origin": origin["url"],},) + snp_swhid = gen_swhid( + SNAPSHOT, snapshot["id"], metadata={"origin": origin["url"],}, + ) assert_contains(resp, snp_swhid) diff --git a/swh/web/tests/browse/views/test_directory.py b/swh/web/tests/browse/views/test_directory.py --- a/swh/web/tests/browse/views/test_directory.py +++ b/swh/web/tests/browse/views/test_directory.py @@ -240,7 +240,7 @@ directory_subdir["target"], metadata={ "origin": origin["url"], - "visit": gen_swhid(SNAPSHOT, snapshot), + "visit": gen_swhid(SNAPSHOT, snapshot["id"]), "anchor": gen_swhid(REVISION, branch_info["revision"]), "path": "/", }, @@ -250,11 +250,16 @@ rev_swhid = gen_swhid( REVISION, branch_info["revision"], - metadata={"origin": origin["url"], "visit": gen_swhid(SNAPSHOT, snapshot),}, + metadata={ + "origin": origin["url"], + "visit": gen_swhid(SNAPSHOT, snapshot["id"]), + }, ) assert_contains(resp, rev_swhid) - snp_swhid = gen_swhid(SNAPSHOT, snapshot, metadata={"origin": origin["url"],},) + snp_swhid = gen_swhid( + SNAPSHOT, snapshot["id"], metadata={"origin": origin["url"],}, + ) assert_contains(resp, snp_swhid) @@ -299,7 +304,7 @@ directory_subdir["target"], metadata={ "origin": origin["url"], - "visit": gen_swhid(SNAPSHOT, snapshot), + "visit": gen_swhid(SNAPSHOT, snapshot["id"]), "anchor": gen_swhid(RELEASE, release_info["id"]), "path": "/", }, @@ -309,18 +314,26 @@ rev_swhid = gen_swhid( REVISION, release_info["target"], - metadata={"origin": origin["url"], "visit": gen_swhid(SNAPSHOT, snapshot),}, + metadata={ + "origin": origin["url"], + "visit": gen_swhid(SNAPSHOT, snapshot["id"]), + }, ) assert_contains(resp, rev_swhid) rel_swhid = gen_swhid( RELEASE, release_info["id"], - metadata={"origin": origin["url"], "visit": gen_swhid(SNAPSHOT, snapshot),}, + metadata={ + "origin": origin["url"], + "visit": gen_swhid(SNAPSHOT, snapshot["id"]), + }, ) assert_contains(resp, rel_swhid) - snp_swhid = gen_swhid(SNAPSHOT, snapshot, metadata={"origin": origin["url"],},) + snp_swhid = gen_swhid( + SNAPSHOT, snapshot["id"], metadata={"origin": origin["url"],}, + ) assert_contains(resp, snp_swhid) 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 @@ -182,7 +182,7 @@ directory_file["checksums"]["sha1_git"], metadata={ "origin": origin["url"], - "visit": gen_swhid(SNAPSHOT, snapshot), + "visit": gen_swhid(SNAPSHOT, snapshot["id"]), "anchor": gen_swhid(REVISION, revision), }, )