Beispiel #1
0
class BackwardCompatibilityTests(unittest.TestCase):
    def setUp(self):
        tmp_config = tempfile.mktemp()
        self.cfg = Config(tmp_config)
        jiracli.utils.CONFIG_FILE = tmp_config

    def test_jira_cli_v1_invoked(self):
        with mock.patch("jiracli.interface.old_main") as old_main:
            self.cfg.v1 = "1"
            self.cfg.save()
            cli(['--help'])
            self.assertTrue(old_main.call_count == 1)
            self.cfg.v1 = "True"
            self.cfg.save()
            cli(['--help'])
            self.assertTrue(old_main.call_count == 2)

    def test_jira_cli_v2_invoked(self):
        with mock.patch("sys.stdout") as stdout:
            with mock.patch("jiracli.interface.old_main") as old_main:
                with mock.patch(
                        "jiracli.processor.Command.execute") as execute:
                    self.assertRaises(SystemExit, cli, ['--help', '--v2'])
                    self.assertRaises(SystemExit, cli, ['--help'])
                    self.assertRaises(SystemExit, cli, ['--help'])
Beispiel #2
0
def cli(args=sys.argv[1:]):
    parser = build_parser()
    try:
        config = Config()
        pre_opts, pre_args = None, None
        try:
            pre_opts, pre_args = fake_parse(args)
        except StopIteration:
            pre_opts, pre_args = None, None
            if "--v1" in args or config.v1:
                if '--v1' in sys.argv:
                    print_error(
                        "Use of the v1 interface is no longer supported. Please refer to jiracli.readthedocs.io",
                        WARNING
                    )
                    sys.argv.remove("--v1")
                return old_main()
        except SystemExit:
            pass
        if pre_opts and pre_opts.version:
            print(__version__)
            return
        if (
            not (pre_opts or pre_args) or (pre_opts and not (pre_opts.v1 or config.v1))
            and not (pre_opts and ("configure" in pre_args or "clear_cache" in pre_args))
        ):
            post_args = parser.parse_args(args)
            jira = initialize(
                config, post_args.jira_url, post_args.username, post_args.password,
                persist=not (post_args.username or post_args.jira_url),
                protocol=post_args.protocol or config.protocol or 'rest'
            )
            return post_args.cmd(jira, post_args).execute()
        else:
            if "configure" in pre_args:
                config.reset()
                initialize(
                    config, "", "", "", True,
                    protocol=pre_opts.protocol or config.protocol or 'soap'
                )
            elif "clear_cache" in pre_args:
                clear_cache()
                print_output(colorfunc("jira-cli cache cleared", "green"))
            return

    except KeyboardInterrupt:
        print_error("aborted", severity=WARNING)
    except UsageWarning as e:
        print_error(str(e), severity=WARNING)
    except (JiraCliError, UsageError) as e:
        print_error(str(e))
    except (WebFault) as e:
        print_error(JiraCliError(e))
    except (JIRAError) as e:
        print_error(JiraCliError(e))
    except NotImplementedError as e:
        print_error(e)
Beispiel #3
0
def cli(args=sys.argv[1:]):
    parser = build_parser()
    try:
        config = Config()
        pre_opts, pre_args = None, None
        try:
            pre_opts, pre_args = fake_parse(args)
        except StopIteration:
            pre_opts, pre_args = None, None
            if not ("--v2" in args or config.v2):
                return old_main()
        except SystemExit:
            pass
        if pre_opts and pre_opts.version:
            print(__version__)
            return
        if (not (pre_opts or pre_args)
                or (pre_opts and (pre_opts.v2 or config.v2)) and
                not (pre_opts and
                     ("configure" in pre_args or "clear_cache" in pre_args))):
            post_args = parser.parse_args(args)
            jira = initialize(
                config,
                post_args.jira_url,
                post_args.username,
                post_args.password,
                persist=not (post_args.username or post_args.jira_url),
                protocol=post_args.protocol or config.protocol or 'soap')
            return post_args.cmd(jira, post_args).execute()
        else:
            if "configure" in pre_args:
                config.reset()
                initialize(config,
                           "",
                           "",
                           "",
                           True,
                           protocol=pre_opts.protocol or config.protocol
                           or 'soap')
            elif "clear_cache" in pre_args:
                clear_cache()
                print_output(colorfunc("jira-cli cache cleared", "green"))
            return

    except KeyboardInterrupt:
        print_error("aborted", severity=WARNING)
    except UsageWarning as e:
        print_error(str(e), severity=WARNING)
    except (JiraCliError, UsageError) as e:
        print_error(str(e))
    except (WebFault) as e:
        print_error(JiraCliError(e))
    except (JIRAError) as e:
        print_error(JiraCliError(e))
    except NotImplementedError as e:
        print_error(e)
