repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

pi/agent/skills/gh-cli/scripts/gh_failed_run.py

Raw
File outline
#!/usr/bin/env python3
"""
GitHub Actions Failed Run Analyzer

This script finds the most recent failed GitHub Actions run (or a specific run)
and provides detailed information about what went wrong, including error excerpts from logs.
"""

import argparse
import json
import subprocess
import sys
import re
from typing import Optional, Dict, List, Any


# Comprehensive ANSI escape sequence patterns
ANSI_PATTERNS = [
    r'\x1b\[[0-9;]*m',       # SGR (color/style)
    r'\x1b\[[0-9;]*;[0-9;]*m',  # Extended SGR
    r'\x1b\][^\x07]*\x07',   # OSC (title, etc)
    r'\x1b\[[0-9;]*H',       # Cursor position
    r'\x1b\[([0-9]+)?J',     # Erase display
    r'\x1b\[([0-9]+)?K',     # Erase line
    r'\x1b\[[0-9;]*;[0-9;]*H',  # Cursor position
    r'\r',                   # Carriage return (keep newlines)
]


def clean_ansi(text: str) -> str:
    """Remove all ANSI escape sequences from text."""
    result = text
    for pattern in ANSI_PATTERNS:
        result = re.sub(pattern, '', result)
    return result


def check_gh_installed() -> bool:
    """Check if gh CLI is installed and authenticated."""
    try:
        result = subprocess.run(
            ["gh", "auth", "status"],
            capture_output=True,
            text=True,
            check=False
        )
        return result.returncode == 0
    except FileNotFoundError:
        return False


def run_gh_command(cmd: List[str], timeout: int = 60) -> Dict[str, Any]:
    """
    Execute a gh CLI command and return parsed JSON output.

    Args:
        cmd: List of command arguments to pass to gh
        timeout: Timeout in seconds

    Returns:
        Parsed JSON output from the command

    Raises:
        SystemExit: If gh command fails
    """
    try:
        result = subprocess.run(
            cmd,
            capture_output=True,
            text=True,
            check=True,
            timeout=timeout
        )
        return json.loads(result.stdout) if result.stdout else {}
    except subprocess.TimeoutExpired:
        print(f"Error: gh command timed out after {timeout}s", file=sys.stderr)
        sys.exit(1)
    except subprocess.CalledProcessError as e:
        print(f"Error running gh command: {' '.join(cmd)}", file=sys.stderr)
        print(f"Error: {e.stderr}", file=sys.stderr)
        sys.exit(1)
    except json.JSONDecodeError as e:
        print(f"Error parsing JSON output from gh command", file=sys.stderr)
        print(f"Error: {e}", file=sys.stderr)
        sys.exit(1)


def get_run_by_id(run_id: int, repo: Optional[str] = None) -> Optional[Dict[str, Any]]:
    """
    Get a specific workflow run by ID.

    Args:
        run_id: The workflow run database ID
        repo: Optional repository in format 'owner/name'

    Returns:
        Run information dictionary or None if not found
    """
    cmd = [
        "gh", "run", "view", str(run_id),
        "--json", "databaseId,number,conclusion,status,createdAt,displayTitle,url,headBranch,headSha,event"
    ]

    if repo:
        cmd.extend(["--repo", repo])

    try:
        runs = run_gh_command(cmd)
        return runs if runs else None
    except SystemExit:
        return None


def get_most_recent_failed_run(repo: Optional[str] = None) -> Optional[Dict[str, Any]]:
    """
    Get the most recent failed workflow run.

    Args:
        repo: Optional repository in format 'owner/name'

    Returns:
        Run information dictionary or None if no failed runs found
    """
    cmd = [
        "gh", "run", "list",
        "--status", "failure",
        "--limit", "1",
        "--json", "databaseId,number,conclusion,status,createdAt,displayTitle,url,headBranch,headSha,event"
    ]

    if repo:
        cmd.extend(["--repo", repo])

    runs = run_gh_command(cmd)

    if not runs:
        return None

    return runs[0]


def get_failed_jobs(run_id: int, repo: Optional[str] = None) -> List[Dict[str, Any]]:
    """
    Get all jobs from a run and filter for failed ones.

    Args:
        run_id: The workflow run database ID
        repo: Optional repository in format 'owner/name'

    Returns:
        List of failed job dictionaries
    """
    cmd = [
        "gh", "run", "view", str(run_id),
        "--json", "jobs"
    ]

    if repo:
        cmd.extend(["--repo", repo])

    result = run_gh_command(cmd)
    jobs = result.get("jobs", [])

    # Filter for failed jobs (conclusion is failure, timed_out, cancelled, etc.)
    failed_jobs = [
        job for job in jobs
        if job.get("conclusion") not in ["success", "skipped", None]
    ]

    return failed_jobs


