Exemplo n.º 1
0
 def test_die(self):
     fl = six.StringIO()
     with redirect_stderr(fl):
         with self.assertRaises(SystemExit) as test:
             cli.die("foobar")
     self.assertEqual(fl.getvalue(), "foobar\n")
     self.assertEqual(test.exception.code, 1)
Exemplo n.º 2
0
    def _get_id(self, cls, args):
        try:
            id = args.pop(cls.idAttr)
        except Exception:
            cli.die("Missing --%s argument" % cls.idAttr.replace('_', '-'))

        return id
Exemplo n.º 3
0
def test_die():
    fl = io.StringIO()
    with redirect_stderr(fl):
        with pytest.raises(SystemExit) as test:
            cli.die("foobar")
    assert fl.getvalue() == "foobar\n"
    assert test.value.code == 1
Exemplo n.º 4
0
 def test_die(self):
     fl = six.StringIO()
     with redirect_stderr(fl):
         with self.assertRaises(SystemExit) as test:
             cli.die("foobar")
     self.assertEqual(fl.getvalue(), "foobar\n")
     self.assertEqual(test.exception.code, 1)
Exemplo n.º 5
0
def run(gl, what, action, args, verbose, *fargs, **kwargs):
    try:
        cls = gitlab.v3.objects.__dict__[cli.what_to_cls(what)]
    except ImportError:
        cli.die("Unknown object: %s" % what)

    g_cli = GitlabCLI()
    method = None
    what = what.replace('-', '_')
    action = action.lower().replace('-', '')
    for test in ["do_%s_%s" % (what, action),
                 "do_%s" % action]:
        if hasattr(g_cli, test):
            method = test
            break

    if method is None:
        sys.stderr.write("Don't know how to deal with this!\n")
        sys.exit(1)

    ret_val = getattr(g_cli, method)(cls, gl, what, args)

    if isinstance(ret_val, list):
        for o in ret_val:
            if isinstance(o, gitlab.GitlabObject):
                o.display(verbose)
                print("")
            else:
                print(o)
    elif isinstance(ret_val, gitlab.base.GitlabObject):
        ret_val.display(verbose)
    elif isinstance(ret_val, six.string_types):
        print(ret_val)
Exemplo n.º 6
0
    def _get_id(self, cls, args):
        try:
            id = args.pop(cls.idAttr)
        except Exception:
            cli.die("Missing --%s argument" % cls.idAttr.replace('_', '-'))

        return id
Exemplo n.º 7
0
def test_die(message, error, expected):
    fl = io.StringIO()
    with redirect_stderr(fl):
        with pytest.raises(SystemExit) as test:
            cli.die(message, error)
    assert fl.getvalue() == expected
    assert test.value.code == 1
Exemplo n.º 8
0
    def do_project_export_download(self):
        try:
            project = self.gl.projects.get(int(self.args["project_id"]), lazy=True)
            data = project.exports.get().download()
            sys.stdout.buffer.write(data)

        except Exception as e:
            cli.die("Impossible to download the export", e)
Exemplo n.º 9
0
 def do_update(self):
     id = None
     if gitlab.mixins.GetWithoutIdMixin not in inspect.getmro(self.mgr_cls):
         id = self.args.pop(self.cls._id_attr)
     try:
         return self.mgr.update(id, self.args)
     except Exception as e:
         cli.die("Impossible to update object", e)
Exemplo n.º 10
0
 def do_update(self):
     id = None
     if gitlab.mixins.GetWithoutIdMixin not in inspect.getmro(self.mgr_cls):
         id = self.args.pop(self.cls._id_attr)
     try:
         return self.mgr.update(id, self.args)
     except Exception as e:
         cli.die("Impossible to update object", e)
Exemplo n.º 11
0
 def do_create(self) -> gitlab.base.RESTObject:
     if TYPE_CHECKING:
         assert isinstance(self.mgr, gitlab.mixins.CreateMixin)
     try:
         result = self.mgr.create(self.args)
     except Exception as e:
         cli.die("Impossible to create object", e)
     return result
Exemplo n.º 12
0
    def do_delete(self, cls, gl, what, args):
        if not cls.canDelete:
            cli.die("%s objects can't be deleted" % what)

        id = args.pop(cls.idAttr)
        try:
            gl.delete(cls, id, **args)
        except Exception as e:
            cli.die("Impossible to destroy object", e)
Exemplo n.º 13
0
    def do_delete(self, cls, gl, what, args):
        if not cls.canDelete:
            cli.die("%s objects can't be deleted" % what)

        id = args.pop(cls.idAttr)
        try:
            gl.delete(cls, id, **args)
        except Exception as e:
            cli.die("Impossible to destroy object", e)
