diff options
Diffstat (limited to 'gitlab/v3')
| -rw-r--r-- | gitlab/v3/cli.py | 21 | ||||
| -rw-r--r-- | gitlab/v3/objects.py | 63 |
2 files changed, 83 insertions, 1 deletions
diff --git a/gitlab/v3/cli.py b/gitlab/v3/cli.py index ae16cf7..a8e3a5f 100644 --- a/gitlab/v3/cli.py +++ b/gitlab/v3/cli.py @@ -68,7 +68,8 @@ EXTRA_ACTIONS = { 'unstar': {'required': ['id']}, 'archive': {'required': ['id']}, 'unarchive': {'required': ['id']}, - 'share': {'required': ['id', 'group-id', 'group-access']}}, + 'share': {'required': ['id', 'group-id', 'group-access']}, + 'upload': {'required': ['id', 'filename', 'filepath']}}, gitlab.v3.objects.User: { 'block': {'required': ['id']}, 'unblock': {'required': ['id']}, @@ -348,6 +349,20 @@ class GitlabCLI(object): except Exception as e: cli.die("Impossible to get user %s" % args['query'], e) + def do_project_upload(self, cls, gl, what, args): + try: + project = gl.projects.get(args["id"]) + except Exception as e: + cli.die("Could not load project '{!r}'".format(args["id"]), e) + + try: + res = project.upload(filename=args["filename"], + filepath=args["filepath"]) + except Exception as e: + cli.die("Could not upload file into project", e) + + return res + def _populate_sub_parser_by_class(cls, sub_parser): for action_name in ['list', 'get', 'create', 'update', 'delete']: @@ -469,6 +484,7 @@ def run(gl, what, action, args, verbose, *fargs, **kwargs): cli.die("Unknown object: %s" % what) g_cli = GitlabCLI() + method = None what = what.replace('-', '_') action = action.lower().replace('-', '') @@ -491,6 +507,9 @@ def run(gl, what, action, args, verbose, *fargs, **kwargs): print("") else: print(o) + elif isinstance(ret_val, dict): + for k, v in six.iteritems(ret_val): + print("{} = {}".format(k, v)) elif isinstance(ret_val, gitlab.base.GitlabObject): ret_val.display(verbose) elif isinstance(ret_val, six.string_types): 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' |
