Skip to content

Commit 8c17afa

Browse files
chore: remove custom URL encoding
We were using `str.replace()` calls to take care of URL encoding issues. Switch them to use our `utils.clean_str_id` which itself uses `urllib.parse.quote()` Closes: #1356
1 parent d45b59e commit 8c17afa

File tree

4 files changed

+13
-11
lines changed

4 files changed

+13
-11
lines changed

gitlab/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1717

1818
from typing import Any, Callable, Dict, Optional
19-
from urllib.parse import quote
19+
import urllib.parse
2020

2121
import requests
2222

@@ -57,7 +57,7 @@ def copy_dict(dest: Dict[str, Any], src: Dict[str, Any]) -> None:
5757

5858

5959
def clean_str_id(id: str) -> str:
60-
return quote(id, safe="")
60+
return urllib.parse.quote(id, safe="")
6161

6262

6363
def remove_none_from_dict(data: Dict[str, Any]) -> Dict[str, Any]:

gitlab/v4/objects/features.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ def set(
5252
Returns:
5353
The created/updated attribute
5454
"""
55-
path = f"{self.path}/{name.replace('/', '%2F')}"
55+
name = utils.clean_str_id(name)
56+
path = f"{self.path}/{name}"
5657
data = {
5758
"value": value,
5859
"feature_group": feature_group,

gitlab/v4/objects/files.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def save( # type: ignore
5656
"""
5757
self.branch = branch
5858
self.commit_message = commit_message
59-
self.file_path = self.file_path.replace("/", "%2F")
59+
self.file_path = utils.clean_str_id(self.file_path)
6060
super(ProjectFile, self).save(**kwargs)
6161

6262
@exc.on_http_error(exc.GitlabDeleteError)
@@ -76,7 +76,7 @@ def delete( # type: ignore
7676
GitlabAuthenticationError: If authentication is not correct
7777
GitlabDeleteError: If the server cannot perform the request
7878
"""
79-
file_path = self.get_id().replace("/", "%2F")
79+
file_path = utils.clean_str_id(self.get_id())
8080
self.manager.delete(file_path, branch, commit_message, **kwargs)
8181

8282

@@ -144,7 +144,7 @@ def create(
144144
assert data is not None
145145
self._check_missing_create_attrs(data)
146146
new_data = data.copy()
147-
file_path = new_data.pop("file_path").replace("/", "%2F")
147+
file_path = utils.clean_str_id(new_data.pop("file_path"))
148148
path = f"{self.path}/{file_path}"
149149
server_data = self.gitlab.http_post(path, post_data=new_data, **kwargs)
150150
if TYPE_CHECKING:
@@ -173,7 +173,7 @@ def update( # type: ignore
173173
"""
174174
new_data = new_data or {}
175175
data = new_data.copy()
176-
file_path = file_path.replace("/", "%2F")
176+
file_path = utils.clean_str_id(file_path)
177177
data["file_path"] = file_path
178178
path = f"{self.path}/{file_path}"
179179
self._check_missing_update_attrs(data)
@@ -203,7 +203,8 @@ def delete( # type: ignore
203203
GitlabAuthenticationError: If authentication is not correct
204204
GitlabDeleteError: If the server cannot perform the request
205205
"""
206-
path = f"{self.path}/{file_path.replace('/', '%2F')}"
206+
file_path = utils.clean_str_id(file_path)
207+
path = f"{self.path}/{file_path}"
207208
data = {"branch": branch, "commit_message": commit_message}
208209
self.gitlab.http_delete(path, query_data=data, **kwargs)
209210

@@ -238,7 +239,7 @@ def raw(
238239
Returns:
239240
The file content
240241
"""
241-
file_path = file_path.replace("/", "%2F").replace(".", "%2E")
242+
file_path = utils.clean_str_id(file_path)
242243
path = f"{self.path}/{file_path}/raw"
243244
query_data = {"ref": ref}
244245
result = self.gitlab.http_get(
@@ -265,7 +266,7 @@ def blame(self, file_path: str, ref: str, **kwargs: Any) -> List[Dict[str, Any]]
265266
Returns:
266267
A list of commits/lines matching the file
267268
"""
268-
file_path = file_path.replace("/", "%2F").replace(".", "%2E")
269+
file_path = utils.clean_str_id(file_path)
269270
path = f"{self.path}/{file_path}/blame"
270271
query_data = {"ref": ref}
271272
result = self.gitlab.http_list(path, query_data, **kwargs)

gitlab/v4/objects/repositories.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def update_submodule(
3939
GitlabPutError: If the submodule could not be updated
4040
"""
4141

42-
submodule = submodule.replace("/", "%2F") # .replace('.', '%2E')
42+
submodule = utils.clean_str_id(submodule)
4343
path = f"/projects/{self.get_id()}/repository/submodules/{submodule}"
4444
data = {"branch": branch, "commit_sha": commit_sha}
4545
if "commit_message" in kwargs:

0 commit comments

Comments
 (0)