Exemplo n.º 14
0
 def do_delete(self) -> None:
     if TYPE_CHECKING:
         assert isinstance(self.mgr, gitlab.mixins.DeleteMixin)
         assert isinstance(self.cls._id_attr, str)
     id = self.args.pop(self.cls._id_attr)
     try:
         self.mgr.delete(id, **self.args)
     except Exception as e:
         cli.die("Impossible to destroy object", e)
Exemplo n.º 15
0
    def do_list(self, cls, gl, what, args):
        if not cls.canList:
            cli.die("%s objects can't be listed" % what)

        try:
            l = cls.list(gl, **args)
        except Exception as e:
            cli.die("Impossible to list objects", e)

        return l
Exemplo n.º 16
0
    def do_create(self, cls, gl, what, args):
        if not cls.canCreate:
            cli.die("%s objects can't be created" % what)

        try:
            o = cls.create(gl, args)
        except Exception as e:
            cli.die("Impossible to create object", e)

        return o
Exemplo n.º 17
0
    def do_list(self, cls, gl, what, args):
        if not cls.canList:
            cli.die("%s objects can't be listed" % what)

        try:
            l = cls.list(gl, **args)
        except Exception as e:
            cli.die("Impossible to list objects", e)

        return l
Exemplo n.º 18
0
    def do_create(self, cls, gl, what, args):
        if not cls.canCreate:
            cli.die("%s objects can't be created" % what)

        try:
            o = cls.create(gl, args)
        except Exception as e:
            cli.die("Impossible to create object", e)

        return o
Exemplo n.º 19
0
 def do_list(
     self,
 ) -> Union[gitlab.base.RESTObjectList, List[gitlab.base.RESTObject]]:
     if TYPE_CHECKING:
         assert isinstance(self.mgr, gitlab.mixins.ListMixin)
     try:
         result = self.mgr.list(**self.args)
     except Exception as e:
         cli.die("Impossible to list objects", e)
     return result
Exemplo n.º 20
0
 def do_project_merge_request_merge(self, cls, gl, what, args):
     try:
         o = self.do_get(cls, gl, what, args)
         should_remove = args.get('should_remove_source_branch', False)
         build_succeeds = args.get('merged_when_build_succeeds', False)
         return o.merge(
             merge_commit_message=args.get('merge_commit_message', ''),
             should_remove_source_branch=should_remove,
             merged_when_build_succeeds=build_succeeds)
     except Exception as e:
         cli.die("Impossible to validate merge request", e)
Exemplo n.º 21
0
 def do_project_merge_request_merge(self, cls, gl, what, args):
     try:
         o = self.do_get(cls, gl, what, args)
         should_remove = args.get('should_remove_source_branch', False)
         build_succeeds = args.get('merged_when_build_succeeds', False)
         return o.merge(
             merge_commit_message=args.get('merge_commit_message', ''),
             should_remove_source_branch=should_remove,
             merged_when_build_succeeds=build_succeeds)
     except Exception as e:
         cli.die("Impossible to validate merge request", e)
Exemplo n.º 22
0
    def do_project_export_download(self) -> None:
        try:
            project = self.gl.projects.get(int(self.args["project_id"]),
                                           lazy=True)
            export_status = project.exports.get()
            if TYPE_CHECKING:
                assert export_status is not None
            data = export_status.download()
            sys.stdout.buffer.write(data)

        except Exception as e:
            cli.die("Impossible to download the export", e)
Exemplo n.º 23
0
    def do_project_export_download(self):
        try:
            project = self.gl.projects.get(int(self.args["project_id"]), lazy=True)
            data = project.exports.get().download()
            if hasattr(sys.stdout, "buffer"):
                # python3
                sys.stdout.buffer.write(data)
            else:
                sys.stdout.write(data)

        except Exception as e:
            cli.die("Impossible to download the export", e)
Exemplo n.º 24
0
    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
Exemplo n.º 25
0
    def do_update(self, cls, gl, what, args):
        if not cls.canUpdate:
            cli.die("%s objects can't be updated" % what)

        o = self.do_get(cls, gl, what, args)
        try:
            for k, v in args.items():
                o.__dict__[k] = v
            o.save()
        except Exception as e:
            cli.die("Impossible to update object", e)

        return o
Exemplo n.º 26
0
    def do_project_export_download(self):
        try:
            project = self.gl.projects.get(int(self.args['project_id']),
                                           lazy=True)
            data = project.exports.get().download()
            if hasattr(sys.stdout, 'buffer'):
                # python3
                sys.stdout.buffer.write(data)
            else:
                sys.stdout.write(data)

        except Exception as e:
            cli.die("Impossible to download the export", e)
Exemplo n.º 27
0
    def do_update(self, cls, gl, what, args):
        if not cls.canUpdate:
            cli.die("%s objects can't be updated" % what)

        o = self.do_get(cls, gl, what, args)
        try:
            for k, v in args.items():
                o.__dict__[k] = v
            o.save()
        except Exception as e:
            cli.die("Impossible to update object", e)

        return o
