diff --git a/swh/web/misc/badges.py b/swh/web/misc/badges.py --- a/swh/web/misc/badges.py +++ b/swh/web/misc/badges.py @@ -13,20 +13,11 @@ from django.http import HttpRequest, HttpResponse from swh.model.exceptions import ValidationError -from swh.model.identifiers import ( - CONTENT, - DIRECTORY, - ORIGIN, - RELEASE, - REVISION, - SNAPSHOT, - parse_swhid, - swhid, -) +from swh.model.hashutil import hash_to_bytes, hash_to_hex +from swh.model.identifiers import RELEASE, CoreSWHID, ObjectType, QualifiedSWHID from swh.web.common import archive from swh.web.common.exc import BadInputExc, NotFoundExc from swh.web.common.identifiers import resolve_swhid -from swh.web.common.utils import reverse _orange = "#f36a24" _blue = "#0172b2" @@ -35,12 +26,11 @@ _swh_logo_data = None _badge_config = { - CONTENT: {"color": _blue, "title": "Archived source file",}, - DIRECTORY: {"color": _blue, "title": "Archived source tree",}, - ORIGIN: {"color": _orange, "title": "Archived software repository",}, - RELEASE: {"color": _blue, "title": "Archived software release",}, - REVISION: {"color": _blue, "title": "Archived commit",}, - SNAPSHOT: {"color": _blue, "title": "Archived software repository snapshot",}, + "content": {"color": _blue, "title": "Archived source file",}, + "directory": {"color": _blue, "title": "Archived source tree",}, + "release": {"color": _blue, "title": "Archived software release",}, + "revision": {"color": _blue, "title": "Archived commit",}, + "snapshot": {"color": _blue, "title": "Archived software repository snapshot",}, "error": {"color": _red, "title": "An error occurred when generating the badge"}, } @@ -89,32 +79,33 @@ whole_link = None try: - if object_type == ORIGIN: - archive.lookup_origin({"url": object_id}) - right_text = "repository" - whole_link = reverse( - "browse-origin", query_params={"origin_url": object_id} + # when SWHID is provided, object type and id will be parsed + # from it + if object_swhid: + parsed_swhid = QualifiedSWHID.from_string(object_swhid) + object_type = parsed_swhid.object_type.name.lower() + object_id = hash_to_hex(parsed_swhid.object_id) + swh_object = archive.lookup_object(object_type, object_id) + # remove SWHID qualified if any for badge text + right_text = str( + CoreSWHID( + object_type=parsed_swhid.object_type, + object_id=parsed_swhid.object_id, + ) ) else: - # when SWHID is provided, object type and id will be parsed - # from it - if object_swhid: - parsed_swhid = parse_swhid(object_swhid) - object_type = parsed_swhid.object_type - object_id = parsed_swhid.object_id + right_text = str( + CoreSWHID( + object_type=ObjectType[object_type.upper()], + object_id=hash_to_bytes(object_id), + ) + ) swh_object = archive.lookup_object(object_type, object_id) - if object_swhid: - right_text = object_swhid - else: - right_text = swhid(object_type, object_id) - - whole_link = resolve_swhid(right_text)["browse_url"] - # remove SWHID metadata if any for badge text - if object_swhid: - right_text = right_text.split(";")[0] - # use release name for badge text - if object_type == RELEASE: - right_text = "release %s" % swh_object["name"] + + whole_link = resolve_swhid(str(right_text))["browse_url"] + # use release name for badge text + if object_type == RELEASE: + right_text = "release %s" % swh_object["name"] left_text = "archived" except (BadInputExc, ValidationError): right_text = f'invalid {object_type if object_type else "object"} id' diff --git a/swh/web/tests/misc/test_badges.py b/swh/web/tests/misc/test_badges.py --- a/swh/web/tests/misc/test_badges.py +++ b/swh/web/tests/misc/test_badges.py @@ -6,15 +6,8 @@ from corsheaders.middleware import ACCESS_CONTROL_ALLOW_ORIGIN from hypothesis import given -from swh.model.identifiers import ( - CONTENT, - DIRECTORY, - ORIGIN, - RELEASE, - REVISION, - SNAPSHOT, - swhid, -) +from swh.model.hashutil import hash_to_bytes +from swh.model.identifiers import ObjectType, QualifiedSWHID from swh.web.common import archive from swh.web.common.identifiers import resolve_swhid from swh.web.common.utils import reverse @@ -24,8 +17,6 @@ content, directory, invalid_sha1, - new_origin, - origin, release, revision, snapshot, @@ -40,38 +31,32 @@ @given(content()) def test_content_badge(client, content): - _test_badge_endpoints(client, CONTENT, content["sha1_git"]) + _test_badge_endpoints(client, ObjectType.CONTENT, content["sha1_git"]) @given(directory()) def test_directory_badge(client, directory): - _test_badge_endpoints(client, DIRECTORY, directory) - - -@given(origin()) -def test_origin_badge(client, origin): - _test_badge_endpoints(client, ORIGIN, origin["url"]) + _test_badge_endpoints(client, ObjectType.DIRECTORY, directory) @given(release()) def test_release_badge(client, release): - _test_badge_endpoints(client, RELEASE, release) + _test_badge_endpoints(client, ObjectType.RELEASE, release) @given(revision()) def test_revision_badge(client, revision): - _test_badge_endpoints(client, REVISION, revision) + _test_badge_endpoints(client, ObjectType.REVISION, revision) @given(snapshot()) def test_snapshot_badge(client, snapshot): - _test_badge_endpoints(client, SNAPSHOT, snapshot) + _test_badge_endpoints(client, ObjectType.SNAPSHOT, snapshot) @given( unknown_content(), unknown_directory(), - new_origin(), unknown_release(), unknown_revision(), unknown_snapshot(), @@ -81,43 +66,33 @@ client, unknown_content, unknown_directory, - new_origin, unknown_release, unknown_revision, unknown_snapshot, invalid_sha1, ): for object_type, object_id in ( - (CONTENT, unknown_content["sha1_git"]), - (DIRECTORY, unknown_directory), - (ORIGIN, new_origin.url), - (RELEASE, unknown_release), - (REVISION, unknown_revision), - (SNAPSHOT, unknown_snapshot), + (ObjectType.CONTENT, unknown_content["sha1_git"]), + (ObjectType.DIRECTORY, unknown_directory), + (ObjectType.RELEASE, unknown_release), + (ObjectType.REVISION, unknown_revision), + (ObjectType.SNAPSHOT, unknown_snapshot), ): - url_args = {"object_type": object_type, "object_id": object_id} + url_args = {"object_type": object_type.name.lower(), "object_id": object_id} url = reverse("swh-badge", url_args=url_args) resp = check_http_get_response( client, url, status_code=200, content_type="image/svg+xml" ) _check_generated_badge(resp, **url_args, error="not found") - if object_type != ORIGIN: - object_swhid = swhid(object_type, object_id) - url = reverse("swh-badge-swhid", url_args={"object_swhid": object_swhid}) - resp = check_http_get_response( - client, url, status_code=200, content_type="image/svg+xml" - ) - _check_generated_badge(resp, **url_args, error="not found") - for object_type, object_id in ( - (CONTENT, invalid_sha1), - (DIRECTORY, invalid_sha1), - (RELEASE, invalid_sha1), - (REVISION, invalid_sha1), - (SNAPSHOT, invalid_sha1), + (ObjectType.CONTENT, invalid_sha1), + (ObjectType.DIRECTORY, invalid_sha1), + (ObjectType.RELEASE, invalid_sha1), + (ObjectType.REVISION, invalid_sha1), + (ObjectType.SNAPSHOT, invalid_sha1), ): - url_args = {"object_type": object_type, "object_id": object_id} + url_args = {"object_type": object_type.name.lower(), "object_id": object_id} url = reverse("swh-badge", url_args=url_args) resp = check_http_get_response( @@ -125,7 +100,7 @@ ) _check_generated_badge(resp, **url_args, error="invalid id") - object_swhid = f"swh:1:{object_type[:3]}:{object_id}" + object_swhid = f"swh:1:{object_type.value}:{object_id}" url = reverse("swh-badge-swhid", url_args={"object_swhid": object_swhid}) resp = check_http_get_response( client, url, status_code=200, content_type="image/svg+xml" @@ -133,10 +108,10 @@ _check_generated_badge(resp, "", "", error="invalid id") -@given(origin(), release()) -def test_badge_endpoints_have_cors_header(client, origin, release): +@given(release()) +def test_badge_endpoints_have_cors_header(client, release): url = reverse( - "swh-badge", url_args={"object_type": ORIGIN, "object_id": origin["url"]} + "swh-badge", url_args={"object_type": "release", "object_id": release} ) resp = check_http_get_response( @@ -148,7 +123,9 @@ ) assert ACCESS_CONTROL_ALLOW_ORIGIN in resp - release_swhid = swhid(RELEASE, release) + release_swhid = str( + QualifiedSWHID(object_type=ObjectType.RELEASE, object_id=hash_to_bytes(release)) + ) url = reverse("swh-badge-swhid", url_args={"object_swhid": release_swhid}) resp = check_http_get_response( client, @@ -160,20 +137,22 @@ assert ACCESS_CONTROL_ALLOW_ORIGIN in resp -def _test_badge_endpoints(client, object_type, object_id): - url_args = {"object_type": object_type, "object_id": object_id} +def _test_badge_endpoints(client, object_type: ObjectType, object_id: str): + url_args = {"object_type": object_type.name.lower(), "object_id": object_id} url = reverse("swh-badge", url_args=url_args) resp = check_http_get_response( client, url, status_code=200, content_type="image/svg+xml" ) _check_generated_badge(resp, **url_args) - if object_type != ORIGIN: - obj_swhid = swhid(object_type, object_id) - url = reverse("swh-badge-swhid", url_args={"object_swhid": obj_swhid}) - resp = check_http_get_response( - client, url, status_code=200, content_type="image/svg+xml" - ) - _check_generated_badge(resp, **url_args) + + obj_swhid = str( + QualifiedSWHID(object_type=object_type, object_id=hash_to_bytes(object_id)) + ) + url = reverse("swh-badge-swhid", url_args={"object_swhid": obj_swhid}) + resp = check_http_get_response( + client, url, status_code=200, content_type="image/svg+xml" + ) + _check_generated_badge(resp, **url_args) def _check_generated_badge(response, object_type, object_id, error=None): @@ -183,13 +162,15 @@ if not object_type: object_type = "object" - if object_type == ORIGIN and error is None: - link = reverse("browse-origin", query_params={"origin_url": object_id}) - text = "repository" - elif error is None: - text = swhid(object_type, object_id) + if error is None: + text = str( + QualifiedSWHID( + object_type=ObjectType[object_type.upper()], + object_id=hash_to_bytes(object_id), + ) + ) link = resolve_swhid(text)["browse_url"] - if object_type == RELEASE: + if object_type == "release": release = archive.lookup_release(object_id) text = release["name"] elif error == "invalid id":