add error handling

This commit is contained in:
Enzo Nunes
2025-02-23 22:39:41 +00:00
parent 675704225a
commit 97f497c024

View File

@@ -14,8 +14,12 @@ MARGIN = 0.1
def get_stars_info(owner, repo, token):
headers = {"Authorization": f"token {token}"}
stars_url = f"https://api.github.com/repos/{owner}/{repo}"
stars_response = requests.get(stars_url, headers=headers).json()
stars = stars_response["stargazers_count"]
stars_response = requests.get(stars_url, headers=headers)
if stars_response.status_code != 200:
return -1
stars = stars_response.json()["stargazers_count"]
return stars
@@ -24,8 +28,12 @@ def get_commits_last_30_days(owner, repo, token):
since = (datetime.now() - timedelta(days=30)).isoformat()
print(since)
commits_url = f"https://api.github.com/repos/{owner}/{repo}/commits?since={since}&per_page={LIMIT_COMMITS_30_DAYS}"
commits_response = requests.get(commits_url, headers=headers).json()
commits_30_days = len(commits_response)
commits_response = requests.get(commits_url, headers=headers)
if commits_response.status_code != 200:
return -1
commits_30_days = len(commits_response.json())
return commits_30_days
@@ -33,7 +41,11 @@ def get_lines_of_code(owner, repo):
repo_url = f"https://github.com/{owner}/{repo}.git"
clone_dir = os.path.expanduser("~/temp/PICable")
subprocess.run(["git", "clone", repo_url, clone_dir], check=True, stdout=sys.stdout, stderr=sys.stderr)
try:
subprocess.run(["git", "clone", repo_url, clone_dir], check=True, stdout=sys.stdout, stderr=sys.stderr)
except:
return -1
result = subprocess.run(["sloccount", clone_dir], capture_output=True, text=True, check=True)
for line in result.stdout.splitlines():
if "Total Physical Source Lines of Code (SLOC)" in line:
@@ -51,6 +63,10 @@ def PICable(owner, repository, token):
res = ""
stars = get_stars_info(owner, repository, token)
if stars == -1:
return "The repository does not exist or the token is invalid. ❌"
if stars >= LIMIT_STARS:
res += f"The repository has over {LIMIT_STARS} stars. ✅\n"
elif stars >= LIMIT_STARS * (1 - MARGIN):
@@ -59,6 +75,10 @@ def PICable(owner, repository, token):
res += f"The repository has {stars} stars, which is way below the limit of {LIMIT_STARS} stars, considering a {margin_percentage}% margin. ❌\n"
commits_30_days = get_commits_last_30_days(owner, repository, token)
if commits_30_days == -1:
return "The repository does not exist or the token is invalid. ❌"
if commits_30_days >= LIMIT_COMMITS_30_DAYS:
res += f"The repository has over {LIMIT_COMMITS_30_DAYS} commits in the last 30 days. ✅\n"
elif commits_30_days >= LIMIT_COMMITS_30_DAYS * (1 - MARGIN):
@@ -67,6 +87,10 @@ def PICable(owner, repository, token):
res += f"The repository has {commits_30_days} commits in the last 30 days, which is way below the limit of {LIMIT_COMMITS_30_DAYS} commits, considering a {margin_percentage}% margin. ❌\n"
nloc = get_lines_of_code(owner, repository)
if nloc == -1:
return "The repository does not exist. ❌"
if nloc >= LIMIT_NLOC:
res += f"The repository has over {LIMIT_NLOC} lines of code. ✅\n"
elif nloc >= LIMIT_NLOC * (1 - MARGIN):