summaryrefslogtreecommitdiff
path: root/gitlab/v4/cli.py
diff options
context:
space:
mode:
authorJohn L. Villalovos <john@sodarock.com>2022-01-08 14:15:03 -0800
committerJohn L. Villalovos <john@sodarock.com>2022-01-08 14:15:03 -0800
commit79b1cc0b1be1e9986d9d3dae68db0bff981014fb (patch)
tree05a8cda1d7d038c247ab9e4208b775ed39bd243b /gitlab/v4/cli.py
parentc9ed3ddc1253c828dc877dcd55000d818c297ee7 (diff)
downloadgitlab-jlvillal/parent_attrs.tar.gz
fix: cli: url-encode path components of the URLjlvillal/parent_attrs
In the CLI we need to make sure the components put into the path portion of the URL are url-encoded. Otherwise they will be interpreted as part of the path. For example can specify the project ID as a path, but in the URL it must be url-encoded or it doesn't work. Also stop adding the components of the path as query parameters in the URL. Closes: #783 Closes: #1498
Diffstat (limited to 'gitlab/v4/cli.py')
-rw-r--r--gitlab/v4/cli.py20
1 files changed, 18 insertions, 2 deletions
diff --git a/gitlab/v4/cli.py b/gitlab/v4/cli.py
index 675f93a..5b276ae 100644
--- a/gitlab/v4/cli.py
+++ b/gitlab/v4/cli.py
@@ -39,6 +39,7 @@ class GitlabCLI(object):
self.action = action.lower()
self.gl = gl
self.args = args
+ self.parent_args: Dict[str, Any] = {}
self.mgr_cls: Union[
Type[gitlab.mixins.CreateMixin],
Type[gitlab.mixins.DeleteMixin],
@@ -53,7 +54,10 @@ class GitlabCLI(object):
# the class _path attribute, and replace the value with the result.
if TYPE_CHECKING:
assert self.mgr_cls._path is not None
- self.mgr_cls._path = self.mgr_cls._path.format(**self.args)
+
+ self._process_from_parent_attrs()
+
+ self.mgr_cls._path = self.mgr_cls._path.format(**self.parent_args)
self.mgr = self.mgr_cls(gl)
if self.mgr_cls._types:
@@ -63,6 +67,18 @@ class GitlabCLI(object):
obj.set_from_cli(self.args[attr_name])
self.args[attr_name] = obj.get()
+ def _process_from_parent_attrs(self) -> None:
+ """Items in the path need to be url-encoded. There is a 1:1 mapping from
+ mgr_cls._from_parent_attrs <--> mgr_cls._path. Those values must be url-encoded
+ as they may contain a slash '/'."""
+ for key in self.mgr_cls._from_parent_attrs:
+ if key not in self.args:
+ continue
+
+ self.parent_args[key] = gitlab.utils.clean_str_id(self.args[key])
+ # If we don't delete it then it will be added to the URL as a query-string
+ del self.args[key]
+
def __call__(self) -> Any:
# Check for a method that matches object + action
method = f"do_{self.what}_{self.action}"
@@ -85,7 +101,7 @@ class GitlabCLI(object):
data = {}
if self.mgr._from_parent_attrs:
for k in self.mgr._from_parent_attrs:
- data[k] = self.args[k]
+ data[k] = self.parent_args[k]
if not issubclass(self.cls, gitlab.mixins.GetWithoutIdMixin):
if TYPE_CHECKING:
assert isinstance(self.cls._id_attr, str)