Exemplo n.º 28
0
    def do_get(self, cls, gl, what, args):
        if cls.canGet is False:
            cli.die("%s objects can't be retrieved" % what)

        id = None
        if cls not in [gitlab.v3.objects.CurrentUser] and cls.getRequiresId:
            id = self._get_id(cls, args)

        try:
            o = cls.get(gl, id, **args)
        except Exception as e:
            cli.die("Impossible to get object", e)

        return o
Exemplo n.º 29
0
    def do_get(self, cls, gl, what, args):
        if cls.canGet is False:
            cli.die("%s objects can't be retrieved" % what)

        id = None
        if cls not in [gitlab.v3.objects.CurrentUser] and cls.getRequiresId:
            id = self._get_id(cls, args)

        try:
            o = cls.get(gl, id, **args)
        except Exception as e:
            cli.die("Impossible to get object", e)

        return o
Exemplo n.º 30
0
    def do_update(self) -> Dict[str, Any]:
        if TYPE_CHECKING:
            assert isinstance(self.mgr, gitlab.mixins.UpdateMixin)
        if issubclass(self.mgr_cls, gitlab.mixins.GetWithoutIdMixin):
            id = None
        else:
            if TYPE_CHECKING:
                assert isinstance(self.cls._id_attr, str)
            id = self.args.pop(self.cls._id_attr)

        try:
            result = self.mgr.update(id, self.args)
        except Exception as e:
            cli.die("Impossible to update object", e)
        return result
Exemplo n.º 31
0
    def do_get(self) -> Optional[gitlab.base.RESTObject]:
        if isinstance(self.mgr, gitlab.mixins.GetWithoutIdMixin):
            try:
                result = self.mgr.get(id=None, **self.args)
            except Exception as e:
                cli.die("Impossible to get object", e)
            return result

        if TYPE_CHECKING:
            assert isinstance(self.mgr, gitlab.mixins.GetMixin)
            assert isinstance(self.cls._id_attr, str)

        id = self.args.pop(self.cls._id_attr)
        try:
            result = self.mgr.get(id, lazy=False, **self.args)
        except Exception as e:
            cli.die("Impossible to get object", e)
        return result
Exemplo n.º 32
0
def run(gl, what, action, args, verbose, *fargs, **kwargs):
    try:
        cls = gitlab.v3.objects.__dict__[cli.what_to_cls(what)]
    except ImportError:
        cli.die("Unknown object: %s" % what)

    g_cli = GitlabCLI()

    method = None
    what = what.replace('-', '_')
    action = action.lower().replace('-', '')
    for test in ["do_%s_%s" % (what, action),
                 "do_%s" % action]:
        if hasattr(g_cli, test):
            method = test
            break

    if method is None:
        sys.stderr.write("Don't know how to deal with this!\n")
        sys.exit(1)

    ret_val = getattr(g_cli, method)(cls, gl, what, args)

    if isinstance(ret_val, list):
        for o in ret_val:
            if isinstance(o, gitlab.GitlabObject):
                o.display(verbose)
                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):
        print(ret_val)
Exemplo n.º 33
0
 def do_create(self):
     try:
         return self.mgr.create(self.args)
     except Exception as e:
         cli.die("Impossible to create object", e)
Exemplo n.º 34
0
 def do_list(self):
     try:
         return self.mgr.list(**self.args)
     except Exception as e:
         cli.die("Impossible to list objects", e)
Exemplo n.º 35
0
 def do_user_search(self, cls, gl, what, args):
     try:
         return gl.users.search(args['query'])
     except Exception as e:
         cli.die("Impossible to search users", e)
Exemplo n.º 36
0
 def do_project_commit_builds(self, cls, gl, what, args):
     try:
         o = self.do_get(cls, gl, what, args)
         return o.builds()
     except Exception as e:
         cli.die("Impossible to get commit builds", e)
Exemplo n.º 37
0
 def do_create(self):
     try:
         return self.mgr.create(self.args)
     except Exception as e:
         cli.die("Impossible to create object", e)
Exemplo n.º 38
0
 def do_delete(self):
     id = self.args.pop(self.cls._id_attr)
     try:
         self.mgr.delete(id, **self.args)
     except Exception as e:
         cli.die("Impossible to destroy object", e)
Exemplo n.º 39
0
 def do_project_commit_diff(self, cls, gl, what, args):
     try:
         o = self.do_get(cls, gl, what, args)
         return [x['diff'] for x in o.diff()]
     except Exception as e:
         cli.die("Impossible to get commit diff", e)