Beispiel #4
0
def cli(args=sys.argv[1:]):
    import optparse
    alias_config = Config(section='alias')
    if set(list(alias_config.items().keys())).intersection(args):
        for alias, target in list(alias_config.items()).items():
            if args[0] == alias:
                args = shlex.split(target) + args[1:]
                break
    parser = build_parser()
    try:
        config = Config()
        pre_opts, pre_args = None, None
        try:
            optparser = optparse.OptionParser()

            def void(*args):
                raise SystemExit()

            optparser.print_usage = void
            optparser.add_option("", "--version", action='store_true', default=False)
            pre_opts, pre_args = optparser.parse_args(args)
        except SystemExit:
            pass
        if pre_opts and pre_opts.version:
            print(__version__)
            return
        if not (pre_opts and ("configure" in pre_args or "clear_cache" in pre_args)):
            post_args = parser.parse_args(args)
            jira = initialize(
                config, post_args.jira_url, post_args.username, post_args.password,
                persist=not (post_args.username or post_args.jira_url),
                protocol='rest'
            )
            return post_args.cmd(jira, post_args).execute()
        else:
            if "configure" in pre_args:
                config.reset()
                initialize(
                    config, "", "", "", True,
                    protocol='rest'
                )
            elif "clear_cache" in pre_args:
                clear_cache()
                print_output(colorfunc("jira-cli cache cleared", "green"))
            return

    except KeyboardInterrupt:
        print_error("aborted", severity=WARNING)
    except UsageWarning as e:
        print_error(str(e), severity=WARNING)
    except (JiraCliError, UsageError) as e:
        print_error(str(e))
    except (WebFault) as e:
        print_error(JiraCliError(e))
    except (JIRAError) as e:
        print_error(JiraCliError(e))
    except NotImplementedError as e:
        print_error(e)
Beispiel #5
0
def cli(args=sys.argv[1:]):
    parser = build_parser()

    try:
        config = Config()
        pre_opts, pre_args = None, None
        try:
            pre_opts, pre_args = fake_parse(args)
        except StopIteration:
            pre_opts, pre_args = None, None
            if not ("--v2" in args or config.v2):
                return old_main()
        except SystemExit:
            pass
        if (
            not (pre_opts or pre_args) or (pre_opts and pre_opts.v2)
            and not (pre_opts and ("configure" in pre_args or "clear_cache" in pre_args))
        ):
            post_args = parser.parse_args(args)
            jira = initialize(
                config, post_args.jira_url, post_args.username, post_args.password,
                persist=not (post_args.username or post_args.jira_url),
                protocol=post_args.protocol or config.protocol
            )
            return post_args.cmd(jira, post_args).execute()
        else:
            if "configure" in pre_args:
                config.reset()
                initialize(config, "", "", "", True)
            elif "clear_cache" in pre_args:
                clear_cache()
                print_output(colorfunc("jira-cli cache cleared", "green"))
            return

    except KeyboardInterrupt:
        print_error("aborted", severity=WARNING)
    except UsageWarning as e:
        print_error(str(e), severity=WARNING)
    except (JiraCliError, UsageError) as e:
        print_error(str(e))
    except (WebFault) as e:
        print_error(JiraCliError(e))
    except (JIRAError) as e:
        print_error(JiraCliError(e))
    except NotImplementedError as e:
        print_error(e)
