示例#1
0
def get_protocol(name):
    """Get the service URL protocol.

    Inspects the value of ``issue.<service>.https`` for ``0`` or ``false``
    to assertain the URL schema to use, defaults to ``'https'`` if the
    config setting is not found, ``'http'`` is returned otherwise.

    Arguments:
        :name: Name of the service.

    Returns:
        :str: ``'https'`` or ``'http'`` depending on the value of
        ``issue.<service>.https`` config setting.
    """
    url = get_url(name)
    if url:
        _check_service_url_(name, url)
        return parse(url).protocol
    try:
        use_https = get_config('issue.%s.https' % name)
        if use_https == '0' or use_https == 'false':
            return 'http'
    except CalledProcessError:
        pass
    return 'https'
示例#2
0
def get_repo_owner_name(name):
    """Get the owner/name of the remote.

    Arguments:
        :name: Name of the service.
    """
    try:
        url = get_config('issue.%s.url' % name)
        _check_service_url_(name, url)
    except CalledProcessError:
        try:
            url = get_config('remote.%s.url' % get_remote(name))
        except CalledProcessError:
            raise GitIssueError(
                'failed to determine repository HTTP URL, specify using:\n'
                'git config issue.%s.url <url>' % name)
    remote = parse(url)
    return '%s/%s' % (remote.owner, remote.name)
示例#3
0
def get_remote(name):
    """Get the Git remote, default to ``'origin'``.

    Arguments:
        :name: Name of the service.
    """
    try:
        remote = get_config('issue.%s.remote' % name)
    except CalledProcessError:
        remote = 'origin'
    return remote
示例#4
0
def get_token(name):
    """Get basic authentication header from API token.

    Arguments:
        :name: Name of the service.
    """
    try:
        token = get_config('issue.%s.token' % name)
    except CalledProcessError:
        raise GitIssueError('failed to get {0} API token, specify using:\n'
                            'git config issue.{0}.token <token>'.format(name))
    return token
示例#5
0
def get_url(name):
    """Get the service URL.

    Arguments:
        :name: Name of the service.

    Returns:
        :str: If ``issue.<service>.url`` is set.
        :None: If ``issue.<service>.url`` is not set.
    """
    try:
        return get_config('issue.%s.url' % name)
    except CalledProcessError:
        pass
示例#6
0
def get_resource(name):
    """Get the resource from the Git remote URL.

    Arguments:
        :name: Name of the service.

    Raises:
        :GitIssueError: If the HTTP URL could not be determined.
    """
    url = get_url(name)
    if url:
        _check_service_url_(name, url)
    else:
        try:
            url = get_config('remote.%s.url' % get_remote(name))
        except CalledProcessError:
            raise GitIssueError(
                'failed to determine service HTTP URL, specify using:\n'
                'git config issue.%s.url <url>' % name)
    return parse(url).resource
示例#7
0
def _editor_(template='\n'):
    try:
        editor = get_config('core.editor')
    except CalledProcessError:
        editor = environ.get('EDITOR', None)
    if not editor:
        # TODO: Check if vim/vi/nano exist and use that
        # TODO: Use the default editor on Windows
        editor = 'vi'
    path = join(_git_dir_(), 'ISSUEMSG')
    with open(path, 'w') as issuemsg:
        issuemsg.write(template)
    # TODO: Support configurable filetype
    check_call(
        [editor, '+setfiletype markdown' if 'vim' in editor else '', path])
    with open(path, 'r') as issuemsg:
        message = issuemsg.read().splitlines()
        if len(message) == 0:
            raise GitIssueError('aborted due to empty message')
    remove(path)
    return message
示例#8
0
def main():
    """Main entry point."""
    try:
        warnings.showwarning = lambda *args: _warn_(args[0].message)

        parser = ArgumentParser()
        parser.add_argument('-d',
                            '--debug',
                            action='store_true',
                            help=SUPPRESS)
        subparsers = parser.add_subparsers()

        create_parser = subparsers.add_parser('create')
        create_parser.set_defaults(_command_=create)
        create_parser.add_argument('-m', '--message')
        create_parser.add_argument('-a', '--assignee')
        create_parser.add_argument('-s', '--milestone')
        create_parser.add_argument('-l',
                                   '--label',
                                   action='append',
                                   dest='labels')

        edit_parser = subparsers.add_parser('edit')
        edit_parser.set_defaults(_command_=edit)
        edit_group = edit_parser.add_mutually_exclusive_group()
        edit_group.add_argument('-m', '--message')
        edit_group.add_argument('-n', '--no-message', action='store_true')
        edit_parser.add_argument('-a', '--assignee')
        edit_parser.add_argument('-s', '--milestone')
        edit_parser.add_argument('-l',
                                 '--label',
                                 action='append',
                                 dest='labels')
        edit_parser.add_argument('number')

        comment_parser = subparsers.add_parser('comment')
        comment_parser.set_defaults(_command_=comment)
        comment_parser.add_argument('-m', '--message')
        comment_parser.add_argument('number')

        close_parser = subparsers.add_parser('close')
        close_parser.set_defaults(_command_=close)
        close_parser.add_argument('-m', '--message')
        close_parser.add_argument('-n', '--no-message', action='store_true')
        close_parser.add_argument('number')

        reopen_parser = subparsers.add_parser('reopen')
        reopen_parser.set_defaults(_command_=reopen)
        reopen_parser.add_argument('number')

        show_parser = subparsers.add_parser('show')
        show_parser.set_defaults(_command_=show)
        show_parser.add_argument('--summary', action='store_true')
        show_parser.add_argument('-q', '--quiet', action='store_true')
        show_parser.add_argument('number')

        list_parser = subparsers.add_parser('list')
        list_parser.set_defaults(_command_=list)
        list_parser.add_argument('--oneline', action='store_true')
        list_parser.add_argument('state', default='open', nargs='?')

        browse_parser = subparsers.add_parser('browse')
        browse_parser.set_defaults(_command_=browse)
        browse_parser.add_argument('-u', '--url', action='store_true')
        browse_parser.add_argument('number', nargs='?')

        complete_parser = subparsers.add_parser('complete')
        complete_parser.set_defaults(_command_=complete)
        complete_parser.add_argument(
            'type', choices=['issues', 'labels', 'milestones', 'states'])
        complete_parser.add_argument('--state',
                                     choices=['all', 'open', 'closed'])

        args = vars(parser.parse_args())
        debug = args.pop('debug')
        command = args.pop('_command_')
        command(get_service(), **args)
    except GitIssueError as error:
        if debug:
            _print_exception_()
        _error_(error.message)
    except ConnectionError:
        if debug:
            _print_exception_()
        service_name = get_config('issue.service')
        _error_('authentication failed, check that:'
                '\n* issue.{0}.token is correct'
                '\n* issue.{0}.url is correct, if applicable'
                '\n* issue.{0}.remote is correct, if applicable'
                '\n* issue.{0}.https is correct, if applicable'.format(
                    service_name))
    except KeyboardInterrupt:
        exit(130)