summaryrefslogtreecommitdiff
path: root/gitlab/v4/objects.py
diff options
context:
space:
mode:
authorJames Johnson <d0c.s4vage@gmail.com>2017-09-11 23:20:08 -0500
committerGauvain Pocentek <gauvain@pocentek.net>2017-09-12 06:20:08 +0200
commit29879d61d117ff7909302ed845a6a1eb13814365 (patch)
treefafcb7f7003cc4b5a4146ea6090a76d9bf82082e /gitlab/v4/objects.py
parentfd40fce913fbb3cd0e3aa2fd042e20bf1d51e9d6 (diff)
downloadgitlab-29879d61d117ff7909302ed845a6a1eb13814365.tar.gz
adds project upload feature (#239)
Diffstat (limited to 'gitlab/v4/objects.py')
-rw-r--r--gitlab/v4/objects.py53
1 files changed, 53 insertions, 0 deletions
diff --git a/gitlab/v4/objects.py b/gitlab/v4/objects.py
index 353f854..f1ec007 100644
--- a/gitlab/v4/objects.py
+++ b/gitlab/v4/objects.py
@@ -2071,6 +2071,59 @@ class Project(SaveMixin, ObjectDeleteMixin, RESTObject):
post_data.update(form)
self.manager.gitlab.http_post(path, post_data=post_data, **kwargs)
+ # see #56 - add file attachment features
+ @cli.register_custom_action('Project', ('filename', 'filepath'))
+ @exc.on_http_error(exc.GitlabUploadError)
+ def upload(self, filename, filedata=None, filepath=None, **kwargs):
+ """Upload the specified file into the project.
+
+ .. 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/uploads' % {
+ 'id': self.id,
+ })
+ file_info = {
+ 'file': (filename, filedata),
+ }
+ data = self.manager.gitlab.http_post(url, files=file_info)
+
+ return {
+ "alt": data['alt'],
+ "url": data['url'],
+ "markdown": data['markdown']
+ }
+
class Runner(SaveMixin, ObjectDeleteMixin, RESTObject):
pass