Spaces:
Sleeping
Sleeping
File size: 3,594 Bytes
f0b7f76 |
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 |
import gradio as gr
import requests
import subprocess
import git
import zipfile
import io
from getpass import getpass
import radon.complexity as radon_complexity
import pylint.lint
from pylint.reporters.text import TextReporter
# Function to authenticate GitHub and fetch the user's repositories
def authenticate_github(token):
headers = {
"Authorization": f"token {token}"
}
# Fetch the list of repositories
response = requests.get("https://api.github.com/user/repos", headers=headers)
if response.status_code == 200:
repos = response.json()
repo_names = [repo['name'] for repo in repos]
return repo_names
else:
return "Error: Failed to fetch repositories"
# Function to download the repo as a zip file from GitHub API
def download_repo(github_url):
repo_name = github_url.split('/')[-1]
repo_owner = github_url.split('/')[-2]
api_url = f"https://github.com/{repo_owner}/{repo_name}/archive/refs/heads/main.zip"
# Download the zip file
response = requests.get(api_url)
# Extract the zip file
zip_file = zipfile.ZipFile(io.BytesIO(response.content))
zip_file.extractall(f"/content/repos/{repo_name}")
return f"/content/repos/{repo_name}"
# Function to fetch the GitHub repo using SSH or token for private repos
def fetch_repo(github_url, token=None):
try:
# Check if the repository is private or public
if token:
repo_name = github_url.split('/')[-1]
repo_owner = github_url.split('/')[-2]
clone_url = f"https://{token}@github.com/{repo_owner}/{repo_name}.git"
else:
repo_name = github_url.split('/')[-1]
repo_owner = github_url.split('/')[-2]
clone_url = f"[email protected]:{repo_owner}/{repo_name}.git"
# Create a temporary directory to clone the repo
temp_dir = f"/content/repos/{repo_name}"
# Check if repo already exists
if not os.path.exists(temp_dir):
# If SSH method fails, fallback to downloading as a zip
try:
git.Repo.clone_from(clone_url, temp_dir)
except Exception as e:
return download_repo(github_url)
return temp_dir
except Exception as e:
return f"Error: {str(e)}"
# Function to analyze the repository
def analyze_code(github_url, token=None):
try:
repo_path = fetch_repo(github_url, token)
# Perform analysis
complexity = analyze_complexity(repo_path)
linting = analyze_linting(repo_path)
coverage = analyze_coverage(repo_path)
duplication = analyze_duplication(repo_path)
# Combine results
results = f"### Cyclomatic Complexity Analysis\n{complexity}\n\n"
results += f"### Linting Results\n{linting}\n\n"
results += f"### Test Coverage\n{coverage}\n\n"
results += f"### Code Duplication\n{duplication}\n"
return results
except Exception as e:
return f"Error: {str(e)}"
# Create Gradio Interface
def gradio_interface():
def fetch_and_analyze(token):
repos = authenticate_github(token)
return repos
gr.Interface(
fn=fetch_and_analyze,
inputs=[gr.Textbox(label="GitHub Token", type="password")],
outputs="text",
live=True,
title="GitHub Repository Selector",
description="Enter your GitHub token to fetch and analyze your repositories."
).launch(share=True)
gradio_interface()
|