Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

2.6 KB
Raw Blame
from posthog.models.github_integration_base import GitHubIntegrationBase


def canonical_github_pull_request_url(url: str) -> str | None:
    parsed = GitHubIntegrationBase.parse_pull_request_url(url)
    return f"https://github.com/{parsed.repository.lower()}/pull/{parsed.number}" if parsed else None


def normalize_verified_pull_request_urls(value: object) -> list[object]:
    urls = value if isinstance(value, list) else []
    normalized: list[object] = []
    for url in urls:
        if url not in normalized:
            normalized.append(url)
        canonical = canonical_github_pull_request_url(url) if isinstance(url, str) else None
        if canonical is not None and canonical not in normalized:
            normalized.append(canonical)
    return normalized


def read_pr_urls(output: object) -> list[str]:
    if not isinstance(output, dict):
        return []

    raw_urls = output.get("pr_urls")
    urls: list[object] = raw_urls if isinstance(raw_urls, list) else []
    pr_url = output.get("pr_url")
    candidates = [*urls, pr_url]
    return list(dict.fromkeys(url for url in candidates if isinstance(url, str) and url))


def read_head_branches(output: object) -> list[dict[str, str]]:
    if not isinstance(output, dict):
        return []

    raw_branches = output.get("head_branches")
    branches: list[object] = raw_branches if isinstance(raw_branches, list) else []
    normalized: list[dict[str, str]] = []
    seen: set[tuple[str, str]] = set()
    for candidate in branches:
        if not isinstance(candidate, dict):
            continue
        repository = candidate.get("repository")
        branch = candidate.get("branch")
        if not isinstance(repository, str) or not repository or not isinstance(branch, str) or not branch:
            continue
        pair = (repository.strip().lower(), branch)
        if pair in seen:
            continue
        seen.add(pair)
        normalized.append({"repository": pair[0], "branch": pair[1]})
    return normalized


def merge_pr_output(existing: object, incoming: dict) -> dict:
    current = existing if isinstance(existing, dict) else {}
    merged = {**current, **incoming}
    head_branches = [*read_head_branches(incoming), *read_head_branches(current)]
    if head_branches:
        merged["head_branches"] = read_head_branches({"head_branches": head_branches})
    urls = list(dict.fromkeys([*read_pr_urls(incoming), *read_pr_urls(current)]))
    if not urls:
        return merged

    primary = incoming.get("pr_url") or current.get("pr_url") or urls[0]
    ordered = [primary, *(url for url in urls if url != primary)]
    return {**merged, "pr_url": primary, "pr_urls": ordered}