Example #1
0
def _add(epic):
    if epic is None:
        raise CommandException('No epic remote/branch given')
    repo = get_repo()
    config = configuration(repo)
    if epic in _list_all(config):
        raise CommandException('Epic %s is already added to the list' % epic)
    config.set('all', '%s,%s' % (config.all, epic))
Example #2
0
def parse_value(rawValue, destType):
    rawValue = str(rawValue)
    if destType is bool:
        if rawValue in ['True', 'true', 'yes', '1']:
            return True
        elif rawValue in ['False', 'false', 'no', '0']:
            return False
        else:
            raise CommandException("Wrong value '%s' (with: %s) for '%s'" % (rawValue, type(rawValue), destType))
    elif destType is str:
        return rawValue
    elif destType is int:
        return int(rawValue)
    else:
        raise CommandException('Unsupported type: %s' % destType)
Example #3
0
def _handle_github_exception(e, event):
    logging.warn('%s raised an exception: %s' % (event, e))
    if 'errors' in e.data:
        error = ', '.join(map(_map_github_error, e.data['errors']))
    else:
        error = e.data['message']
    raise CommandException('Unable to %s: %s' % (event, error))
Example #4
0
def _push():
    repo = get_repo()
    check_repo_is_clean(repo)
    (target_remote, target_branch) = gifi.epic.current()
    base = '%s/%s' % (target_remote, target_branch)
    if repo.head.commit == repo.commit(base):
        raise CommandException(
            'You are currently at %s, there is nothing to push' % base)
    commit_message = repo.head.commit.message
    repo.git.reset('--soft', 'HEAD^')
    repo.git.stash('save', _escape_new_lines(commit_message))
Example #5
0
def _rebase(repo=None, config=None):
    repo = get_repo(repo)
    if config is None:
        config = configuration(repo)
    feature = current(repo)
    _fetch(repo, feature.target_remote)
    interactive = '-i' if config.finish_with_rebase_interactive else ''
    rebase_cmd = 'git rebase %s/%s %s' % (feature.target_remote, feature.target_branch, interactive)
    rebase_status = subprocess.call(rebase_cmd, shell=True)
    if rebase_status != 0:
        message = 'Rebase finished with an error, please fix it manually and then use "git rebase --continue"'
        raise CommandException(message)
Example #6
0
def _pop():
    repo = get_repo()
    check_repo_is_clean(repo)
    if repo.git.stash('list') == '':
        raise CommandException('There is nothing in the queue to pop from.')
    try:
        repo.git.stash('apply', '--index')
    except GitCommandError as e:
        if 'Try without --index' in e.stderr:
            try:
                repo.git.stash('apply')
            except GitCommandError:
                raise CommandException(
                    'Unable to pop automatically. Resolve conflicts then run queue-pop-finish.'
                )
        else:
            raise CommandException(
                'Unable to pop automatically. Resolve conflicts then run queue-pop-finish.'
            )

    _pop_finish()
Example #7
0
def _push_working_branch(config, repo):
    current_branch = _current_feature_branch(repo)
    push_params = [config.working_remote, 'HEAD:%s' % current_branch]
    try:
        repo.git.push('-u', *push_params)
    except GitCommandError as e:
        logging.warn('Unable push (publish) feature branch without force: %s' % e)
        message = 'Unable to push your changes ("git push -u %s %s"). Would you like to use force?'
        question = message % tuple(push_params)
        if ask(question):
            repo.git.push('-f', '-u', *push_params)
        else:
            raise CommandException('Manual pull and rebase is required')
Example #8
0
def get_repo(repo=None):
    """

    :rtype : git.Repo
    """
    if repo is None:
        try:
            repo = Repo('.')
        except InvalidGitRepositoryError:
            raise CommandException(
                'To run this command you need to be in git source code directory.'
            )
    return repo
Example #9
0
def _create_pull_request(repo, message=None):
    feature_config = gifi.feature.configuration(repo)
    f = gifi.feature.current(repo)
    working_remote = feature_config.working_remote
    full_repo_name = get_remote_url(f.target_remote,
                                    repo).split(':')[1].split('.')[0]
    working_namespace = get_remote_url(working_remote,
                                       repo).split(':')[1].split('/')[0]
    current_branch = get_current_branch(repo)

    head = '%s:%s' % (working_namespace, current_branch)
    if f.target_remote is working_remote:
        if current_branch is f.target_branch:
            raise CommandException(
                "Unable to create a pull request from the same remote and branch."
            )
        head = current_branch

    github = _get_github(repo).get_repo(full_repo_name)
    pull_requests = github.get_pulls('open')
    for pull_request in pull_requests:
        html_url = pull_request.html_url
        if head == pull_request.head.label:
            print("Pull request is already created, see: ", html_url)
            return

    epic = '/'.join(current_branch.split('/')[1:-1])
    if epic == 'master':
        epic = ''
    else:
        epic = "(%s) " % epic
    default_title = repo.head.commit.summary
    if message:
        title = message
    else:
        title = ask("Title: ", default_title)
    body = ""
    if title is default_title:
        body = repo.head.commit.message
    pull_request_parameters = {
        'title': ("%s%s" % (epic, title)),
        'body': body,
        'head': head,
        'base': f.target_branch
    }

    logging.debug('Creating pull request with: %s' % pull_request_parameters)
    pull_request = github.create_pull(**pull_request_parameters)
    print('Pull request URL: ', pull_request.html_url)
Example #10
0
def _current_feature_branch(repo):
    current_branch = git_utils.get_current_branch(repo)
    if not current_branch.count('/') > 1:
        raise CommandException('Please checkout to a feature branch.')
    return current_branch
Example #11
0
def _missing_configuration_exception(item):
    return CommandException(
        'No github %s is set, please do configure or authorize github first.' %
        item)
Example #12
0
def check_repo_is_clean(repo):
    if repo.is_dirty():
        raise CommandException('Please commit all untracked files.')