Skip to content

Commit d7ee0cb

Browse files
fix: cli: url-encode path components of the URL
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
1 parent a3eafab commit d7ee0cb

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

gitlab/v4/cli.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def __init__(
3939
self.action = action.lower()
4040
self.gl = gl
4141
self.args = args
42+
self.parent_args = {}
4243
self.mgr_cls: Union[
4344
Type[gitlab.mixins.CreateMixin],
4445
Type[gitlab.mixins.DeleteMixin],
@@ -53,7 +54,15 @@ def __init__(
5354
# the class _path attribute, and replace the value with the result.
5455
if TYPE_CHECKING:
5556
assert self.mgr_cls._path is not None
56-
self.mgr_cls._path = self.mgr_cls._path.format(**self.args)
57+
# Items in the path need to be url-encoded
58+
if self.mgr_cls._from_parent_attrs:
59+
for k in self.mgr_cls._from_parent_attrs:
60+
if k in self.args:
61+
self.parent_args[k] = gitlab.utils.clean_str_id(self.args[k])
62+
# If we don't delete it then it will be added to the URL as a
63+
# query-string
64+
del self.args[k]
65+
self.mgr_cls._path = self.mgr_cls._path.format(**self.parent_args)
5766
self.mgr = self.mgr_cls(gl)
5867

5968
if self.mgr_cls._types:
@@ -85,7 +94,7 @@ def do_custom(self) -> Any:
8594
data = {}
8695
if self.mgr._from_parent_attrs:
8796
for k in self.mgr._from_parent_attrs:
88-
data[k] = self.args[k]
97+
data[k] = self.parent_args[k]
8998
if not issubclass(self.cls, gitlab.mixins.GetWithoutIdMixin):
9099
if TYPE_CHECKING:
91100
assert isinstance(self.cls._id_attr, str)

0 commit comments

Comments
 (0)