Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Github issues to a CSV file.
#!/usr/bin/env python
# Written for an import to Pivotal Tracker
from github import Github
FMT = 'Story,Labels,Story Type,Created at,Requested By,Owned By,Description,Comment'
PT_GH_map = {
'story': 'title',
'labels': 'labels',
'story type': 'type',
'created at': 'created_at',
'requested by': 'created_by',
'owned by': 'assignee',
'description': 'body',
'comment': 'comments',
}
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 issue_to_dict(issue, additional_labels=['training']):
info = dict()
for attr in ('title', 'body', 'created_at', ):
info[attr] = getattr(issue, attr)
info['assignee'] = getattr(issue.assignee, 'name', '')
info['labels'] = [l.name for l in issue.labels] + additional_labels
info['created_by'] = getattr(issue.user, 'name', '')
if issue.comments:
info['comments'] = [c.body for c in issue.get_comments()]
else:
info['comments'] = []
info['type'] = 'bug' if issue.title.upper().startswith('BUG') else 'feature'
return info
def issues_to_csv(issues, fmt=FMT):
rows = []
fmt_items = [name.strip().lower() for name in fmt.split(',')]
if 'comment' in fmt_items:
max_comment = max([len(issue['comments']) for issue in issues])
else:
max_comment = 0
for issue in issues:
row = []
for col in fmt_items:
data = issue[PT_GH_map[col]]
if col == 'labels':
data = '"%s"' % ','.join(data)
row.append(data)
elif isinstance(data, list):
for text in data:
row.append('"%s"' % text)
else:
row.append('"%s"' % data)
row = ','.join(row)
if 'comment' in fmt_items:
row += ',' + ','.join([''] * (max_comment - len(issue['comments'])))
row.replace('\r\n', '\n')
row.replace('\r', '\n')
rows.append(row)
return rows
def modify_header(issues, fmt=FMT):
fmt_items = [name.strip().lower() for name in fmt.split(',')]
if 'comment' in fmt_items:
max_comment = max([len(issue['comments']) for issue in issues])
else:
max_comment = 0
header = fmt + ',' + ','.join(['Comment'] * (max_comment - 1))
return header
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 = repo.get_issues(state='open')
#
# issue_dicts = [issue_to_dict(issue) for issue in issues]
import pickle
# with open('output.pickle', 'w') as f:
# pickle.dump(issue_dicts, f)
#
with open('output.pickle') as f:
issue_dicts = pickle.load(f)
rows = issues_to_csv(issue_dicts)
header = modify_header(issue_dicts)
with open('output.csv', 'w') as f:
f.write(header)
f.write('\n')
for line in rows:
f.write(line)
f.write('\n')
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.