From c66b53f62b79264bba91293856e35cd9d21f4901 Mon Sep 17 00:00:00 2001 From: Jay Zhang Date: Wed, 4 Dec 2019 11:31:31 +0800 Subject: [PATCH 1/2] Fix export an empty maintenance would crash Now if the report is empty and last_updated field is None, then while exporting report, current datetime will be used to aviod crashing. --- CHANGELOG.md | 3 ++- pubtools/pulplib/_impl/model/convert.py | 3 +++ setup.py | 2 +- tests/maintenance/test_maintenance.py | 15 +++++++++++++-- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fa373f31..a045ca8a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] -- n/a +### Fixed +- Fixed a bug that export an empty maintenance report would crash. ## [2.3.1] - 2019-10-03 diff --git a/pubtools/pulplib/_impl/model/convert.py b/pubtools/pulplib/_impl/model/convert.py index d241dee6..009209b5 100644 --- a/pubtools/pulplib/_impl/model/convert.py +++ b/pubtools/pulplib/_impl/model/convert.py @@ -32,4 +32,7 @@ def read_timestamp(value): def write_timestamp(value): + # value could be None if the report is empty + if value is None: + value = datetime.datetime.utcnow() return value.strftime("%Y-%m-%dT%H:%M:%SZ") diff --git a/setup.py b/setup.py index 202ce918..d685d13b 100644 --- a/setup.py +++ b/setup.py @@ -21,7 +21,7 @@ def get_requirements(): setup( name="pubtools-pulplib", - version="2.3.1", + version="2.3.2", packages=find_packages(exclude=["tests"]), package_data={"pubtools.pulplib._impl.schema": ["*.yaml"]}, url="https://github.com/release-engineering/pubtools-pulplib", diff --git a/tests/maintenance/test_maintenance.py b/tests/maintenance/test_maintenance.py index 15049c02..95f47ac4 100644 --- a/tests/maintenance/test_maintenance.py +++ b/tests/maintenance/test_maintenance.py @@ -50,7 +50,7 @@ def test_load_invalid_report_raise_exception(): def test_create_export_report(): data = { - "last_updated": "2019-08-15T14:21:12Z", # invalid timestamp + "last_updated": "2019-08-15T14:21:12Z", "last_updated_by": "pubtools.pulplib", "repos": { "repo1": { @@ -70,7 +70,7 @@ def test_create_export_report(): def test_report_add_remove(): data = { - "last_updated": "2019-08-15T14:21:12Z", # invalid timestamp + "last_updated": "2019-08-15T14:21:12Z", "last_updated_by": "pubtools.pulplib", "repos": { "repo1": { @@ -92,3 +92,14 @@ def test_report_add_remove(): assert len(report.entries) == 1 assert report.last_updated_by == "jazhang" + + +def test_export_empty_report(): + # create an empty report with out data passed + report = MaintenanceReport() + assert report.last_updated is None + + # export empty report shouldn't have problem + data = report._export_dict() + # exported report should have a timestamp + assert datetime.datetime.strptime(data["last_updated"], "%Y-%m-%dT%H:%M:%SZ") From c15616919c379b8b70ff6a6edc3870675c89fa7e Mon Sep 17 00:00:00 2001 From: Jay Zhang Date: Fri, 6 Dec 2019 15:01:34 +0800 Subject: [PATCH 2/2] better comment --- pubtools/pulplib/_impl/model/convert.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubtools/pulplib/_impl/model/convert.py b/pubtools/pulplib/_impl/model/convert.py index 009209b5..e5f8c957 100644 --- a/pubtools/pulplib/_impl/model/convert.py +++ b/pubtools/pulplib/_impl/model/convert.py @@ -32,7 +32,7 @@ def read_timestamp(value): def write_timestamp(value): - # value could be None if the report is empty + # defaults to current time if value is None if value is None: value = datetime.datetime.utcnow() return value.strftime("%Y-%m-%dT%H:%M:%SZ")