Beispiel #6
0
    def test_aliases(self):
        tmp_config = tempfile.mktemp()
        open(tmp_config, 'w').write("""
[jira]
username = testuser
base = http://foo.bar

[alias]
test_alias = view TEST-123
        """)
        config = Config(tmp_config)
        jiracli.utils.CONFIG_FILE = tmp_config
        config.save()
        with mock.patch("jiracli.interface.prompt"):
            with mock.patch("jiracli.interface.initialize") as initialize:
                cli(["test_alias"])
                self.assertEqual(initialize.return_value.get_issue.call_count, 1)
Beispiel #7
0
    def test_aliases(self):
        tmp_config = tempfile.mktemp()
        open(tmp_config, 'w').write("""
[jira]
username = testuser
base = http://foo.bar

[alias]
test_alias = view TEST-123
        """)
        config = Config(tmp_config)
        jiracli.utils.CONFIG_FILE = tmp_config
        config.save()
        with mock.patch("jiracli.interface.prompt"):
            with mock.patch("jiracli.interface.initialize") as initialize:
                cli(["test_alias"])
                self.assertEqual(initialize.return_value.get_issue.call_count,
                                 1)
Beispiel #8
0
class BackwardCompatibilityTests(unittest.TestCase):
    def setUp(self):
        tmp_config = tempfile.mktemp()
        self.cfg = Config(tmp_config)
        jiracli.utils.CONFIG_FILE = tmp_config

    def test_jira_cli_v1_invoked(self):
        with mock.patch("jiracli.interface.old_main") as old_main:
            cli(['--help'])
            self.assertTrue(old_main.call_count==1)
            self.cfg.v2 = "0"
            self.cfg.save()
            cli(['--help'])
            self.assertTrue(old_main.call_count==2)
            self.cfg.v2 = "False"
            self.cfg.save()
            cli(['--help'])
            self.assertTrue(old_main.call_count==3)

    def test_jira_cli_v2_invoked(self):
        with mock.patch("sys.stdout") as stdout:
            with mock.patch("jiracli.interface.old_main") as old_main:
                with mock.patch("jiracli.processor.Command.execute") as execute:
                    self.assertRaises(SystemExit, cli, ['--help', '--v2'])
                    self.cfg.v2 = "True"
                    self.cfg.save()
                    self.assertRaises(SystemExit, cli, ['--help'])
                    self.cfg.v2 = "1"
                    self.assertRaises(SystemExit, cli, ['--help'])
Beispiel #9
0
 def eval(self):
     mappers = {
         "issue_types": (self.jira.get_issue_types, ),
         'subtask_types': (self.jira.get_subtask_issue_types, ),
         'projects': (self.jira.get_projects, ),
         'priorities': (self.jira.get_priorities, ),
         'statuses': (self.jira.get_statuses, ),
         'resolutions': (self.jira.get_resolutions, ),
         'components': (self.jira.get_components, 'project'),
         'versions': (self.jira.list_versions, 'project'),
         'transitions': (self.jira.get_available_transitions, 'issue'),
         'filters': (self.jira.get_filters, ),
         'aliases': (lambda: [{
             "name": k,
             "description": v
         } for k, v in list(Config(section='alias').items()).items()], )
     }
     func, arguments = mappers[self.args.type][0], mappers[
         self.args.type][1:]
     _ = []
     _k = {}
     for earg in arguments:
         if isinstance(earg, tuple):
             if getattr(self.args, earg[0]):
                 _k.update({earg[0]: getattr(self.args, earg[0])})
             else:
                 _k[earg[0]] = earg[1]
         else:
             if not getattr(self.args, earg):
                 raise UsageError("'--%s' is required for listing '%s'" %
                                  (earg, self.args.type))
             _.append(getattr(self.args, earg))
     found = False
     data = func(*_, **_k)
     data_dict = OrderedDict()
     if type(data) == type([]):
         for item in data:
             data_dict[item['name']] = item
     else:
         data_dict = data
     for item in list(data_dict.values()):
         found = True
         val = item
         if type(item) == type({}):
             val = colorfunc(item['name'], 'white')
             if 'key' in item and item['key']:
                 val += " [" + colorfunc(item['key'], 'magenta') + "]"
             if 'description' in item and item['description']:
                 val += " [" + colorfunc(item['description'], 'green') + "]"
         print_output(colorfunc(val, 'white'))
     if not found:
         raise UsageWarning("No %s found." % self.args.type)
