diff --git a/swh/loader/cvs/tasks.py b/swh/loader/cvs/tasks.py index bd0bfc8..63867dc 100644 --- a/swh/loader/cvs/tasks.py +++ b/swh/loader/cvs/tasks.py @@ -1,51 +1,54 @@ # Copyright (C) 2015-2021 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 from datetime import datetime from typing import Optional from celery import shared_task import iso8601 from .loader import CvsLoader def convert_to_datetime(date: Optional[str]) -> Optional[datetime]: + if date is None: + return None try: + assert isinstance(date, str) return iso8601.parse_date(date) except Exception: return None @shared_task(name=__name__ + ".LoadCvsRepository") def load_cvs( *, url: str, origin_url: Optional[str] = None, destination_path: Optional[str] = None, swh_revision: Optional[str] = None, visit_date: Optional[str] = None, ): """Import a CVS repository Args: - url: (mandatory) CVS's repository url to ingest data from - origin_url: Optional original url override to use as origin reference in the archive. If not provided, "url" is used as origin. - destination_path: (optional) root directory to locally retrieve svn's data - swh_revision: (optional) extra revision hex to start from. See swh.loader.svn.CvsLoader.process docstring - visit_date: Optional date to override the visit date """ loader = CvsLoader.from_configfile( url=url, origin_url=origin_url, destination_path=destination_path, swh_revision=swh_revision, visit_date=convert_to_datetime(visit_date), ) return loader.load() diff --git a/swh/loader/cvs/tests/test_tasks.py b/swh/loader/cvs/tests/test_tasks.py new file mode 100644 index 0000000..332c43e --- /dev/null +++ b/swh/loader/cvs/tests/test_tasks.py @@ -0,0 +1,25 @@ +# Copyright (C) 2019-2021 The Software Heritage developers +# See the AUTHORS file at the top-level directory of this distribution +# License: GNU General Public License version 3, or any later version +# See top-level LICENSE file for more information + +from datetime import datetime, timezone + +import pytest + +from swh.loader.cvs.tasks import convert_to_datetime + + +@pytest.mark.parametrize( + "date,expected_result", + [ + (None, None), + ( + "2021-11-23 09:41:02.434195+00:00", + datetime(2021, 11, 23, 9, 41, 2, 434195, tzinfo=timezone.utc), + ), + ("23112021", None,), # failure to parse + ], +) +def test_convert_to_datetime(date, expected_result): + assert convert_to_datetime(date) == expected_result