Repository: python-gitlab/python-gitlab

Issue:
delete branch when branch‘s name have "#" and "$"
### Description of the problem, including code/CLI snippet
When I delete the branch with "#" and "$" in the name,the result returned by the interface shows 404.
`project = gl.projects.get(project_id) 
branch=project.branches.list(search=branch_name)[0] 
branch.delete()`
### Expected Behavior
The interface returns success to delete the branches.
### Actual Behavior
### Specifications
### python-gitlab version:
API version you are using (v3/v4):
v4
Gitlab server version (or gitlab.com):
GitLab Community Edition 11.8.0 



Discussion:

Resolving patch:
diff --git a/gitlab/mixins.py b/gitlab/mixins.py
--- a/gitlab/mixins.py
+++ b/gitlab/mixins.py
@@ -20,6 +20,7 @@
 from gitlab import cli
 from gitlab import exceptions as exc
 from gitlab import types as g_types
+from gitlab import utils
 
 
 class GetMixin(object):
@@ -42,7 +43,7 @@ def get(self, id, lazy=False, **kwargs):
             GitlabGetError: If the server cannot perform the request
         """
         if not isinstance(id, int):
-            id = id.replace("/", "%2F")
+            id = utils.clean_str_id(id)
         path = "%s/%s" % (self.path, id)
         if lazy is True:
             return self._obj_cls(self, {self._obj_cls._id_attr: id})
@@ -299,7 +300,7 @@ def set(self, key, value, **kwargs):
         Returns:
             obj: The created/updated attribute
         """
-        path = "%s/%s" % (self.path, key.replace("/", "%2F"))
+        path = "%s/%s" % (self.path, utils.clean_str_id(key))
         data = {"value": value}
         server_data = self.gitlab.http_put(path, post_data=data, **kwargs)
         return self._obj_cls(self, server_data)
@@ -322,7 +323,7 @@ def delete(self, id, **kwargs):
             path = self.path
         else:
             if not isinstance(id, int):
-                id = id.replace("/", "%2F")
+                id = utils.clean_str_id(id)
             path = "%s/%s" % (self.path, id)
         self.gitlab.http_delete(path, **kwargs)
 
diff --git a/gitlab/utils.py b/gitlab/utils.py
--- a/gitlab/utils.py
+++ b/gitlab/utils.py
@@ -47,6 +47,10 @@ def copy_dict(dest, src):
             dest[k] = v
 
 
+def clean_str_id(id):
+    return id.replace("/", "%2F").replace("#", "%23")
+
+
 def sanitized_url(url):
     parsed = six.moves.urllib.parse.urlparse(url)
     new_path = parsed.path.replace(".", "%2E")


Test patch:
diff --git a/gitlab/tests/test_utils.py b/gitlab/tests/test_utils.py
new file mode 100644
--- /dev/null
+++ b/gitlab/tests/test_utils.py
@@ -0,0 +1,43 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright (C) 2019 Gauvain Pocentek <gauvain@pocentek.net>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+try:
+    import unittest
+except ImportError:
+    import unittest2 as unittest
+
+from gitlab import utils
+
+
+class TestUtils(unittest.TestCase):
+    def test_clean_str_id(self):
+        src = "nothing_special"
+        dest = "nothing_special"
+        self.assertEqual(dest, utils.clean_str_id(src))
+
+        src = "foo#bar/baz/"
+        dest = "foo%23bar%2Fbaz%2F"
+        self.assertEqual(dest, utils.clean_str_id(src))
+
+    def test_sanitized_url(self):
+        src = "http://localhost/foo/bar"
+        dest = "http://localhost/foo/bar"
+        self.assertEqual(dest, utils.sanitized_url(src))
+
+        src = "http://localhost/foo.bar.baz"
+        dest = "http://localhost/foo%2Ebar%2Ebaz"
+        self.assertEqual(dest, utils.sanitized_url(src))