From e4e0c73f31fe2cb26108d7d7bcf4f30826458587 Mon Sep 17 00:00:00 2001 From: kernelport <30635575+kernelport@users.noreply.github.com> Date: Wed, 22 Apr 2020 17:33:55 +0200 Subject: [PATCH 1/4] make it possible to update subpages in wikis Workaround for https://github.com/python-gitlab/python-gitlab/issues/1079 --- gitlab/mixins.py | 1 + 1 file changed, 1 insertion(+) diff --git a/gitlab/mixins.py b/gitlab/mixins.py index 9c00c324d..1378e6cd1 100644 --- a/gitlab/mixins.py +++ b/gitlab/mixins.py @@ -382,6 +382,7 @@ def save(self, **kwargs): # call the manager obj_id = self.get_id() + obj_id = obj_id.replace('/', '%2F') server_data = self.manager.update(obj_id, updated_data, **kwargs) if server_data is not None: self._update_attrs(server_data) From f64348684b57d7b43176fb8ad701535ee393bda5 Mon Sep 17 00:00:00 2001 From: kernelport <30635575+kernelport@users.noreply.github.com> Date: Wed, 22 Apr 2020 21:27:10 +0200 Subject: [PATCH 2/4] save should return an updated slug in case of change the title --- gitlab/mixins.py | 1 + 1 file changed, 1 insertion(+) diff --git a/gitlab/mixins.py b/gitlab/mixins.py index 1378e6cd1..a280784bd 100644 --- a/gitlab/mixins.py +++ b/gitlab/mixins.py @@ -386,6 +386,7 @@ def save(self, **kwargs): server_data = self.manager.update(obj_id, updated_data, **kwargs) if server_data is not None: self._update_attrs(server_data) + return server_data class ObjectDeleteMixin(object): From cc73f69571131c2f1b1bfc7a7086a93a58c843e9 Mon Sep 17 00:00:00 2001 From: kernelport <30635575+kernelport@users.noreply.github.com> Date: Fri, 24 Apr 2020 16:49:33 +0200 Subject: [PATCH 3/4] introduce wikiattachment upload method to attach files to a wiki by using this API: https://docs.gitlab.com/ee/api/wikis.html#upload-an-attachment-to-the-wiki-repository --- gitlab/v4/objects.py | 46 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/gitlab/v4/objects.py b/gitlab/v4/objects.py index f6c09d9f2..b658b43c2 100644 --- a/gitlab/v4/objects.py +++ b/gitlab/v4/objects.py @@ -4937,7 +4937,53 @@ def upload(self, filename, filedata=None, filepath=None, **kwargs): data = self.manager.gitlab.http_post(url, files=file_info) return {"alt": data["alt"], "url": data["url"], "markdown": data["markdown"]} + + # see #56 - add file attachment features + @cli.register_custom_action("Project", ("filename", "filepath")) + @exc.on_http_error(exc.GitlabUploadError) + def wikiattachment(self, filename, filedata=None, filepath=None, **kwargs): + """Upload the specified file into the project Wiki. + https://docs.gitlab.com/ee/api/wikis.html#upload-an-attachment-to-the-wiki-repository + + .. note:: + + Either ``filedata`` or ``filepath`` *MUST* be specified. + + Args: + filename (str): The name of the file being uploaded + filedata (bytes): The raw data of the file being uploaded + filepath (str): The path to a local file to upload (optional) + + Raises: + GitlabConnectionError: If the server cannot be reached + GitlabUploadError: If the file upload fails + GitlabUploadError: If ``filedata`` and ``filepath`` are not + specified + GitlabUploadError: If both ``filedata`` and ``filepath`` are + specified + + Returns: + dict: A ``dict`` with the keys: + * ``alt`` - The alternate text for the upload + * ``url`` - The direct url to the uploaded file + * ``markdown`` - Markdown for the uploaded file + """ + if filepath is None and filedata is None: + raise GitlabUploadError("No file contents or path specified") + + if filedata is not None and filepath is not None: + raise GitlabUploadError("File contents and file path specified") + + if filepath is not None: + with open(filepath, "rb") as f: + filedata = f.read() + + url = "/projects/%(id)s/wikis/attachments" % {"id": self.id} + file_info = {"file": (filename, filedata)} + data = self.manager.gitlab.http_post(url, files=file_info) + return data + @cli.register_custom_action("Project", optional=("wiki",)) @exc.on_http_error(exc.GitlabGetError) def snapshot( From 3e9859ddd6bf05f0244da1c721737b3f5914ef77 Mon Sep 17 00:00:00 2001 From: kernelport <30635575+kernelport@users.noreply.github.com> Date: Fri, 24 Apr 2020 16:41:12 +0200 Subject: [PATCH 4/4] FIX: replace sub pages in wiki --- gitlab/mixins.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gitlab/mixins.py b/gitlab/mixins.py index a280784bd..c2ba6d096 100644 --- a/gitlab/mixins.py +++ b/gitlab/mixins.py @@ -382,7 +382,8 @@ def save(self, **kwargs): # call the manager obj_id = self.get_id() - obj_id = obj_id.replace('/', '%2F') + if isinstance(obj_id, (str)): + obj_id = obj_id.replace('/', '%2F') server_data = self.manager.update(obj_id, updated_data, **kwargs) if server_data is not None: self._update_attrs(server_data)