Skip to content

Instantly share code, notes, and snippets.

@punchagan
Last active October 11, 2015 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save punchagan/3888203 to your computer and use it in GitHub Desktop.
Save punchagan/3888203 to your computer and use it in GitHub Desktop.
Simple script to plot Github turn around time
#!/usr/bin/env python
from github import Github
from datetime import datetime
from pylab import plot, show, figure, legend, title, ylabel
def set_repo_name(repo_name, username):
if '/' in repo_name:
return repo_name
else:
return "%s/%s" %(username, repo_name)
def get_repo(username, password, full_reponame):
print 'Fetching repository'
g = Github(username, password)
user, reponame = full_reponame.split('/')
user = g.get_user(user)
return user.get_repo(reponame)
def get_issues(repo, states=('closed', 'open')):
issues = dict()
for state in states:
print 'Fetching %s issues' % state
issues[state] = list(repo.get_issues(state=state))
return issues
def get_first_user_comment(issue, author=''):
#print 'Fetching first comment date for', issue.number
for comment in issue.get_comments():
if not author or comment.user.login == author:
return comment.created_at
def plot_issues(issues, author):
""" Issues is a dict with keys as states and values as a list of issues.
"""
figure(figsize=(12,9))
all_issues = []
for state in issues:
for issue in issues[state]:
#print 'Plotting issue', issue.number
start = issue.created_at
if state == 'closed':
end = issue.closed_at
color = 'g'
elif issue.comments:
end = get_first_user_comment(issue, author)
color = 'r'
else:
end = None
if not end:
color = 'k'
end = datetime.today()
plot([start, end], [issue.number]*2, color, linewidth=2)
all_issues.append((issue, start, end))
return all_issues
def mark_work_days(all_issues):
""" If three or more issues were fixed on a day, draw a vertical line
"""
FMT = '%Y-%m-%d'
n = len(all_issues)
ends = [datetime.strptime(i.strftime(FMT), FMT) for _, _, i in all_issues]
days = [day for day in ends if ends.count(day) >= 3]
for day in days:
plot([day]*n, range(n), 'b--')
def add_legend():
import matplotlib.patches as mpatches
r = mpatches.Patch(color='r', label='Time to first comment (open issues)')
g = mpatches.Patch(color='g', label='Time to close issue/PR')
k = mpatches.Patch(color='k', label='Time open with no comment')
b = mpatches.Patch(color='b', label='Work days (> 3 issues closed)')
legend(handles=[r, g, k, b], loc=2)
title('%s turnaround' % reponame)
ylabel('Issues/PRs')
if __name__ == "__main__":
from sys import argv, exit
from getpass import getpass
if len(argv) < 3:
print "Usage: %s username reponame" % argv[0]
print "reponame can be of the form <author>/<repository>"
exit(1)
username = argv[1]
reponame = set_repo_name(argv[2], username)
author = reponame.split('/')[0]
password = getpass('Github password for %s: ' % username)
repo = get_repo(username, password, reponame)
issues = get_issues(repo)
all_issues = plot_issues(issues, author)
mark_work_days(all_issues)
add_legend()
show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment