Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,19 +3,53 @@ import os
|
|
3 |
import time
|
4 |
import tempfile
|
5 |
import traceback
|
6 |
-
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
|
7 |
from github import Github
|
8 |
from openai import OpenAI
|
9 |
|
10 |
-
#
|
11 |
-
|
12 |
-
(
|
13 |
-
|
14 |
-
|
15 |
-
(
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
def analyze_github_repo(repo_input, github_token=None):
|
21 |
if github_token:
|
@@ -23,23 +57,20 @@ def analyze_github_repo(repo_input, github_token=None):
|
|
23 |
|
24 |
github_token = os.environ.get("GITHUB_TOKEN")
|
25 |
if not github_token:
|
|
|
26 |
return "β Error: GITHUB_TOKEN environment variable not set.", gr.update(visible=True), gr.update(visible=False)
|
27 |
|
28 |
openrouter_api_key = os.environ.get("OPENROUTER_API_KEY")
|
29 |
if not openrouter_api_key:
|
|
|
30 |
return "β Error: OPENROUTER_API_KEY environment variable not set.", gr.update(visible=True), gr.update(visible=False)
|
31 |
|
32 |
progress_md = ""
|
33 |
-
|
34 |
-
|
35 |
-
for emoji, message in PROGRESS_STEPS:
|
36 |
-
progress_md += f"{emoji} {message} \n"
|
37 |
-
yield progress_md, gr.update(visible=True), gr.update(visible=False)
|
38 |
-
time.sleep(4)
|
39 |
-
|
40 |
try:
|
41 |
owner, repo_name = get_repo_info(repo_input)
|
42 |
repo_url = f"https://github.com/{owner}/{repo_name}"
|
|
|
43 |
|
44 |
g = Github(github_token)
|
45 |
github_repo = g.get_repo(f"{owner}/{repo_name}")
|
@@ -51,21 +82,25 @@ def analyze_github_repo(repo_input, github_token=None):
|
|
51 |
|
52 |
with tempfile.TemporaryDirectory() as temp_dir:
|
53 |
repo_path = clone_repo(owner, repo_name, temp_dir)
|
|
|
54 |
|
55 |
progress_md += "π¬ Analyzing code structure... \n"
|
56 |
yield progress_md, gr.update(visible=True), gr.update(visible=False)
|
57 |
code_analysis = analyze_code(repo_path)
|
58 |
code_analysis['llm_analysis'] = llm_analyze_code(client, code_analysis)
|
|
|
59 |
|
60 |
progress_md += f"π Analyzing issues (max {max_issues})... \n"
|
61 |
yield progress_md, gr.update(visible=True), gr.update(visible=False)
|
62 |
issues_data = analyze_issues(github_repo, max_issues)
|
63 |
issues_analysis = llm_analyze_issues(client, issues_data, repo_url)
|
|
|
64 |
|
65 |
progress_md += f"π Analyzing pull requests (max {max_prs})... \n"
|
66 |
yield progress_md, gr.update(visible=True), gr.update(visible=False)
|
67 |
prs_data = analyze_pull_requests(github_repo, max_prs)
|
68 |
pr_analysis = llm_analyze_prs(client, prs_data, repo_url)
|
|
|
69 |
|
70 |
progress_md += "π§ Synthesizing findings... \n"
|
71 |
yield progress_md, gr.update(visible=True), gr.update(visible=False)
|
@@ -75,6 +110,7 @@ def analyze_github_repo(repo_input, github_token=None):
|
|
75 |
issues_analysis.get('summary', ''),
|
76 |
pr_analysis.get('summary', '')
|
77 |
)
|
|
|
78 |
|
79 |
repo_info = {
|
80 |
"owner": owner,
|
@@ -84,11 +120,13 @@ def analyze_github_repo(repo_input, github_token=None):
|
|
84 |
progress_md += "π Generating report... \n"
|
85 |
yield progress_md, gr.update(visible=True), gr.update(visible=False)
|
86 |
report = generate_report(repo_info, code_analysis, issues_analysis, pr_analysis, final_analysis)
|
|
|
87 |
|
88 |
-
|
89 |
return progress_md + "β
Analysis complete!", gr.update(visible=False), gr.update(visible=True, value=report)
|
90 |
except Exception as e:
|
91 |
error_message = f"β An error occurred: {str(e)}"
|
|
|
92 |
traceback.print_exc()
|
93 |
return progress_md + error_message, gr.update(visible=True), gr.update(visible=False)
|
94 |
|
|
|
3 |
import time
|
4 |
import tempfile
|
5 |
import traceback
|
|
|
6 |
from github import Github
|
7 |
from openai import OpenAI
|
8 |
|
9 |
+
# Mock functions for testing - replace these with your actual functions
|
10 |
+
def get_repo_info(repo_input):
|
11 |
+
return repo_input.split('/')[-2:]
|
12 |
+
|
13 |
+
def clone_repo(owner, repo_name, temp_dir):
|
14 |
+
return os.path.join(temp_dir, f"{owner}_{repo_name}")
|
15 |
+
|
16 |
+
def analyze_code(repo_path):
|
17 |
+
return {"files": 10, "lines": 1000}
|
18 |
+
|
19 |
+
def analyze_issues(github_repo, max_issues):
|
20 |
+
return [{"title": "Test Issue", "number": 1}]
|
21 |
+
|
22 |
+
def analyze_pull_requests(github_repo, max_prs):
|
23 |
+
return [{"title": "Test PR", "number": 1}]
|
24 |
+
|
25 |
+
def llm_analyze_code(client, code_analysis):
|
26 |
+
return "Code analysis summary"
|
27 |
+
|
28 |
+
def llm_analyze_issues(client, issues_data, repo_url):
|
29 |
+
return {"summary": "Issues analysis summary"}
|
30 |
+
|
31 |
+
def llm_analyze_prs(client, prs_data, repo_url):
|
32 |
+
return {"summary": "PRs analysis summary"}
|
33 |
+
|
34 |
+
def llm_synthesize_findings(client, code_analysis, issues_analysis, pr_analysis):
|
35 |
+
return "Synthesized findings"
|
36 |
+
|
37 |
+
def generate_report(repo_info, code_analysis, issues_analysis, pr_analysis, final_analysis):
|
38 |
+
return f"""
|
39 |
+
# Repository Analysis Report for {repo_info['owner']}/{repo_info['repo_name']}
|
40 |
+
|
41 |
+
## Code Analysis
|
42 |
+
{code_analysis['llm_analysis']}
|
43 |
+
|
44 |
+
## Issues Analysis
|
45 |
+
{issues_analysis['summary']}
|
46 |
+
|
47 |
+
## Pull Requests Analysis
|
48 |
+
{pr_analysis['summary']}
|
49 |
+
|
50 |
+
## Final Analysis
|
51 |
+
{final_analysis}
|
52 |
+
"""
|
53 |
|
54 |
def analyze_github_repo(repo_input, github_token=None):
|
55 |
if github_token:
|
|
|
57 |
|
58 |
github_token = os.environ.get("GITHUB_TOKEN")
|
59 |
if not github_token:
|
60 |
+
print("GitHub token not found")
|
61 |
return "β Error: GITHUB_TOKEN environment variable not set.", gr.update(visible=True), gr.update(visible=False)
|
62 |
|
63 |
openrouter_api_key = os.environ.get("OPENROUTER_API_KEY")
|
64 |
if not openrouter_api_key:
|
65 |
+
print("OpenRouter API key not found")
|
66 |
return "β Error: OPENROUTER_API_KEY environment variable not set.", gr.update(visible=True), gr.update(visible=False)
|
67 |
|
68 |
progress_md = ""
|
69 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
try:
|
71 |
owner, repo_name = get_repo_info(repo_input)
|
72 |
repo_url = f"https://github.com/{owner}/{repo_name}"
|
73 |
+
print(f"Analyzing repository: {repo_url}")
|
74 |
|
75 |
g = Github(github_token)
|
76 |
github_repo = g.get_repo(f"{owner}/{repo_name}")
|
|
|
82 |
|
83 |
with tempfile.TemporaryDirectory() as temp_dir:
|
84 |
repo_path = clone_repo(owner, repo_name, temp_dir)
|
85 |
+
print(f"Cloned repository to: {repo_path}")
|
86 |
|
87 |
progress_md += "π¬ Analyzing code structure... \n"
|
88 |
yield progress_md, gr.update(visible=True), gr.update(visible=False)
|
89 |
code_analysis = analyze_code(repo_path)
|
90 |
code_analysis['llm_analysis'] = llm_analyze_code(client, code_analysis)
|
91 |
+
print("Code analysis complete")
|
92 |
|
93 |
progress_md += f"π Analyzing issues (max {max_issues})... \n"
|
94 |
yield progress_md, gr.update(visible=True), gr.update(visible=False)
|
95 |
issues_data = analyze_issues(github_repo, max_issues)
|
96 |
issues_analysis = llm_analyze_issues(client, issues_data, repo_url)
|
97 |
+
print("Issues analysis complete")
|
98 |
|
99 |
progress_md += f"π Analyzing pull requests (max {max_prs})... \n"
|
100 |
yield progress_md, gr.update(visible=True), gr.update(visible=False)
|
101 |
prs_data = analyze_pull_requests(github_repo, max_prs)
|
102 |
pr_analysis = llm_analyze_prs(client, prs_data, repo_url)
|
103 |
+
print("Pull requests analysis complete")
|
104 |
|
105 |
progress_md += "π§ Synthesizing findings... \n"
|
106 |
yield progress_md, gr.update(visible=True), gr.update(visible=False)
|
|
|
110 |
issues_analysis.get('summary', ''),
|
111 |
pr_analysis.get('summary', '')
|
112 |
)
|
113 |
+
print("Findings synthesized")
|
114 |
|
115 |
repo_info = {
|
116 |
"owner": owner,
|
|
|
120 |
progress_md += "π Generating report... \n"
|
121 |
yield progress_md, gr.update(visible=True), gr.update(visible=False)
|
122 |
report = generate_report(repo_info, code_analysis, issues_analysis, pr_analysis, final_analysis)
|
123 |
+
print("Report generated")
|
124 |
|
125 |
+
print("Analysis complete, returning results")
|
126 |
return progress_md + "β
Analysis complete!", gr.update(visible=False), gr.update(visible=True, value=report)
|
127 |
except Exception as e:
|
128 |
error_message = f"β An error occurred: {str(e)}"
|
129 |
+
print(f"Error occurred: {error_message}")
|
130 |
traceback.print_exc()
|
131 |
return progress_md + error_message, gr.update(visible=True), gr.update(visible=False)
|
132 |
|