Ejemplo n.º 1
0
def get_github_pull_from_event(g, repo, installation_id, event_type, data):
    if "pull_request" in data:
        return github.PullRequest.PullRequest(repo._requester, {},
                                              data["pull_request"],
                                              completed=True)
    elif event_type == "status":
        return get_github_pull_from_sha(g, repo, installation_id, data["sha"])

    elif event_type in ["check_suite", "check_run"]:
        if event_type == "check_run":
            pulls = data["check_run"]["check_suite"]["pull_requests"]
            sha = data["check_run"]["head_sha"]
        else:
            pulls = data["check_suite"]["pull_requests"]
            sha = data["check_suite"]["head_sha"]
        if not pulls:
            return get_github_pull_from_sha(g, repo, installation_id, sha)
        if len(pulls) > 1:  # pragma: no cover
            # NOTE(sileht): It's that technically possible, but really ?
            LOG.warning("check_suite/check_run attached on multiple pulls")

        for p in pulls:

            pull = v1.Caching(
                repository=repo,
                installation_id=installation_id).get_pr_for_pull_number(
                    p["base"]["ref"], p["number"])
            if not pull:
                try:
                    pull = repo.get_pull(p["number"])
                except github.UnknownObjectException:  # pragma: no cover
                    pass

            if pull and not pull.merged:
                return pull
Ejemplo n.º 2
0
def get_github_pull_from_sha(g, repo, installation_id, installation_token,
                             sha):

    # TODO(sileht): Replace this optimisation when we drop engine v1
    pull = v1.Caching(
        repository=repo,
        installation_id=installation_id,
        installation_token=installation_token).get_pr_for_sha(sha)
    if pull:
        return pull

    issues = list(
        g.search_issues("repo:%s is:pr is:open %s" % (repo.full_name, sha)))
    if not issues:
        return
    if len(issues) > 1:  # pragma: no cover
        # NOTE(sileht): It's that technically possible, but really ?
        LOG.warning("sha attached to multiple pull requests", sha=sha)
    for i in issues:
        try:
            pull = repo.get_pull(i.number)
        except github.GithubException as e:  # pragma: no cover
            if e.status != 404:
                raise
        if pull and not pull.merged:
            return pull