Skip to content

Commit

Permalink
added script to detect unauthorized contributors
Browse files Browse the repository at this point in the history
  • Loading branch information
lassewesth committed Apr 29, 2014
1 parent ee67d59 commit 0edb168
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions cla-management/detect-unauthorized-contributors
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/usr/bin/env python

import argparse
import requests
import sys


def get_usernames(contributors):
usernames = set()
for contributor in contributors:
usernames.add(contributor['login'])
return usernames


def get_next_url(link_header):
# need to parse:
# <https://api.github.com/repositories/6650539/contributors?page=2>; rel="next", <https://api.github.com/repositories/6650539/contributors?page=3>; rel="last"
# <https://api.github.com/repositories/6650539/contributors?page=3>; rel="next", <https://api.github.com/repositories/6650539/contributors?page=3>; rel="last", <https://api.github.com/repositories/6650539/contributors?page=1>; rel="first", <https://api.github.com/repositories/6650539/contributors?page=1>; rel="prev"
# <https://api.github.com/repositories/6650539/contributors?page=1>; rel="first", <https://api.github.com/repositories/6650539/contributors?page=2>; rel="prev"
links = link_header.split(',')
for link in links:
(url, rel) = link.strip().split(';')
if rel.strip()[5:-1] == 'next':
return url.strip()[1:-1]
pass


def get_contributors(auth):
url = 'https://api.github.com/repos/neo4j/neo4j/contributors'
try:
contributors = []
while url is not None:
response = requests.get(url, auth=auth)
response.raise_for_status()
contributors += response.json( )
url = get_next_url(response.headers['Link'])
return get_usernames(contributors)
except requests.exceptions.HTTPError as e:
if e.response.status_code == requests.codes.forbidden and e.response.headers['X-RateLimit-Remaining'] == '0':
exit("Failed to get list of contributors from GitHub due to rate limiting.")

print >> sys.stderr, "Failed to obtain pull request details from GitHub API."
print >> sys.stderr, "url: %s" % url
print >> sys.stderr, "response status: %s" % e.response.status_code
print >> sys.stderr, "headers:"
for header in e.response.headers:
print >> sys.stderr, "\t%s: %s" % (header, e.response.headers[header])
print >> sys.stderr, "body: %s" % e.response.text
exit(1)


def get_whitelisted_users(whitelist):
usernames = set()
if len(whitelist.strip()) == 0:
return usernames
for username in whitelist.split(','):
usernames.add(username.strip())
return usernames


parser = argparse.ArgumentParser(description='Ensure all contributors to the product have been whitelisted')
parser.add_argument('whitelist', help='Comma-separated list of GitHub usernames')
parser.add_argument('--username', help='GitHub username, to avoid rate limit')
parser.add_argument('--password', help='GitHub password, to avoid rate limit')
args = parser.parse_args()

auth = None
if args.username is not None and args.password is not None:
auth = (args.username, args.password)

contributors = get_contributors(auth)
whitelisted_users = get_whitelisted_users(args.whitelist)

if not contributors.issubset(whitelisted_users):
exit("Users [%s] were not in the whitelist." % ", ".join(contributors.difference(whitelisted_users)))

0 comments on commit 0edb168

Please sign in to comment.