Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
repo watcher
import requests
import smtplib
from email.mime.text import MIMEText
API = "api.github.com"
TOKEN = ""
WHITELIST = './whitelist'
EMAIL_LIST = ['sharter@trulia.com']
SMTP_SERVER = 'mail.trulia.com'
def uri(uri):
return "https://%s%s" % (API, uri)
def is_whitelisted(repo):
try:
return repo in is_whitelisted.list
except AttributeError:
is_whitelisted.list = [line.strip() for line in open(WHITELIST).readlines()]
return repo in is_whitelisted.list
def get_users():
try:
users = requests.get(uri('/orgs/trulia/members'), headers = {'Authorization': 'token ' + TOKEN}).json
if 'message' in users:
raise RuntimeError('Error Loading User List: %s' % users['message'])
except requests.exceptions.RequestException as e:
raise RuntimeError('Error Loading User List: %s' % e)
def main():
public_repos = []
for user in get_users():
try:
for repo in requests.get(uri('/users/%s/repos' % user['login'])).json:
if not (repo['private'] or is_whitelisted(repo['full_name'])):
public_repos.append(repo['full_name'])
except requests.exceptions.RequestException as e:
print "Error Loading Repo List for User %s: %s" % (user, e)
if public_repos:
try:
msg = MIMEText('\n'.join(public_repos))
msg['Subject'] = '%d Public Github Repos Found' % len(public_repos)
msg['From'] = 'gitforce@trulia.com'
msg['To'] = '\n'.join(EMAIL_LIST)
s = smtplib.SMTP(SMTP_SERVER)
s.sendmail('gitforce@trulia.com', EMAIL_LIST, msg.as_string())
s.quit()
except smtplib.SMTPException as e:
print "Error Sending Email Notification via SMTP Server '%s': %s" % (SMTP_SERVER, e)
print "Public Repos Found!"
print public_repos
if __name__ == '__main__':
try:
main()
except Exception as e:
print 'Fatal Error: %s' % e
exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.