Spaces:
Sleeping
Sleeping
mriusero
commited on
Commit
·
9332f3e
1
Parent(s):
210b910
feat: analyze repo
Browse files- tools/analyze_repo.py +47 -16
tools/analyze_repo.py
CHANGED
@@ -23,44 +23,75 @@ class GitHubCodeQualityTool(Tool):
|
|
23 |
Returns:
|
24 |
A string containing the code quality analysis results in Markdown format.
|
25 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
metrics = {
|
27 |
-
"
|
28 |
-
"code_coverage": "Percentage of code covered by tests",
|
29 |
-
"issues": self.fetch_open_issues(github_url),
|
30 |
"contributors": self.fetch_contributors(github_url),
|
31 |
"last_commit": self.fetch_last_commit_date(github_url),
|
|
|
|
|
|
|
32 |
}
|
33 |
-
|
34 |
-
# Format the metrics as a Markdown string
|
35 |
markdown_output = "\n".join([
|
36 |
f"- **{key}**: {value}" for key, value in metrics.items()
|
37 |
])
|
38 |
|
39 |
-
|
|
|
|
|
40 |
|
41 |
-
|
42 |
-
"""Retrieves the number of open issues for a GitHub repository."""
|
43 |
-
api_url = f"https://api.github.com/repos/{self.extract_repo_info(github_url)}/issues"
|
44 |
-
response = requests.get(api_url)
|
45 |
-
issues = response.json()
|
46 |
-
return len(issues)
|
47 |
|
48 |
def fetch_contributors(self, github_url: str) -> int:
|
49 |
"""Retrieves the number of contributors for a GitHub repository."""
|
50 |
-
|
|
|
51 |
response = requests.get(api_url)
|
|
|
|
|
52 |
contributors = response.json()
|
53 |
return len(contributors)
|
54 |
|
55 |
def fetch_last_commit_date(self, github_url: str) -> str:
|
56 |
"""Retrieves the date of the last commit for a GitHub repository."""
|
57 |
-
|
|
|
58 |
response = requests.get(api_url)
|
|
|
|
|
59 |
commits = response.json()
|
60 |
if commits:
|
61 |
return commits[0]['commit']['author']['date']
|
62 |
return "No commits found"
|
63 |
|
64 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
"""Extracts repository information from the GitHub URL."""
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
Returns:
|
24 |
A string containing the code quality analysis results in Markdown format.
|
25 |
"""
|
26 |
+
if not self.validate_github_url(github_url):
|
27 |
+
return "Please provide a valid GitHub repository URL."
|
28 |
+
|
29 |
+
user, repo = self.extract_repo_info(github_url)
|
30 |
+
api_url = f"https://api.github.com/repos/{user}/{repo}"
|
31 |
+
response = requests.get(api_url)
|
32 |
+
|
33 |
+
if response.status_code == 404:
|
34 |
+
return "Repository not found."
|
35 |
+
|
36 |
+
data = response.json()
|
37 |
+
|
38 |
metrics = {
|
39 |
+
"issues": data.get('open_issues_count', 'N/A'),
|
|
|
|
|
40 |
"contributors": self.fetch_contributors(github_url),
|
41 |
"last_commit": self.fetch_last_commit_date(github_url),
|
42 |
+
"stars": data.get('stargazers_count', 'N/A'),
|
43 |
+
"forks": data.get('forks_count', 'N/A'),
|
44 |
+
"pull_requests": self.fetch_pull_requests(github_url)
|
45 |
}
|
|
|
|
|
46 |
markdown_output = "\n".join([
|
47 |
f"- **{key}**: {value}" for key, value in metrics.items()
|
48 |
])
|
49 |
|
50 |
+
# Add the repository summary image
|
51 |
+
repo_image_url = f"https://github-readme-stats.vercel.app/api/pin/?username={user}&repo={repo}&theme=chartreuse-dark"
|
52 |
+
repo_image_markdown = f""
|
53 |
|
54 |
+
return f"{repo_image_markdown}\n\n{markdown_output}"
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
def fetch_contributors(self, github_url: str) -> int:
|
57 |
"""Retrieves the number of contributors for a GitHub repository."""
|
58 |
+
user, repo = self.extract_repo_info(github_url)
|
59 |
+
api_url = f"https://api.github.com/repos/{user}/{repo}/contributors"
|
60 |
response = requests.get(api_url)
|
61 |
+
if response.status_code != 200:
|
62 |
+
return "N/A"
|
63 |
contributors = response.json()
|
64 |
return len(contributors)
|
65 |
|
66 |
def fetch_last_commit_date(self, github_url: str) -> str:
|
67 |
"""Retrieves the date of the last commit for a GitHub repository."""
|
68 |
+
user, repo = self.extract_repo_info(github_url)
|
69 |
+
api_url = f"https://api.github.com/repos/{user}/{repo}/commits"
|
70 |
response = requests.get(api_url)
|
71 |
+
if response.status_code != 200:
|
72 |
+
return "No commits found"
|
73 |
commits = response.json()
|
74 |
if commits:
|
75 |
return commits[0]['commit']['author']['date']
|
76 |
return "No commits found"
|
77 |
|
78 |
+
def fetch_pull_requests(self, github_url: str) -> int:
|
79 |
+
"""Retrieves the number of open pull requests for a GitHub repository."""
|
80 |
+
user, repo = self.extract_repo_info(github_url)
|
81 |
+
api_url = f"https://api.github.com/repos/{user}/{repo}/pulls"
|
82 |
+
response = requests.get(api_url)
|
83 |
+
if response.status_code != 200:
|
84 |
+
return "N/A"
|
85 |
+
pull_requests = response.json()
|
86 |
+
return len(pull_requests)
|
87 |
+
|
88 |
+
def extract_repo_info(self, github_url: str) -> tuple:
|
89 |
"""Extracts repository information from the GitHub URL."""
|
90 |
+
parts = github_url.split('/')
|
91 |
+
if len(parts) < 5:
|
92 |
+
return "URL should be in the format: https://github.com/username/repository"
|
93 |
+
return parts[3], parts[4]
|
94 |
+
|
95 |
+
def validate_github_url(self, github_url: str) -> bool:
|
96 |
+
"""Validates if the provided URL is a valid GitHub repository URL."""
|
97 |
+
return "github.com" in github_url
|