diff --git a/swh/web/api/views/origin.py b/swh/web/api/views/origin.py --- a/swh/web/api/views/origin.py +++ b/swh/web/api/views/origin.py @@ -473,3 +473,39 @@ enrich_fn=enrich_origin, request=request, ) + + +@api_route( + r"/origin/(?P.+)" "/raw-extrinsic-metadata", + "api-origin-raw-extrinsic-metadata", +) +@api_doc("/origin/raw-extrinsic-metadata/") +@format_docstring() +def api_origin_raw_extrinsic_metadata(request, origin_url): + """ + .. http:get:: /api/1/origin/(origin_url)/raw-extrinsic-metadata + + Get raw extrinsic metadata of a software origin + + :param string origin_url: the origin url + + :>json string ???: raw extrinsic metadata field of the origin + + {common_headers} + + :statuscode 200: no error + :statuscode 404: requested origin can not be found in the archive + + **Example:** + + .. parsed-literal:: + + :swh_web_api:`origin/https://github.com/python/cpython/raw-extrinsic-metadata` + """ + return api_lookup( + archive.lookup_origin_raw_extrinsic_metadata, + origin_url, + notfound_msg=f"Origin with url {origin_url} not found", + enrich_fn=enrich_origin, + request=request, + ) diff --git a/swh/web/common/archive.py b/swh/web/common/archive.py --- a/swh/web/common/archive.py +++ b/swh/web/common/archive.py @@ -12,7 +12,12 @@ from swh.model import hashutil from swh.model.identifiers import CONTENT, DIRECTORY, RELEASE, REVISION, SNAPSHOT -from swh.model.model import OriginVisit, Revision +from swh.model.model import ( + MetadataAuthority, + MetadataAuthorityType, + OriginVisit, + Revision, +) from swh.storage.algos import diff, revisions_walker from swh.storage.algos.origin import origin_get_latest_visit_status from swh.storage.algos.snapshot import snapshot_get_latest, snapshot_resolve_alias @@ -414,6 +419,35 @@ return result +def lookup_origin_raw_extrinsic_metadata(origin_url: str) -> Dict[str, Any]: + """Return extrinsic metadata for origin whose origin matches given origin. + + Args: + origin_url: origin url + + Raises: + NotFoundExc when the origin is not found + + Returns: + origin metadata. + + """ + origins = [origin_url] + origin_info = storage.origin_get(origins)[0] + # assert 0 -> for using pytest --pdb + if not origin_info: + raise NotFoundExc(f"Origin with url {origin_url} not found!") + SWH_AUTHORITY = MetadataAuthority( + type=MetadataAuthorityType.REGISTRY, url="https://softwareheritage.org/" + ) # Fixing to SWH_AUTHORITY for now + match = _first_element( + storage.raw_extrinsic_metadata_get( + "origin", origin_url, authority=SWH_AUTHORITY + ) + ) + return match + + def _to_sha1_bin(sha1_hex): _, sha1_git_bin = query.parse_hash_with_algorithms_or_throws( sha1_hex, ["sha1"], "Only sha1_git is supported." # HACK: sha1_git really diff --git a/swh/web/tests/api/views/test_origin.py b/swh/web/tests/api/views/test_origin.py --- a/swh/web/tests/api/views/test_origin.py +++ b/swh/web/tests/api/views/test_origin.py @@ -676,6 +676,18 @@ assert rv.data == expected_data +@given(origin()) +def test_api_origin_raw_extrinsic_metadata(api_client, origin): + url = reverse( + "api-origin-raw-extrinsic-metadata", url_args={"origin_url": origin["url"]} + ) + # assert 0 + rv = check_api_get_responses(api_client, url, status_code=200) + + expected_data = {ORIGIN_METADATA_KEY: ORIGIN_METADATA_VALUE} + assert rv.data == expected_data + + def test_api_origin_metadata_search_invalid(api_client, mocker): mock_idx_storage = mocker.patch("swh.web.common.archive.idx_storage") url = reverse("api-1-origin-metadata-search")