Beispiel #10
0
 def setUp(self):
     tmp_config = tempfile.mktemp()
     self.config = Config(tmp_config)
     jiracli.utils.CONFIG_FILE = tmp_config
     self.cache_dir = tempfile.mkdtemp()
     jiracli.cache.CACHE_DIR = self.cache_dir
     self.config.username = "******"
     self.config.password = "******"
     self.vcr_directory = "fixtures/rest"
     with jiravcr.use_cassette(
             os.path.join(self.vcr_directory, "login.yaml")):
         self.bridge = JiraRestBridge("https://indydevs.atlassian.net",
                                      self.config)
         self.bridge.login(self.config.username, self.config.password)
Beispiel #11
0
 def setUp(self):
     tmp_config = tempfile.mktemp()
     self.cfg = Config(tmp_config)
     jiracli.utils.CONFIG_FILE = tmp_config
Beispiel #12
0
def cli(args=sys.argv[1:]):
    alias_config = Config(section='alias')
    if set(alias_config.items().keys()).intersection(args):
        for alias, target in alias_config.items().items():
            if args[0] == alias:
                args = shlex.split(target) + args[1:]
                break
    parser = build_parser()
    try:
        config = Config()
        pre_opts, pre_args = None, None
        try:
            pre_opts, pre_args = fake_parse(args)
        except StopIteration:
            pre_opts, pre_args = None, None
            if "--v1" in args or config.v1:
                if '--v1' in sys.argv:
                    print_error(
                        "Use of the v1 interface is no longer supported. Please refer to jiracli.readthedocs.io",
                        WARNING)
                    sys.argv.remove("--v1")
                return old_main()
        except SystemExit:
            pass
        if pre_opts and pre_opts.version:
            print(__version__)
            return
        if (not (pre_opts or pre_args)
                or (pre_opts and not (pre_opts.v1 or config.v1)) and
                not (pre_opts and
                     ("configure" in pre_args or "clear_cache" in pre_args))):
            post_args = parser.parse_args(args)
            jira = initialize(
                config,
                post_args.jira_url,
                post_args.username,
                post_args.password,
                persist=not (post_args.username or post_args.jira_url),
                protocol=post_args.protocol or config.protocol or 'rest')
            return post_args.cmd(jira, post_args).execute()
        else:
            if "configure" in pre_args:
                config.reset()
                initialize(config,
                           "",
                           "",
                           "",
                           True,
                           protocol=pre_opts.protocol or config.protocol
                           or 'soap')
            elif "clear_cache" in pre_args:
                clear_cache()
                print_output(colorfunc("jira-cli cache cleared", "green"))
            return

    except KeyboardInterrupt:
        print_error("aborted", severity=WARNING)
    except UsageWarning as e:
        print_error(str(e), severity=WARNING)
    except (JiraCliError, UsageError) as e:
        print_error(str(e))
    except (WebFault) as e:
        print_error(JiraCliError(e))
    except (JIRAError) as e:
        print_error(JiraCliError(e))
    except NotImplementedError as e:
        print_error(e)
Beispiel #13
0
 def setUp(self):
     tmp_config = tempfile.mktemp()
     self.cfg = Config(tmp_config)
     jiracli.utils.CONFIG_FILE = tmp_config
Beispiel #14
0
 def setUp(self):
     self.stderr_patcher = mock.patch("sys.stderr")
     self.stderr = self.stderr_patcher.start()
     tmp_config = tempfile.mktemp()
     self.cfg = Config(tmp_config)