diff --git a/swh/model/identifiers.py b/swh/model/identifiers.py --- a/swh/model/identifiers.py +++ b/swh/model/identifiers.py @@ -720,7 +720,7 @@ """ headers = [ - (b"target_type", metadata["target_type"].encode("ascii")), + (b"target_type", metadata["type"].encode("ascii")), (b"target", str(metadata["target"]).encode()), (b"discovery_date", metadata["discovery_date"].isoformat().encode("ascii")), ( diff --git a/swh/model/model.py b/swh/model/model.py --- a/swh/model/model.py +++ b/swh/model/model.py @@ -22,6 +22,7 @@ directory_identifier, normalize_timestamp, parse_swhid, + raw_extrinsic_metadata_identifier, release_identifier, revision_identifier, snapshot_identifier, @@ -862,7 +863,7 @@ @attr.s(frozen=True, slots=True) -class RawExtrinsicMetadata(BaseModel): +class RawExtrinsicMetadata(HashableObject, BaseModel): object_type: Final = "raw_extrinsic_metadata" # target object @@ -888,6 +889,11 @@ path = attr.ib(type=Optional[bytes], default=None, validator=type_validator()) directory = attr.ib(type=Optional[SWHID], default=None, validator=type_validator()) + id = attr.ib(type=Sha1Git, validator=type_validator(), default=b"") + + def compute_hash(self) -> bytes: + return hash_to_bytes(raw_extrinsic_metadata_identifier(self.to_dict())) + @target.validator def check_target(self, attribute, value): if self.type == MetadataTargetType.ORIGIN: diff --git a/swh/model/tests/test_identifiers.py b/swh/model/tests/test_identifiers.py --- a/swh/model/tests/test_identifiers.py +++ b/swh/model/tests/test_identifiers.py @@ -780,7 +780,7 @@ } self.minimal = { - "target_type": "content", + "type": "content", "target": parse_swhid("swh:1:cnt:568aaf43d83b2c3df8067f3bedbb97d83260be6d"), "discovery_date": datetime.datetime( 2021, 1, 25, 11, 27, 51, tzinfo=datetime.timezone.utc diff --git a/swh/model/tests/test_model.py b/swh/model/tests/test_model.py --- a/swh/model/tests/test_model.py +++ b/swh/model/tests/test_model.py @@ -784,7 +784,9 @@ _origin_url = "https://forge.softwareheritage.org/source/swh-model.git" _dummy_qualifiers = {"origin": "https://example.com", "lines": "42"} _common_metadata_fields = dict( - discovery_date=datetime.datetime.now(tz=datetime.timezone.utc), + discovery_date=datetime.datetime( + 2021, 1, 29, 13, 57, 9, tzinfo=datetime.timezone.utc + ), authority=_metadata_authority, fetcher=_metadata_fetcher, format="json", @@ -825,6 +827,7 @@ assert m.to_dict() == { "type": "origin", "target": _origin_url, + "id": b"\x9b[\x94\xf7\xa7$Dwbc\xf4\xdf\xaf(7G0t-\xd8", **common_fields, } assert RawExtrinsicMetadata.from_dict(m.to_dict()) == m @@ -837,6 +840,7 @@ assert m.to_dict() == { "type": "content", "target": "swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2", + "id": b"\x00\xfd\xe4\x88\xfa\xb5\xac\x7f\x16'\x96\xa8\x10\x9a\xafI\xe9>w\xa7", **common_fields, } assert RawExtrinsicMetadata.from_dict(m.to_dict()) == m