diff --git a/swh/indexer/origin_head.py b/swh/indexer/origin_head.py --- a/swh/indexer/origin_head.py +++ b/swh/indexer/origin_head.py @@ -30,6 +30,8 @@ def index(self, origin): origin_id = origin['id'] latest_snapshot = self.storage.snapshot_get_latest(origin_id) + if latest_snapshot is None: + return None method = getattr(self, '_try_get_%s_head' % origin['type'], None) if method is None: method = self._try_get_head_generic @@ -46,10 +48,9 @@ def _try_get_vcs_head(self, snapshot): try: - if isinstance(snapshot, dict): - branches = snapshot['branches'] - if branches[b'HEAD']['target_type'] == 'revision': - return branches[b'HEAD']['target'] + branches = snapshot['branches'] + if branches[b'HEAD']['target_type'] == 'revision': + return branches[b'HEAD']['target'] except KeyError: return None @@ -111,8 +112,7 @@ def _try_get_head_generic(self, snapshot): # Works on 'deposit', 'svn', and 'pypi'. try: - if isinstance(snapshot, dict): - branches = snapshot['branches'] + branches = snapshot['branches'] except KeyError: return None else: diff --git a/swh/indexer/tests/test_origin_head.py b/swh/indexer/tests/test_origin_head.py --- a/swh/indexer/tests/test_origin_head.py +++ b/swh/indexer/tests/test_origin_head.py @@ -55,6 +55,15 @@ b'\xd7}\xac\xefrm', 'origin_id': origin_id}]) + def test_vcs_missing_snapshot(self): + self.indexer.storage.origin_add([{ + 'type': 'git', + 'url': 'https://github.com/SoftwareHeritage/swh-indexer', + }]) + self.indexer.run( + ['git+https://github.com/SoftwareHeritage/swh-indexer']) + self.assertEqual(self.indexer.results, []) + def test_ftp(self): self.indexer.run( ['ftp+rsync://ftp.gnu.org/gnu/3dldf']) @@ -65,6 +74,15 @@ b'\xcc\x1a\xb4`\x8c\x8by', 'origin_id': origin_id}]) + def test_ftp_missing_snapshot(self): + self.indexer.storage.origin_add([{ + 'type': 'ftp', + 'url': 'rsync://ftp.gnu.org/gnu/foobar', + }]) + self.indexer.run( + ['ftp+rsync://ftp.gnu.org/gnu/foobar']) + self.assertEqual(self.indexer.results, []) + def test_deposit(self): self.indexer.run( ['deposit+https://forge.softwareheritage.org/source/' @@ -76,6 +94,15 @@ b'\xa6\xe9\x99\xb1\x9e]q\xeb', 'origin_id': origin_id}]) + def test_deposit_missing_snapshot(self): + self.indexer.storage.origin_add([{ + 'type': 'deposit', + 'url': 'https://forge.softwareheritage.org/source/foobar', + }]) + self.indexer.run( + ['deposit+https://forge.softwareheritage.org/source/foobar']) + self.assertEqual(self.indexer.results, []) + def test_pypi(self): self.indexer.run( ['pypi+https://pypi.org/project/limnoria/'])