Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions gitlab/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,12 @@ def save(self, **kwargs):

# call the manager
obj_id = self.get_id()
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)
return server_data


class ObjectDeleteMixin(object):
Expand Down
46 changes: 46 additions & 0 deletions gitlab/v4/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down