kevin1911 commited on
Commit
f0b7f76
·
verified ·
1 Parent(s): 8fd76a4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +107 -0
app.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import subprocess
4
+ import git
5
+ import zipfile
6
+ import io
7
+ from getpass import getpass
8
+ import radon.complexity as radon_complexity
9
+ import pylint.lint
10
+ from pylint.reporters.text import TextReporter
11
+
12
+ # Function to authenticate GitHub and fetch the user's repositories
13
+ def authenticate_github(token):
14
+ headers = {
15
+ "Authorization": f"token {token}"
16
+ }
17
+
18
+ # Fetch the list of repositories
19
+ response = requests.get("https://api.github.com/user/repos", headers=headers)
20
+
21
+ if response.status_code == 200:
22
+ repos = response.json()
23
+ repo_names = [repo['name'] for repo in repos]
24
+ return repo_names
25
+ else:
26
+ return "Error: Failed to fetch repositories"
27
+
28
+ # Function to download the repo as a zip file from GitHub API
29
+ def download_repo(github_url):
30
+ repo_name = github_url.split('/')[-1]
31
+ repo_owner = github_url.split('/')[-2]
32
+ api_url = f"https://github.com/{repo_owner}/{repo_name}/archive/refs/heads/main.zip"
33
+
34
+ # Download the zip file
35
+ response = requests.get(api_url)
36
+
37
+ # Extract the zip file
38
+ zip_file = zipfile.ZipFile(io.BytesIO(response.content))
39
+ zip_file.extractall(f"/content/repos/{repo_name}")
40
+
41
+ return f"/content/repos/{repo_name}"
42
+
43
+ # Function to fetch the GitHub repo using SSH or token for private repos
44
+ def fetch_repo(github_url, token=None):
45
+ try:
46
+ # Check if the repository is private or public
47
+ if token:
48
+ repo_name = github_url.split('/')[-1]
49
+ repo_owner = github_url.split('/')[-2]
50
+ clone_url = f"https://{token}@github.com/{repo_owner}/{repo_name}.git"
51
+ else:
52
+ repo_name = github_url.split('/')[-1]
53
+ repo_owner = github_url.split('/')[-2]
54
+ clone_url = f"[email protected]:{repo_owner}/{repo_name}.git"
55
+
56
+ # Create a temporary directory to clone the repo
57
+ temp_dir = f"/content/repos/{repo_name}"
58
+
59
+ # Check if repo already exists
60
+ if not os.path.exists(temp_dir):
61
+ # If SSH method fails, fallback to downloading as a zip
62
+ try:
63
+ git.Repo.clone_from(clone_url, temp_dir)
64
+ except Exception as e:
65
+ return download_repo(github_url)
66
+
67
+ return temp_dir
68
+ except Exception as e:
69
+ return f"Error: {str(e)}"
70
+
71
+ # Function to analyze the repository
72
+ def analyze_code(github_url, token=None):
73
+ try:
74
+ repo_path = fetch_repo(github_url, token)
75
+
76
+ # Perform analysis
77
+ complexity = analyze_complexity(repo_path)
78
+ linting = analyze_linting(repo_path)
79
+ coverage = analyze_coverage(repo_path)
80
+ duplication = analyze_duplication(repo_path)
81
+
82
+ # Combine results
83
+ results = f"### Cyclomatic Complexity Analysis\n{complexity}\n\n"
84
+ results += f"### Linting Results\n{linting}\n\n"
85
+ results += f"### Test Coverage\n{coverage}\n\n"
86
+ results += f"### Code Duplication\n{duplication}\n"
87
+
88
+ return results
89
+ except Exception as e:
90
+ return f"Error: {str(e)}"
91
+
92
+ # Create Gradio Interface
93
+ def gradio_interface():
94
+ def fetch_and_analyze(token):
95
+ repos = authenticate_github(token)
96
+ return repos
97
+
98
+ gr.Interface(
99
+ fn=fetch_and_analyze,
100
+ inputs=[gr.Textbox(label="GitHub Token", type="password")],
101
+ outputs="text",
102
+ live=True,
103
+ title="GitHub Repository Selector",
104
+ description="Enter your GitHub token to fetch and analyze your repositories."
105
+ ).launch(share=True)
106
+
107
+ gradio_interface()