diff --git a/swh/model/identifiers.py b/swh/model/identifiers.py index 485e3bb40eb74894f616ddeb0177cd04aca45568..e59a973e2bc479c09d71f8c0114af744f1d062b8 100644 --- a/swh/model/identifiers.py +++ b/swh/model/identifiers.py @@ -605,7 +605,8 @@ def persistent_identifier(type, object, version=1): Args: type (str): Object's type - object (dict): Object's dict representation + object (dict/bytes/str): Object's dict representation or object + identifier version (int): persistent identifier version (default to 1) Returns: @@ -635,7 +636,11 @@ def persistent_identifier(type, object, version=1): }, } o = _map[type] - _hash = hash_to_hex(object[o['key_id']]) + if isinstance(object, dict): # internal swh representation resolution + _hash = object[o['key_id']] + else: # client passed direct identifier (bytes/str) + _hash = object + _hash = hash_to_hex(_hash) return 'swh:%s:%s:%s' % (version, o['short_name'], _hash) diff --git a/swh/model/tests/test_identifiers.py b/swh/model/tests/test_identifiers.py index b191676a92f30b837eabfabd1460cb217f426854..ed36e1b2840cf676ec2749fd9217157590013757 100644 --- a/swh/model/tests/test_identifiers.py +++ b/swh/model/tests/test_identifiers.py @@ -773,13 +773,29 @@ class SnapshotIdentifier(unittest.TestCase): ) def test_persistent_identifier(self): - _snapshot = {'id': hashutil.hash_to_bytes( - 'c7c108084bc0bf3d81436bf980b46e98bd338453')} - _release = {'id': '22ece559cc7cc2364edc5e5593d63ae8bd229f9f'} - _revision = {'id': '309cf2674ee7a0749978cf8265ab91a60aea0f7d'} - _directory = {'id': 'd198bc9d7a6bcf6db04f476d29314f157507d505'} - _content = {'sha1_git': '94a9ed024d3859793618152ea559a168bbcbb5e2'} + _snapshot_id = hashutil.hash_to_bytes( + 'c7c108084bc0bf3d81436bf980b46e98bd338453') + _release_id = '22ece559cc7cc2364edc5e5593d63ae8bd229f9f' + _revision_id = '309cf2674ee7a0749978cf8265ab91a60aea0f7d' + _directory_id = 'd198bc9d7a6bcf6db04f476d29314f157507d505' + _content_id = '94a9ed024d3859793618152ea559a168bbcbb5e2' + _snapshot = {'id': _snapshot_id} + _release = {'id': _release_id} + _revision = {'id': _revision_id} + _directory = {'id': _directory_id} + _content = {'sha1_git': _content_id} + for full_type, _hash, expected_persistent_id, version in [ + (SNAPSHOT, _snapshot_id, + 'swh:1:snp:c7c108084bc0bf3d81436bf980b46e98bd338453', None), + (RELEASE, _release_id, + 'swh:2:rel:22ece559cc7cc2364edc5e5593d63ae8bd229f9f', 2), + (REVISION, _revision_id, + 'swh:1:rev:309cf2674ee7a0749978cf8265ab91a60aea0f7d', None), + (DIRECTORY, _directory_id, + 'swh:1:dir:d198bc9d7a6bcf6db04f476d29314f157507d505', None), + (CONTENT, _content_id, + 'swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2', 1), (SNAPSHOT, _snapshot, 'swh:1:snp:c7c108084bc0bf3d81436bf980b46e98bd338453', None), (RELEASE, _release, @@ -789,7 +805,7 @@ class SnapshotIdentifier(unittest.TestCase): (DIRECTORY, _directory, 'swh:1:dir:d198bc9d7a6bcf6db04f476d29314f157507d505', None), (CONTENT, _content, - 'swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2', 1) + 'swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2', 1), ]: if version: actual_value = identifiers.persistent_identifier(