summaryrefslogtreecommitdiff
path: root/gitlab
diff options
context:
space:
mode:
Diffstat (limited to 'gitlab')
-rw-r--r--gitlab/base.py8
-rw-r--r--gitlab/mixins.py14
-rw-r--r--gitlab/tests/test_base.py22
3 files changed, 37 insertions, 7 deletions
diff --git a/gitlab/base.py b/gitlab/base.py
index 7121cb0..317ae61 100644
--- a/gitlab/base.py
+++ b/gitlab/base.py
@@ -45,6 +45,7 @@ class RESTObject(object):
_attrs: Dict[str, Any]
_module: ModuleType
_parent_attrs: Dict[str, Any]
+ _persist_attrs: bool
_short_print_attr: Optional[str] = None
_updated_attrs: Dict[str, Any]
manager: "RESTManager"
@@ -59,6 +60,7 @@ class RESTObject(object):
}
)
self.__dict__["_parent_attrs"] = self.manager.parent_attrs
+ self.__dict__["_persist_attrs"] = False
self._create_managers()
def __getstate__(self) -> Dict[str, Any]:
@@ -153,7 +155,11 @@ class RESTObject(object):
def _update_attrs(self, new_attrs: Dict[str, Any]) -> None:
self.__dict__["_updated_attrs"] = {}
- self.__dict__["_attrs"] = new_attrs
+
+ if self.__dict__["_persist_attrs"] is True:
+ self.__dict__["_attrs"].update(new_attrs)
+ else:
+ self.__dict__["_attrs"] = new_attrs
def get_id(self):
"""Returns the id of the resource."""
diff --git a/gitlab/mixins.py b/gitlab/mixins.py
index b9026c5..f0b80ce 100644
--- a/gitlab/mixins.py
+++ b/gitlab/mixins.py
@@ -162,10 +162,12 @@ class RefreshMixin(_RestObjectBase):
manager: base.RESTManager
@exc.on_http_error(exc.GitlabGetError)
- def refresh(self, **kwargs: Any) -> None:
+ def refresh(self, persist_attributes: bool = None, **kwargs: Any) -> None:
"""Refresh a single object from server.
Args:
+ persist_attributes: Whether to keep existing local attributes that
+ were not fetched from the server on refresh
**kwargs: Extra options to send to the server (e.g. sudo)
Returns None (updates the object)
@@ -174,6 +176,9 @@ class RefreshMixin(_RestObjectBase):
GitlabAuthenticationError: If authentication is not correct
GitlabGetError: If the server cannot perform the request
"""
+ if persist_attributes is not None:
+ self.__dict__["_persist_attrs"] = persist_attributes
+
if self._id_attr:
path = "%s/%s" % (self.manager.path, self.id)
else:
@@ -529,18 +534,23 @@ class SaveMixin(_RestObjectBase):
return updated_data
- def save(self, **kwargs: Any) -> None:
+ def save(self, persist_attributes: bool = None, **kwargs: Any) -> None:
"""Save the changes made to the object to the server.
The object is updated to match what the server returns.
Args:
+ persist_attributes: Whether to keep existing local attributes that
+ were not fetched from the server on save
**kwargs: Extra options to send to the server (e.g. sudo)
Raise:
GitlabAuthenticationError: If authentication is not correct
GitlabUpdateError: If the server cannot perform the request
"""
+ if persist_attributes is not None:
+ self.__dict__["_persist_attrs"] = persist_attributes
+
updated_data = self._get_updated_data()
# Nothing to update. Server fails if sent an empty dict.
if not updated_data:
diff --git a/gitlab/tests/test_base.py b/gitlab/tests/test_base.py
index 1c811cf..986d51f 100644
--- a/gitlab/tests/test_base.py
+++ b/gitlab/tests/test_base.py
@@ -128,11 +128,25 @@ class TestRESTObject:
assert {"foo": "foo", "bar": "bar"} == obj._attrs
assert {} == obj._updated_attrs
- def test_update_attrs_deleted(self, fake_manager):
- obj = FakeObject(fake_manager, {"foo": "foo", "bar": "bar"})
- obj.bar = "baz"
+ @pytest.mark.parametrize(
+ "initial_attrs,persist_attrs,assigned_attr,expected_attrs",
+ [
+ ({"foo": "foo", "bar": "bar"}, None, "baz", {"foo": "foo"}),
+ ({"foo": "foo", "bar": "bar"}, False, "baz", {"foo": "foo"}),
+ ({"foo": "foo", "bar": "bar"}, True, "baz", {"foo": "foo", "bar": "baz"}),
+ ],
+ )
+ def test_update_attrs_deleted(
+ self, fake_manager, initial_attrs, persist_attrs, assigned_attr, expected_attrs
+ ):
+ obj = FakeObject(fake_manager, initial_attrs)
+ obj._attrs["bar"] = assigned_attr
+
+ if persist_attrs is not None:
+ obj.__dict__["_persist_attrs"] = persist_attrs
+
obj._update_attrs({"foo": "foo"})
- assert {"foo": "foo"} == obj._attrs
+ assert expected_attrs == obj._attrs
assert {} == obj._updated_attrs
def test_dir_unique(self, fake_manager):