Skip to content

Commit

Permalink
fix: cli: url-encode path components of the URL
Browse files Browse the repository at this point in the history
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
  • Loading branch information
JohnVillalovos committed Jan 2, 2022
1 parent a3eafab commit ae97e4d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
13 changes: 11 additions & 2 deletions gitlab/v4/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def __init__(
self.action = action.lower()
self.gl = gl
self.args = args
self.parent_args = {}
self.mgr_cls: Union[
Type[gitlab.mixins.CreateMixin],
Type[gitlab.mixins.DeleteMixin],
Expand All @@ -53,7 +54,15 @@ def __init__(
# 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)
# Items in the path need to be url-encoded
if self.mgr_cls._from_parent_attrs:
for k in self.mgr_cls._from_parent_attrs:
if k in self.args:
self.parent_args[k] = gitlab.utils.clean_str_id(self.args[k])
# If we don't delete it then it will be added to the URL as a
# query-string
del self.args[k]
self.mgr_cls._path = self.mgr_cls._path.format(**self.parent_args)
self.mgr = self.mgr_cls(gl)

if self.mgr_cls._types:
Expand Down Expand Up @@ -85,7 +94,7 @@ def do_custom(self) -> Any:
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)
Expand Down
7 changes: 7 additions & 0 deletions tests/functional/cli/test_cli_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@ def test_list_project_variables(gitlab_cli, project):
ret = gitlab_cli(cmd)

assert ret.success


def test_list_project_variables_with_path(gitlab_cli, project):
cmd = ["project-variable", "list", "--project-id", project.path_with_namespace]
ret = gitlab_cli(cmd)

assert ret.success

0 comments on commit ae97e4d

Please sign in to comment.