From 9d7922e19ba10a25e7751187d278e5cdec7b47e5 Mon Sep 17 00:00:00 2001 From: Jay Zhang Date: Wed, 11 Dec 2019 12:31:01 +0800 Subject: [PATCH 1/4] give last_updated_by a valid default value if it's None when export If user call set-maintenance but doesn't do anything and the report was originally empty, then the last_updated_by field would be None, this would lead to crash when set-maintenance is called again, since the last_updated_by field couldn't be None. Now, while exporting the report, make it to default value if field last_updated_by is None. --- CHANGELOG.md | 1 + pubtools/pulplib/_impl/model/__init__.py | 6 +----- pubtools/pulplib/_impl/model/maintenance.py | 9 +++++++-- pubtools/pulplib/_impl/model/repository/base.py | 6 +----- 4 files changed, 10 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7cde0c4e..7ee7a484 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Fixed a bug that export an empty maintenance report would crash. +- Fixed another bug that maintenance report could have an invalid `last_updated_by` value. ## [2.3.1] - 2019-10-03 diff --git a/pubtools/pulplib/_impl/model/__init__.py b/pubtools/pulplib/_impl/model/__init__.py index 8eeb7dc5..6d9af2d6 100644 --- a/pubtools/pulplib/_impl/model/__init__.py +++ b/pubtools/pulplib/_impl/model/__init__.py @@ -1,8 +1,4 @@ -from .common import ( - PulpObject, - DetachedException, - InvalidDataException, -) +from .common import PulpObject, DetachedException, InvalidDataException from .repository import ( Repository, YumRepository, diff --git a/pubtools/pulplib/_impl/model/maintenance.py b/pubtools/pulplib/_impl/model/maintenance.py index 8df8e5b0..f41d369c 100644 --- a/pubtools/pulplib/_impl/model/maintenance.py +++ b/pubtools/pulplib/_impl/model/maintenance.py @@ -122,7 +122,7 @@ def _export_dict(self): """export a raw dictionary of maintenance report""" report = { "last_updated": write_timestamp(self.last_updated), - "last_updated_by": self.last_updated_by, + "last_updated_by": self.last_updated_by or self._OWNER, "repos": {}, } @@ -219,4 +219,9 @@ def remove(self, repo_ids, **kwargs): if entry.repo_id not in repo_ids: new_entries.append(entry) - return attr.evolve(self, last_updated_by=owner, entries=new_entries) + return attr.evolve( + self, + entries=new_entries, + last_updated_by=owner, + last_updated=datetime.datetime.utcnow(), + ) diff --git a/pubtools/pulplib/_impl/model/repository/base.py b/pubtools/pulplib/_impl/model/repository/base.py index 4109caff..2ecd756c 100644 --- a/pubtools/pulplib/_impl/model/repository/base.py +++ b/pubtools/pulplib/_impl/model/repository/base.py @@ -4,11 +4,7 @@ from attr import validators from more_executors.futures import f_proxy -from ..common import ( - PulpObject, - Deletable, - DetachedException, -) +from ..common import PulpObject, Deletable, DetachedException from ..attr import pulp_attrib from ..distributor import Distributor from ..frozenlist import FrozenList From 9bbff7857f15de901988fccf697c40b378149602 Mon Sep 17 00:00:00 2001 From: Jay Zhang Date: Tue, 7 Jan 2020 10:48:26 +0800 Subject: [PATCH 2/4] fix black issue --- pubtools/pulplib/_impl/model/__init__.py | 6 +++++- pubtools/pulplib/_impl/model/repository/base.py | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/pubtools/pulplib/_impl/model/__init__.py b/pubtools/pulplib/_impl/model/__init__.py index 6d9af2d6..8eeb7dc5 100644 --- a/pubtools/pulplib/_impl/model/__init__.py +++ b/pubtools/pulplib/_impl/model/__init__.py @@ -1,4 +1,8 @@ -from .common import PulpObject, DetachedException, InvalidDataException +from .common import ( + PulpObject, + DetachedException, + InvalidDataException, +) from .repository import ( Repository, YumRepository, diff --git a/pubtools/pulplib/_impl/model/repository/base.py b/pubtools/pulplib/_impl/model/repository/base.py index 2ecd756c..4109caff 100644 --- a/pubtools/pulplib/_impl/model/repository/base.py +++ b/pubtools/pulplib/_impl/model/repository/base.py @@ -4,7 +4,11 @@ from attr import validators from more_executors.futures import f_proxy -from ..common import PulpObject, Deletable, DetachedException +from ..common import ( + PulpObject, + Deletable, + DetachedException, +) from ..attr import pulp_attrib from ..distributor import Distributor from ..frozenlist import FrozenList From 32d65442eea08ffc79a6513f401ace8eb4a0e4a3 Mon Sep 17 00:00:00 2001 From: Jay Zhang Date: Tue, 7 Jan 2020 14:46:11 +0800 Subject: [PATCH 3/4] Add test that export empty report should have valid last_updated_by value --- tests/maintenance/test_maintenance.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/maintenance/test_maintenance.py b/tests/maintenance/test_maintenance.py index 95f47ac4..cd8396df 100644 --- a/tests/maintenance/test_maintenance.py +++ b/tests/maintenance/test_maintenance.py @@ -103,3 +103,5 @@ def test_export_empty_report(): data = report._export_dict() # exported report should have a timestamp assert datetime.datetime.strptime(data["last_updated"], "%Y-%m-%dT%H:%M:%SZ") + # exported report should have the default owner + assert data["last_updated_by"] == "ContentDelivery" From cb44b310ff289a28b1be82d6609dbdcd8428e9bc Mon Sep 17 00:00:00 2001 From: Jay Zhang Date: Wed, 8 Jan 2020 10:12:43 +0800 Subject: [PATCH 4/4] Remove unrelated change --- pubtools/pulplib/_impl/model/maintenance.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/pubtools/pulplib/_impl/model/maintenance.py b/pubtools/pulplib/_impl/model/maintenance.py index f41d369c..89c6e29f 100644 --- a/pubtools/pulplib/_impl/model/maintenance.py +++ b/pubtools/pulplib/_impl/model/maintenance.py @@ -219,9 +219,4 @@ def remove(self, repo_ids, **kwargs): if entry.repo_id not in repo_ids: new_entries.append(entry) - return attr.evolve( - self, - entries=new_entries, - last_updated_by=owner, - last_updated=datetime.datetime.utcnow(), - ) + return attr.evolve(self, last_updated_by=owner, entries=new_entries)