diff --git a/swh/loader/package/functional/loader.py b/swh/loader/package/functional/loader.py --- a/swh/loader/package/functional/loader.py +++ b/swh/loader/package/functional/loader.py @@ -6,7 +6,9 @@ import json import requests -from typing import Dict, Optional, Any +from typing import Dict, Optional, Any, Mapping + +from swh.model import hashutil from swh.model.model import ( Sha1Git, Revision, RevisionType @@ -26,10 +28,16 @@ def __init__(self, url, origin=None): super().__init__(url=url, origin=origin) - self.sources = self._retrieve_sources()['sources'] + s = self._retrieve_sources() + self.sources = s['sources'] self.provider_url = url + self.revision = s['revision'] def _retrieve_sources(self) -> Dict[str, Any]: + """Download the sources.json file from self.url, parse it and return + the Dict. + + """ response = requests.get(self.url, allow_redirects=True) if response.status_code != 200: @@ -62,6 +70,17 @@ return rev_id return None + def hook_branches( + self, branches: Dict[bytes, Mapping[str, Any]]) \ + -> Dict[bytes, Mapping[str, Any]]: + branches.update({ + b'evaluation': { + 'target_type': 'revision', + 'target': hashutil.hash_to_bytes(self.revision) + } + }) + return branches + def build_revision(self, a_metadata: Dict, uncompressed_path: str, directory: Sha1Git) -> Optional[Revision]: return Revision( diff --git a/swh/loader/package/functional/tests/data/https_nix-community.github.io/nixpkgs-swh_sources.json b/swh/loader/package/functional/tests/data/https_nix-community.github.io/nixpkgs-swh_sources.json --- a/swh/loader/package/functional/tests/data/https_nix-community.github.io/nixpkgs-swh_sources.json +++ b/swh/loader/package/functional/tests/data/https_nix-community.github.io/nixpkgs-swh_sources.json @@ -9,5 +9,6 @@ "url": [ "https://example.com/file.txt" ] } ], - "version": 1 + "version": 1, + "revision": "cc4e04c26672dd74e5fd0fecb78b435fb55368f7" } diff --git a/swh/loader/package/functional/tests/data/https_nix-community.github.io/nixpkgs-swh_sources.json_visit1 b/swh/loader/package/functional/tests/data/https_nix-community.github.io/nixpkgs-swh_sources.json_visit1 --- a/swh/loader/package/functional/tests/data/https_nix-community.github.io/nixpkgs-swh_sources.json_visit1 +++ b/swh/loader/package/functional/tests/data/https_nix-community.github.io/nixpkgs-swh_sources.json_visit1 @@ -13,5 +13,6 @@ "url": [ "https://example.com/file.txt" ] } ], - "version": 1 + "version": 1, + "revision": "602140776b2ce6c9159bcf52ada73a297c063d5e" } diff --git a/swh/loader/package/functional/tests/test_functional.py b/swh/loader/package/functional/tests/test_functional.py --- a/swh/loader/package/functional/tests/test_functional.py +++ b/swh/loader/package/functional/tests/test_functional.py @@ -10,7 +10,7 @@ ) from swh.loader.package.tests.common import ( - get_stats + get_stats, check_snapshot ) sources_url = 'https://nix-community.github.io/nixpkgs-swh/sources.json' @@ -101,3 +101,27 @@ assert loader.resolve_revision_from(known_artifacts, metadata) == 'id1' metadata = {'url': 'url3'} assert loader.resolve_revision_from(known_artifacts, metadata) == None # noqa + + +def test_evaluation_branch(swh_config, requests_mock_datadir): + loader = FunctionalLoader(sources_url, 'nixpkgs') + res = loader.load() + assert res['status'] == 'eventful' + + expected_branches = { + 'https://github.com/owner-1/repository-1/revision-1.tgz': { + 'target': '488ad4e7b8e2511258725063cf43a2b897c503b4', + 'target_type': 'revision', + }, + 'evaluation': { + 'target': 'cc4e04c26672dd74e5fd0fecb78b435fb55368f7', + 'target_type': 'revision', + }, + } + + expected_snapshot = { + 'id': '0c5881c74283793ebe9a09a105a9381e41380383', + 'branches': expected_branches, + } + + check_snapshot(expected_snapshot, storage=loader.storage) diff --git a/swh/loader/package/loader.py b/swh/loader/package/loader.py --- a/swh/loader/package/loader.py +++ b/swh/loader/package/loader.py @@ -214,6 +214,15 @@ uncompress(a_path, dest=uncompressed_path) return uncompressed_path + def hook_branches( + self, branches: Dict[bytes, Mapping[str, Any]]) \ + -> Dict[bytes, Mapping[str, Any]]: + """This hook allows to modify the branches dict before creating the + snapshot. + + """ + return branches + def load(self) -> Dict: """Load for a specific origin the associated contents. @@ -326,7 +335,7 @@ } snapshot_data = { - 'branches': branches + 'branches': self.hook_branches(branches) } logger.debug('snapshot: %s', snapshot_data)