add error handling
This commit is contained in:
34
PICable.py
34
PICable.py
@@ -14,8 +14,12 @@ MARGIN = 0.1
|
|||||||
def get_stars_info(owner, repo, token):
|
def get_stars_info(owner, repo, token):
|
||||||
headers = {"Authorization": f"token {token}"}
|
headers = {"Authorization": f"token {token}"}
|
||||||
stars_url = f"https://api.github.com/repos/{owner}/{repo}"
|
stars_url = f"https://api.github.com/repos/{owner}/{repo}"
|
||||||
stars_response = requests.get(stars_url, headers=headers).json()
|
stars_response = requests.get(stars_url, headers=headers)
|
||||||
stars = stars_response["stargazers_count"]
|
|
||||||
|
if stars_response.status_code != 200:
|
||||||
|
return -1
|
||||||
|
|
||||||
|
stars = stars_response.json()["stargazers_count"]
|
||||||
return stars
|
return stars
|
||||||
|
|
||||||
|
|
||||||
@@ -24,8 +28,12 @@ def get_commits_last_30_days(owner, repo, token):
|
|||||||
since = (datetime.now() - timedelta(days=30)).isoformat()
|
since = (datetime.now() - timedelta(days=30)).isoformat()
|
||||||
print(since)
|
print(since)
|
||||||
commits_url = f"https://api.github.com/repos/{owner}/{repo}/commits?since={since}&per_page={LIMIT_COMMITS_30_DAYS}"
|
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_response = requests.get(commits_url, headers=headers)
|
||||||
commits_30_days = len(commits_response)
|
|
||||||
|
if commits_response.status_code != 200:
|
||||||
|
return -1
|
||||||
|
|
||||||
|
commits_30_days = len(commits_response.json())
|
||||||
return commits_30_days
|
return commits_30_days
|
||||||
|
|
||||||
|
|
||||||
@@ -33,7 +41,11 @@ def get_lines_of_code(owner, repo):
|
|||||||
repo_url = f"https://github.com/{owner}/{repo}.git"
|
repo_url = f"https://github.com/{owner}/{repo}.git"
|
||||||
clone_dir = os.path.expanduser("~/temp/PICable")
|
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)
|
result = subprocess.run(["sloccount", clone_dir], capture_output=True, text=True, check=True)
|
||||||
for line in result.stdout.splitlines():
|
for line in result.stdout.splitlines():
|
||||||
if "Total Physical Source Lines of Code (SLOC)" in line:
|
if "Total Physical Source Lines of Code (SLOC)" in line:
|
||||||
@@ -51,6 +63,10 @@ def PICable(owner, repository, token):
|
|||||||
res = ""
|
res = ""
|
||||||
|
|
||||||
stars = get_stars_info(owner, repository, token)
|
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:
|
if stars >= LIMIT_STARS:
|
||||||
res += f"The repository has over {LIMIT_STARS} stars. ✅\n"
|
res += f"The repository has over {LIMIT_STARS} stars. ✅\n"
|
||||||
elif stars >= LIMIT_STARS * (1 - MARGIN):
|
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"
|
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)
|
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:
|
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"
|
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):
|
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"
|
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)
|
nloc = get_lines_of_code(owner, repository)
|
||||||
|
|
||||||
|
if nloc == -1:
|
||||||
|
return "The repository does not exist. ❌"
|
||||||
|
|
||||||
if nloc >= LIMIT_NLOC:
|
if nloc >= LIMIT_NLOC:
|
||||||
res += f"The repository has over {LIMIT_NLOC} lines of code. ✅\n"
|
res += f"The repository has over {LIMIT_NLOC} lines of code. ✅\n"
|
||||||
elif nloc >= LIMIT_NLOC * (1 - MARGIN):
|
elif nloc >= LIMIT_NLOC * (1 - MARGIN):
|
||||||
|
|||||||
Reference in New Issue
Block a user