def extract_error_excerpts(log_text: str, max_lines: int = 50) -> List[str]:
    """
    Extract relevant error lines from log text.

    Args:
        log_text: Raw log text from failed job
        max_lines: Maximum number of error lines to return

    Returns:
        List of error excerpt strings
    """
    error_patterns = [
        r".*\berror\b.*",
        r".*\bfailed\b.*",
        r".*\bfailure\b.*",
        r".*\bexception\b.*",
        r".*\bERROR\b.*",
        r".*\bFAILED\b.*",
        r".*\bFAILURE\b.*",
        r".*\bEXCEPTION\b.*",
        r".*\bcannot\b.*",
        r".*\bpanic\b.*",
        r".*Process completed with exit code [1-9].*",
        r".*\btimeout\b.*",
    ]

    excerpts = []
    lines = log_text.split('\n')

    for line in lines:
        # Clean ANSI codes and normalize whitespace
        clean_line = clean_ansi(line).strip()

        if not clean_line:
            continue

        # Skip GitHub Actions metadata lines (but keep error messages)
        if re.match(r'^##\[command\]', clean_line):
            continue
        if re.match(r'^##\[group\]', clean_line):
            continue
        if re.match(r'^##\[endgroup\]', clean_line):
            continue

        # Check if line matches any error pattern
        for pattern in error_patterns:
            if re.search(pattern, clean_line, re.IGNORECASE):
                if clean_line not in excerpts:
                    excerpts.append(clean_line)
                    if len(excerpts) >= max_lines:
                        return excerpts
                break

    return excerpts


def get_job_logs(run_id: int, job_name: str, repo: Optional[str] = None) -> str:
    """
    Get logs for a specific failed job.

    Args:
        run_id: The workflow run database ID
        job_name: The name of the job
        repo: Optional repository in format 'owner/name'

    Returns:
        Log text for the job
    """
    cmd = [
        "gh", "run", "view", str(run_id),
        "--log-failed",
        "--job", job_name
    ]

    if repo:
        cmd.extend(["--repo", repo])

    try:
        result = subprocess.run(
            cmd,
            capture_output=True,
            text=True,
            check=True,
            timeout=120
        )
        return result.stdout
    except subprocess.CalledProcessError:
        # If specific job logs fail, try getting all failed logs
        cmd_all = [
            "gh", "run", "view", str(run_id),
            "--log-failed"
        ]
        if repo:
            cmd_all.extend(["--repo", repo])

        try:
            result = subprocess.run(
                cmd_all,
                capture_output=True,
                text=True,
                check=True,
                timeout=120
            )
            return result.stdout
        except subprocess.CalledProcessError:
            return ""


def analyze_failed_run(repo: Optional[str] = None, run_id: Optional[int] = None) -> Dict[str, Any]:
    """
    Analyze a failed run and extract all relevant information.

    Args:
        repo: Optional repository in format 'owner/name'
        run_id: Optional specific run ID to analyze

    Returns:
        Dictionary containing run analysis
    """
    # Get run info
    if run_id:
        run = get_run_by_id(run_id, repo)
    else:
        run = get_most_recent_failed_run(repo)

    if not run:
        return {
            "error": f"No failed runs found{f' for run {run_id}' if run_id else ''}",
            "repository": repo or "current"
        }

    # Get failed jobs using databaseId
    failed_jobs = get_failed_jobs(run["databaseId"], repo)

    # Build result structure
    result = {
        "run": {
            "number": run["number"],
            "database_id": run["databaseId"],
            "url": run["url"],
            "workflow": run["displayTitle"],
            "conclusion": run["conclusion"],
            "status": run["status"],
            "created_at": run["createdAt"],
            "branch": run.get("headBranch"),
            "commit": run.get("headSha"),
            "event": run.get("event")
        },
        "failed_jobs": [],
        "repository": repo or "current"
    }

    # Analyze each failed job
    for job in failed_jobs:
        job_info = {
            "name": job["name"],
            "conclusion": job["conclusion"],
            "status": job.get("status"),
            "started_at": job.get("startedAt"),
            "completed_at": job.get("completedAt"),
            "error_excerpts": []
        }

        # Get logs and extract errors using databaseId
        logs = get_job_logs(run["databaseId"], job["name"], repo)
        if logs:
            job_info["error_excerpts"] = extract_error_excerpts(logs)

        result["failed_jobs"].append(job_info)

    return result


def main():
    """Main entry point for the script."""
    parser = argparse.ArgumentParser(
        description="Analyze GitHub Actions failed runs and extract error information",
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog="""
Examples:
  %(prog)s                        # Analyze current repository
  %(prog)s --repo owner/name      # Analyze specific repository
  %(prog)s --run-id 12345         # Analyze specific run ID
  %(prog)s --json                 # Output as compact JSON
  %(prog)s --pretty               # Output as pretty JSON
        """
    )

    parser.add_argument(
        "--repo",
        type=str,
        help="Repository to analyze in format 'owner/name'"
    )

    parser.add_argument(
        "--run-id",
        type=int,
        help="Specific run ID to analyze"
    )

    parser.add_argument(
        "--pretty",
        action="store_true",
        help="Pretty-print JSON output with indentation"
    )

    parser.add_argument(
        "--json",
        action="store_true",
        help="Output as JSON (alias for machine-readable)"
    )

    parser.add_argument(
        "--timeout",
        type=int,
        default=120,
        help="Timeout for fetching logs in seconds (default: 120)"
    )

    args = parser.parse_args()

    # Check if gh CLI is installed
    if not check_gh_installed():
        print("Error: gh CLI is not installed or not in PATH", file=sys.stderr)
        print("Install it from: https://cli.github.com/", file=sys.stderr)
        sys.exit(1)

    # Analyze the failed run
    result = analyze_failed_run(args.repo, args.run_id)

    # Output JSON
    if args.pretty or (args.json and args.pretty):
        print(json.dumps(result, indent=2))
    else:
        print(json.dumps(result))


if __name__ == "__main__":
    main()