diff options
author | James Johnson <d0c.s4vage@gmail.com> | 2017-09-11 23:20:08 -0500 |
---|---|---|
committer | Gauvain Pocentek <gauvain@pocentek.net> | 2017-09-12 06:20:08 +0200 |
commit | 29879d61d117ff7909302ed845a6a1eb13814365 (patch) | |
tree | fafcb7f7003cc4b5a4146ea6090a76d9bf82082e /gitlab/v3/objects.py | |
parent | fd40fce913fbb3cd0e3aa2fd042e20bf1d51e9d6 (diff) | |
download | gitlab-29879d61d117ff7909302ed845a6a1eb13814365.tar.gz |
adds project upload feature (#239)
Diffstat (limited to 'gitlab/v3/objects.py')
-rw-r--r-- | gitlab/v3/objects.py | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/gitlab/v3/objects.py b/gitlab/v3/objects.py index 94c3873..338d219 100644 --- a/gitlab/v3/objects.py +++ b/gitlab/v3/objects.py @@ -909,6 +909,10 @@ class ProjectIssueNote(GitlabObject): requiredCreateAttrs = ['body'] optionalCreateAttrs = ['created_at'] + # file attachment settings (see #56) + description_attr = "body" + project_id_attr = "project_id" + class ProjectIssueNoteManager(BaseManager): obj_cls = ProjectIssueNote @@ -933,6 +937,10 @@ class ProjectIssue(GitlabObject): [('project_id', 'project_id'), ('issue_id', 'id')]), ) + # file attachment settings (see #56) + description_attr = "description" + project_id_attr = "project_id" + def subscribe(self, **kwargs): """Subscribe to an issue. @@ -1057,6 +1065,7 @@ class ProjectIssueManager(BaseManager): class ProjectMember(GitlabObject): _url = '/projects/%(project_id)s/members' + requiredUrlAttrs = ['project_id'] requiredCreateAttrs = ['access_level', 'user_id'] optionalCreateAttrs = ['expires_at'] @@ -2096,6 +2105,60 @@ class Project(GitlabObject): r = self.gitlab._raw_post(url, data=data, **kwargs) raise_error_from_response(r, GitlabCreateError, 201) + # see #56 - add file attachment features + 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, + }) + r = self.gitlab._raw_post( + url, + files={"file": (filename, filedata)}, + ) + # returns 201 status code (created) + raise_error_from_response(r, GitlabUploadError, expected_code=201) + data = r.json() + + return { + "alt": data['alt'], + "url": data['url'], + "markdown": data['markdown'] + } + class Runner(GitlabObject): _url = '/runners' |