Beispiel #1
0
def git_pull_request(target_remote=None,
                     target_branch=None,
                     title=None,
                     message=None,
                     comment=None,
                     rebase=True,
                     force_editor=False,
                     download=None,
                     download_setup=False,
                     tag_previous_revision=False,
                     fork=True,
                     setup_only=False,
                     branch_prefix=None,
                     dry_run=False,
                     labels=None):
    branch = git_get_branch_name()
    if not branch:
        LOG.critical("Unable to find current branch")
        return 10

    LOG.debug("Local branch name is `%s'", branch)

    target_branch = (target_branch or git_get_remote_branch_for_branch(branch))

    if not target_branch:
        target_branch = "master"
        LOG.info(
            "No target branch configured for local branch `%s', using `%s'.\n"
            "Use the --target-branch option to override.", branch,
            target_branch)

    target_remote = target_remote or git_get_remote_for_branch(target_branch)
    if not target_remote:
        LOG.critical("Unable to find target remote for target branch `%s'",
                     target_branch)
        return 20

    LOG.debug("Target remote for branch `%s' is `%s'", target_branch,
              target_remote)

    target_url = git_remote_url(target_remote)
    if not target_url:
        LOG.critical("Unable to find remote URL for remote `%s'",
                     target_remote)
        return 30

    LOG.debug("Remote URL for remote `%s' is `%s'", target_remote, target_url)

    hosttype, hostname, user_to_fork, reponame_to_fork = (
        get_hosttype_hostname_user_repo_from_url(target_url))
    LOG.debug("%s user and repository to fork: %s/%s on %s",
              hosttype.capitalize(), user_to_fork, reponame_to_fork, hostname)

    user, password = get_login_password(host=hostname)
    if not user and not password:
        LOG.critical(
            "Unable to find your credentials for %s.\n"
            "Make sure you have a git credential working.",
            hostname,
        )
        return 35

    LOG.debug("Found %s user: `%s' password: <redacted>", hostname, user)

    if hosttype == "pagure":
        g = pagure.Client(hostname, user, password, reponame_to_fork)
        repo = g.get_repo(reponame_to_fork)
    else:
        kwargs = {}
        if hostname != "github.com":
            kwargs['base_url'] = "https://" + hostname + "/api/v3"
            LOG.debug("Using API base url `%s'", kwargs['base_url'])
        g = github.Github(user, password, **kwargs)
        repo = g.get_user(user_to_fork).get_repo(reponame_to_fork)

    if download is not None:
        retcode = download_pull_request(g, repo, target_remote, download,
                                        download_setup)

    else:
        retcode = fork_and_push_pull_request(
            g,
            hosttype,
            repo,
            rebase,
            target_remote,
            target_branch,
            branch,
            user,
            title,
            message,
            comment,
            force_editor,
            tag_previous_revision,
            fork,
            setup_only,
            branch_prefix,
            dry_run,
            labels,
        )

    approve_login_password(host=hostname, user=user, password=password)

    return retcode
Beispiel #2
0
def git_pull_request(target_remote=None,
                     target_branch=None,
                     title=None,
                     message=None,
                     comment=None,
                     rebase=True,
                     force_editor=False,
                     download=None,
                     tag_previous_revision=False,
                     dont_fork=True):
    branch = git_get_branch_name()
    if not branch:
        LOG.critical("Unable to find current branch")
        return 10

    LOG.debug("Local branch name is `%s'", branch)

    target_branch = (target_branch or git_get_remote_branch_for_branch(branch))

    if not target_branch:
        target_branch = "master"
        LOG.info(
            "No target branch configured for local branch `%s', using `%s'.\n"
            "Use the --target-branch option to override.", branch,
            target_branch)

    target_remote = target_remote or git_get_remote_for_branch(target_branch)
    if not target_remote:
        LOG.critical("Unable to find target remote for target branch `%s'",
                     target_branch)
        return 20

    LOG.debug("Target remote for branch `%s' is `%s'", target_branch,
              target_remote)

    target_url = git_remote_url(target_remote)
    if not target_url:
        LOG.critical("Unable to find remote URL for remote `%s'",
                     target_remote)
        return 30

    LOG.debug("Remote URL for remote `%s' is `%s'", target_remote, target_url)

    hosttype, hostname, user_to_fork, reponame_to_fork = (
        get_hosttype_hostname_user_repo_from_url(target_url))
    LOG.debug("%s user and repository to fork: %s/%s on %s",
              hosttype.capitalize(), user_to_fork, reponame_to_fork, hostname)

    user, password = get_login_password(hostname)
    if not user or not password:
        LOG.critical(
            "Unable to find your GitHub credentials for %s.\n"
            "Make sure you have a line like this in your ~/.netrc file:\n"
            "machine %s login <login> password <pwd>, "
            "or use git store credentials", hostname, hostname)
        return 35

    LOG.debug("Found %s user: `%s' password: <redacted>", hostname, user)

    kwargs = {}
    if hosttype == "pagure":
        g = pagure.Client(hostname, user, password, reponame_to_fork)
        repo = g.get_repo(reponame_to_fork)
    else:
        if hostname != "github.com":
            kwargs['base_url'] = "https://" + hostname + "/api/v3"
            LOG.debug("Using API base url `%s'", kwargs['base_url'])
        g = github.Github(user, password, **kwargs)
        repo = g.get_user(user_to_fork).get_repo(reponame_to_fork)

    if download is not None:
        download_pull_request(g, repo, target_remote, download)
    else:
        fork_and_push_pull_request(g, repo, rebase, target_remote,
                                   target_branch, branch, user, title, message,
                                   comment, force_editor,
                                   tag_previous_revision, dont_fork)