Skip to content

Instantly share code, notes, and snippets.

@rhelmer
Created April 10, 2012 00:36
Show Gist options
  • Select an option

  • Save rhelmer/2347622 to your computer and use it in GitHub Desktop.

Select an option

Save rhelmer/2347622 to your computer and use it in GitHub Desktop.
contrib/collab
#!/usr/bin/python
"""
Show a graph of external contributions - that is, commits from usernames
not listed as "collaborators" on github.
Example: ./contribs.py --since=2011-01-31
Outputs:
JSON data suitable for graphing in flot
"""
import sys
import optparse
import urllib2
import json
import datetime
import time
import iso8601
github_api_url = 'https://api.github.com'
github_user = 'mozilla'
github_repo = 'socorro'
def main(since):
collabs_url = '%s/repos/%s/%s/collaborators' % (github_api_url, github_user, github_repo)
collabs_json = json.loads(urllib2.urlopen(collabs_url).read())
collabs = set([e['login'] for e in collabs_json])
collab_data = []
contrib_data = []
for (commit_date, commit) in get_commit_page(since):
commit_date = time.mktime(commit_date.timetuple()) * 1000
if commit not in collabs:
contrib_data.append([commit_date, 1])
else:
collab_data.append([commit_date, 1])
flot_attribs = {'bars': { 'show': True } }
flot_data = [
{ 'label': 'Contributors', 'data': contrib_data, 'bars': { 'show': True } },
{ 'label': 'Collaborators', 'data': collab_data, 'bars': { 'show': True } },
]
print 'flot_data = ' + json.dumps(flot_data) + ';'
def get_commit_page(since):
commits_url = '%s/repos/%s/%s/commits' \
% (github_api_url, github_user, github_repo)
while True:
resp = urllib2.urlopen(commits_url)
# Link response header contains the link to the next page of results
commits_url = resp.info().getheader('Link').split(';')[0]
commits_url = commits_url.split('<')[1] + commits_url.split('>')[0]
commit_page = json.loads(resp.read())
for commit in commit_page:
commit_date = iso8601.parse_date(commit['commit']['committer']['date'])
if commit_date >= since:
if commit['committer'] is not None:
yield (commit_date, commit['committer']['login'])
else:
yield (commit_date, commit['commit']['committer']['email'])
else:
return
if __name__ == '__main__':
usage = "%prog [options]"
parser = optparse.OptionParser("%s\n%s" % (usage.strip(), __doc__.strip()))
parser.add_option('-s', '--since', dest='since',
type='string', help='Show commits more recent than a specific date.')
(options, args) = parser.parse_args()
mandatories = ['since']
for m in mandatories:
if not options.__dict__[m]:
print "mandatory option is missing\n"
parser.print_help()
sys.exit(-1)
main(iso8601.parse_date(options.since + 'T00:00:00'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment