std.lint.repo.repository: lint_repository.py
import re
import requests
import sys
with open(sys.argv[1]) as f:
repo_url = f.readline().strip()
github_owner_re = r'[a-zA-Z\d](?:[a-zA-Z\d\-]{0,38})'
github_repo_re = r'[a-zA-Z\d\-_.]+'
git_url_re = re.compile(
fr'^https://github.com/({github_owner_re})/({github_repo_re}?)(?:\.git)?/?$')
m = git_url_re.match(repo_url)
if not m:
print('Not a github repository url')
exit(1)
(owner, repo_name) = m.groups()
api = f'https://api.github.com/repos/{owner}/{repo_name}'
def get(url):
try:
return requests.get(url, timeout=15)
except requests.RequestException as e:
print('Error retrieving:', type(e).__name__)
exit(1)
def fail_on(r):
if r.status_code == 404:
print('Repository not found: it does not exist or is private')
elif r.status_code == 403 and r.headers.get('X-RateLimit-Remaining') == '0':
print('GitHub API rate limit exceeded, try again later')
else:
print('Error retrieving:', r.status_code, r.json().get('message', '') if r.content else '')
exit(1)
r = get(api)
if not r:
fail_on(r)
j = r.json()
if j.get('private'):
print('Repository is private')
exit(1)
if j.get('disabled'):
print('Repository is disabled')
exit(1)
# GitHub answers 409 "Git Repository is empty." for a repository with no commits.
c = get(f'{api}/commits?per_page=1')
if c.status_code == 409:
print('Repository is empty')
exit(1)
if not c:
fail_on(c)
if not c.json():
print('Repository is empty')
exit(1)
print('ok:', j['full_name'] + (' (archived)' if j.get('archived') else ''))