diff --git a/swh/web/common/service.py b/swh/web/common/service.py --- a/swh/web/common/service.py +++ b/swh/web/common/service.py @@ -1,4 +1,4 @@ -# Copyright (C) 2015-2019 The Software Heritage developers +# Copyright (C) 2015-2020 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU Affero General Public License version 3, or any later version # See top-level LICENSE file for more information @@ -215,7 +215,7 @@ hashess={'id'}) -def lookup_origin(origin): +def lookup_origin(origin: Dict[str, str]) -> Dict[str, str]: """Return information about the origin matching dict origin. Args: @@ -225,7 +225,21 @@ origin information as dict. """ - origin_info = storage.origin_get(origin) + origins = [origin] + if origin['url']: + # handle case when user provided an origin url with a trailing + # slash while the url in storage does not have it (e.g. GitHub) + if origin['url'].endswith('/'): + origins.append({'url': origin['url'][:-1]}) + # handle case when user provided an origin url without a trailing + # slash while the url in storage have it (e.g. Debian source package) + else: + origins.append({'url': f"{origin['url']}/"}) + # Check all possible origin urls + for orig in origins: + origin_info = storage.origin_get(orig) + if origin_info: + break if not origin_info: msg = 'Origin with url %s not found!' % origin['url'] raise NotFoundExc(msg) diff --git a/swh/web/tests/common/test_service.py b/swh/web/tests/common/test_service.py --- a/swh/web/tests/common/test_service.py +++ b/swh/web/tests/common/test_service.py @@ -1,4 +1,4 @@ -# Copyright (C) 2015-2019 The Software Heritage developers +# Copyright (C) 2015-2020 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU Affero General Public License version 3, or any later version # See top-level LICENSE file for more information @@ -916,3 +916,16 @@ actual_result = service.lookup_missing_hashes(grouped_pids) assert actual_result == {missing_rev, missing_rel, missing_snp} + + +@given(origin()) +def test_lookup_origin_extra_trailing_slash(origin): + origin_info = service.lookup_origin({'url': f"{origin['url']}/"}) + assert origin_info['url'] == origin['url'] + + +def test_lookup_origin_missing_trailing_slash(archive_data): + deb_origin = {'url': 'http://snapshot.debian.org/package/r-base/'} + archive_data.origin_add_one(deb_origin) + origin_info = service.lookup_origin({'url': deb_origin['url'][:-1]}) + assert origin_info['url'] == deb_origin['url']