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)
def _get_id(self, cls, args): try: id = args.pop(cls.idAttr) except Exception: cli.die("Missing --%s argument" % cls.idAttr.replace('_', '-')) return id
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
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)
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
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)
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)
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
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)
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)
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
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
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
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)
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)
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)
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 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
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)
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
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
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
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)
def do_create(self): try: return self.mgr.create(self.args) except Exception as e: cli.die("Impossible to create object", e)
def do_list(self): try: return self.mgr.list(**self.args) except Exception as e: cli.die("Impossible to list objects", e)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
def test_die(self): with self.assertRaises(SystemExit) as test: cli.die("foobar") self.assertEqual(test.exception.code, 1)
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)