summaryrefslogtreecommitdiff
path: root/gitlab/v4/objects/files.py
diff options
context:
space:
mode:
authorJohn L. Villalovos <john@sodarock.com>2022-01-09 00:53:12 -0800
committerJohn L. Villalovos <john@sodarock.com>2022-01-09 00:53:12 -0800
commit99e1f8bad3e52ca6fb2dc6b876a262c6fee39e41 (patch)
tree6a11bdaf3e49a3dec844255d74a860798e8acc68 /gitlab/v4/objects/files.py
parentac812727c26c9bde4ee5c1115029f2ff4ab1964b (diff)
downloadgitlab-jlvillal/encoded_id_alt.tar.gz
fix: use url-encoded ID in all paths ALTERNATE METHODjlvillal/encoded_id_alt
An alternative to https://github.com/python-gitlab/python-gitlab/pull/1819 Make sure all usage of the ID in the URL path is encoded. Normally it isn't an issue as most IDs are integers or strings which don't contain a slash ('/'). But when the ID is a string with a slash character it will break things. Add a test case that shows this fixes wikis issue with subpages which use the slash character. Closes: #1079
Diffstat (limited to 'gitlab/v4/objects/files.py')
-rw-r--r--gitlab/v4/objects/files.py21
1 files changed, 9 insertions, 12 deletions
diff --git a/gitlab/v4/objects/files.py b/gitlab/v4/objects/files.py
index 64046f9..2dd8a8e 100644
--- a/gitlab/v4/objects/files.py
+++ b/gitlab/v4/objects/files.py
@@ -56,7 +56,7 @@ class ProjectFile(SaveMixin, ObjectDeleteMixin, RESTObject):
"""
self.branch = branch
self.commit_message = commit_message
- self.file_path = utils._url_encode(self.file_path)
+ self.file_path = str(utils.EncodedId(self.file_path))
super(ProjectFile, self).save(**kwargs)
@exc.on_http_error(exc.GitlabDeleteError)
@@ -76,7 +76,7 @@ class ProjectFile(SaveMixin, ObjectDeleteMixin, RESTObject):
GitlabAuthenticationError: If authentication is not correct
GitlabDeleteError: If the server cannot perform the request
"""
- file_path = utils._url_encode(self.get_id())
+ file_path = self.encoded_id
self.manager.delete(file_path, branch, commit_message, **kwargs)
@@ -144,7 +144,7 @@ class ProjectFileManager(GetMixin, CreateMixin, UpdateMixin, DeleteMixin, RESTMa
assert data is not None
self._check_missing_create_attrs(data)
new_data = data.copy()
- file_path = utils._url_encode(new_data.pop("file_path"))
+ file_path = utils.EncodedId(new_data.pop("file_path"))
path = f"{self.path}/{file_path}"
server_data = self.gitlab.http_post(path, post_data=new_data, **kwargs)
if TYPE_CHECKING:
@@ -173,9 +173,9 @@ class ProjectFileManager(GetMixin, CreateMixin, UpdateMixin, DeleteMixin, RESTMa
"""
new_data = new_data or {}
data = new_data.copy()
- file_path = utils._url_encode(file_path)
- data["file_path"] = file_path
- path = f"{self.path}/{file_path}"
+ encoded_file_path = utils.EncodedId(file_path)
+ data["file_path"] = encoded_file_path
+ path = f"{self.path}/{encoded_file_path}"
self._check_missing_update_attrs(data)
result = self.gitlab.http_put(path, post_data=data, **kwargs)
if TYPE_CHECKING:
@@ -203,8 +203,7 @@ class ProjectFileManager(GetMixin, CreateMixin, UpdateMixin, DeleteMixin, RESTMa
GitlabAuthenticationError: If authentication is not correct
GitlabDeleteError: If the server cannot perform the request
"""
- file_path = utils._url_encode(file_path)
- path = f"{self.path}/{file_path}"
+ path = f"{self.path}/{utils.EncodedId(file_path)}"
data = {"branch": branch, "commit_message": commit_message}
self.gitlab.http_delete(path, query_data=data, **kwargs)
@@ -239,8 +238,7 @@ class ProjectFileManager(GetMixin, CreateMixin, UpdateMixin, DeleteMixin, RESTMa
Returns:
The file content
"""
- file_path = utils._url_encode(file_path)
- path = f"{self.path}/{file_path}/raw"
+ path = f"{self.path}/{utils.EncodedId(file_path)}/raw"
query_data = {"ref": ref}
result = self.gitlab.http_get(
path, query_data=query_data, streamed=streamed, raw=True, **kwargs
@@ -266,8 +264,7 @@ class ProjectFileManager(GetMixin, CreateMixin, UpdateMixin, DeleteMixin, RESTMa
Returns:
A list of commits/lines matching the file
"""
- file_path = utils._url_encode(file_path)
- path = f"{self.path}/{file_path}/blame"
+ path = f"{self.path}/{utils.EncodedId(file_path)}/blame"
query_data = {"ref": ref}
result = self.gitlab.http_list(path, query_data, **kwargs)
if TYPE_CHECKING: