Skip to content
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
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
  • Loading branch information
kernelport authored Apr 24, 2020
commit cc73f69571131c2f1b1bfc7a7086a93a58c843e9
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