import gradio as gr
import os
import time
import markdown
import tempfile
import traceback
from github_repo_analyzer import get_repo_info, clone_repo, analyze_code, analyze_issues, analyze_pull_requests, llm_analyze_code, llm_analyze_issues, llm_analyze_prs, llm_synthesize_findings, generate_report
from github import Github
from openai import OpenAI
# Emojis and fun statements for progress updates
PROGRESS_STEPS = [
("🕵️♂️", "Investigating the GitHub realm..."),
("🧬", "Decoding repository DNA..."),
("🐛", "Hunting for bugs and features..."),
("🔍", "Examining pull request tea leaves..."),
("🧠", "Activating AI brain cells..."),
("📝", "Crafting the legendary report..."),
]
def analyze_github_repo(repo_input, github_token=None):
if github_token:
os.environ["GITHUB_TOKEN"] = github_token
github_token = os.environ.get("GITHUB_TOKEN")
if not github_token:
return "❌ Error: GITHUB_TOKEN environment variable not set."
openrouter_api_key = os.environ.get("OPENROUTER_API_KEY")
if not openrouter_api_key:
return "❌ Error: OPENROUTER_API_KEY environment variable not set."
progress_html = ""
yield progress_html # Initial empty output
for emoji, message in PROGRESS_STEPS:
progress_html += f"{emoji} {message}
"
yield progress_html
time.sleep(1) # Reduced sleep time for faster feedback
try:
owner, repo_name = get_repo_info(repo_input)
repo_url = f"https://github.com/{owner}/{repo_name}"
g = Github(github_token)
github_repo = g.get_repo(f"{owner}/{repo_name}")
client = OpenAI(api_key=openrouter_api_key, base_url="https://openrouter.ai/api/v1")
max_issues = 4
max_prs = 4
with tempfile.TemporaryDirectory() as temp_dir:
repo_path = clone_repo(owner, repo_name, temp_dir)
progress_html += "🔬 Analyzing code structure...
"
yield progress_html
code_analysis = analyze_code(repo_path)
code_analysis['llm_analysis'] = llm_analyze_code(client, code_analysis)
progress_html += f"📊 Analyzing issues (max {max_issues})...
"
yield progress_html
issues_data = analyze_issues(github_repo, max_issues)
issues_analysis = llm_analyze_issues(client, issues_data, repo_url)
progress_html += f"🔀 Analyzing pull requests (max {max_prs})...
"
yield progress_html
prs_data = analyze_pull_requests(github_repo, max_prs)
pr_analysis = llm_analyze_prs(client, prs_data, repo_url)
progress_html += "🧠 Synthesizing findings...
"
yield progress_html
final_analysis = llm_synthesize_findings(
client,
code_analysis.get('llm_analysis', ''),
issues_analysis.get('summary', ''),
pr_analysis.get('summary', '')
)
repo_info = {
"owner": owner,
"repo_name": repo_name,
}
progress_html += "📝 Generating report...
"
yield progress_html
report = generate_report(repo_info, code_analysis, issues_analysis, pr_analysis, final_analysis)
# Convert markdown to HTML
html_report = markdown.markdown(report)
# Return the final HTML report
return f"{progress_html}