Exemplo n.º 40
0
 def do_project_merge_request_closesissues(self, cls, gl, what, args):
     try:
         o = self.do_get(cls, gl, what, args)
         return o.closes_issues()
     except Exception as e:
         cli.die("Impossible to list issues closed by merge request", e)
Exemplo n.º 41
0
 def do_project_build_artifacts(self, cls, gl, what, args):
     try:
         o = self.do_get(cls, gl, what, args)
         return o.artifacts()
     except Exception as e:
         cli.die("Impossible to get project build artifacts", e)
Exemplo n.º 42
0
 def do_project_build_retry(self, cls, gl, what, args):
     try:
         o = self.do_get(cls, gl, what, args)
         return o.retry()
     except Exception as e:
         cli.die("Impossible to retry project build", e)
Exemplo n.º 43
0
 def do_project_build_trace(self, cls, gl, what, args):
     try:
         o = self.do_get(cls, gl, what, args)
         return o.trace()
     except Exception as e:
         cli.die("Impossible to get project build trace", e)
Exemplo n.º 44
0
 def do_project_issue_unsubscribe(self, cls, gl, what, args):
     try:
         o = self.do_get(cls, gl, what, args)
         o.unsubscribe()
     except Exception as e:
         cli.die("Impossible to subscribe to issue", e)
Exemplo n.º 45
0
 def do_project_issue_move(self, cls, gl, what, args):
     try:
         o = self.do_get(cls, gl, what, args)
         o.move(args['to_project_id'])
     except Exception as e:
         cli.die("Impossible to move issue", e)
Exemplo n.º 46
0
 def do_delete(self):
     id = self.args.pop(self.cls._id_attr)
     try:
         self.mgr.delete(id, **self.args)
     except Exception as e:
         cli.die("Impossible to destroy object", e)
Exemplo n.º 47
0
 def do_user_unblock(self, cls, gl, what, args):
     try:
         o = self.do_get(cls, gl, what, args)
         o.unblock()
     except Exception as e:
         cli.die("Impossible to block user", e)
Exemplo n.º 48
0
 def do_project_commit_blob(self, cls, gl, what, args):
     try:
         o = self.do_get(cls, gl, what, args)
         return o.blob(args['filepath'])
     except Exception as e:
         cli.die("Impossible to get commit blob", e)
Exemplo n.º 49
0
 def do_user_getbyusername(self, cls, gl, what, args):
     try:
         return gl.users.search(args['query'])
     except Exception as e:
         cli.die("Impossible to get user %s" % args['query'], e)
Exemplo n.º 50
0
 def do_update(self):
     id = self.args.pop(self.cls._id_attr)
     try:
         return self.mgr.update(id, self.args)
     except Exception as e:
         cli.die("Impossible to update object", e)
Exemplo n.º 51
0
 def do_project_merge_request_cancel(self, cls, gl, what, args):
     try:
         o = self.do_get(cls, gl, what, args)
         return o.cancel_merge_when_build_succeeds()
     except Exception as e:
         cli.die("Impossible to cancel merge request", e)
Exemplo n.º 52
0
 def do_project_milestone_issues(self, cls, gl, what, args):
     try:
         o = self.do_get(cls, gl, what, args)
         return o.issues()
     except Exception as e:
         cli.die("Impossible to get milestone issues", e)
Exemplo n.º 53
0
 def do_project_merge_request_cancel(self, cls, gl, what, args):
     try:
         o = self.do_get(cls, gl, what, args)
         return o.cancel_merge_when_build_succeeds()
     except Exception as e:
         cli.die("Impossible to cancel merge request", e)
Exemplo n.º 54
0
 def do_list(self):
     try:
         return self.mgr.list(**self.args)
     except Exception as e:
         cli.die("Impossible to list objects", e)
Exemplo n.º 55
0
 def do_user_getbyusername(self, cls, gl, what, args):
     try:
         return gl.users.search(args['query'])
     except Exception as e:
         cli.die("Impossible to get user %s" % args['query'], e)
Exemplo n.º 56
0
    def test_die(self):
        with self.assertRaises(SystemExit) as test:
            cli.die("foobar")

        self.assertEqual(test.exception.code, 1)
Exemplo n.º 57
0
 def do_project_milestone_issues(self, cls, gl, what, args):
     try:
         o = self.do_get(cls, gl, what, args)
         return o.issues()
     except Exception as e:
         cli.die("Impossible to get milestone issues", e)
Exemplo n.º 58
0
 def do_project_commit_cherrypick(self, cls, gl, what, args):
     try:
         o = self.do_get(cls, gl, what, args)
         o.cherry_pick(branch=args['branch'])
     except Exception as e:
         cli.die("Impossible to cherry-pick commit", e)
Exemplo n.º 59
0
 def do_user_search(self, cls, gl, what, args):
     try:
         return gl.users.search(args['query'])
     except Exception as e:
         cli.die("Impossible to search users", e)