File size: 4,443 Bytes
86d1445
 
 
 
d6b7027
 
 
 
 
86d1445
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6b7027
 
 
 
 
 
 
 
86d1445
 
 
 
 
 
edc590f
86d1445
 
 
d6b7027
86d1445
d6b7027
 
86d1445
d6b7027
 
edc590f
 
d6b7027
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86d1445
 
 
 
 
 
d6b7027
86d1445
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6b7027
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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 "<p>❌ Error: GITHUB_TOKEN environment variable not set.</p>", ""

    openrouter_api_key = os.environ.get("OPENROUTER_API_KEY")
    if not openrouter_api_key:
        return "<p>❌ Error: OPENROUTER_API_KEY environment variable not set.</p>", ""

    progress_html = ""
    yield progress_html, ""  # Initial empty output

    for emoji, message in PROGRESS_STEPS:
        progress_html += f"<p>{emoji} {message}</p>"
        yield progress_html, ""
        time.sleep(4)  # Simulate work being done

    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 += "<p>πŸ”¬ Analyzing code structure...</p>"
            yield progress_html, ""
            code_analysis = analyze_code(repo_path)
            code_analysis['llm_analysis'] = llm_analyze_code(client, code_analysis)

            progress_html += f"<p>πŸ“Š Analyzing issues (max {max_issues})...</p>"
            yield progress_html, ""
            issues_data = analyze_issues(github_repo, max_issues)
            issues_analysis = llm_analyze_issues(client, issues_data, repo_url)

            progress_html += f"<p>πŸ”€ Analyzing pull requests (max {max_prs})...</p>"
            yield progress_html, ""
            prs_data = analyze_pull_requests(github_repo, max_prs)
            pr_analysis = llm_analyze_prs(client, prs_data, repo_url)

            progress_html += "<p>🧠 Synthesizing findings...</p>"
            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 += "<p>πŸ“ Generating report...</p>"
            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 progress_html + "<p>βœ… Analysis complete!</p>", html_report
    except Exception as e:
        error_message = f"<p>❌ An error occurred: {str(e)}</p>"
        traceback.print_exc()
        return progress_html + error_message, ""

# Define the Gradio interface
with gr.Blocks() as app:
    gr.Markdown("# GitHub Repository Analyzer")
    
    repo_input = gr.Textbox(label="Enter GitHub Repository Slug or URL")
    
    with gr.Accordion("Advanced Settings", open=False):
        github_token = gr.Textbox(label="GitHub Token (optional)", type="password")
    
    analyze_button = gr.Button("Analyze Repository")
    
    progress_output = gr.HTML(label="Progress")
    report_output = gr.HTML(label="Analysis Report")
    
    analyze_button.click(
        analyze_github_repo,
        inputs=[repo_input, github_token],
        outputs=[progress_output, report_output],
    )

# Launch the app
if __name__ == "__main__":
    app.launch()