Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mlandauer/bfaa8fe6f034bdb91b9d to your computer and use it in GitHub Desktop.
Save mlandauer/bfaa8fe6f034bdb91b9d to your computer and use it in GitHub Desktop.
"""
Exports Issues from a specified repository to a CSV file
Uses basic authentication (Github username + password) to retrieve Issues
from a repository that username has access to. Supports Github API v3.
"""
import csv
import requests
GITHUB_USER = ''
GITHUB_PASSWORD = ''
REPO = '' # format is username/repo
ISSUES_FOR_REPO_URL = 'https://api.github.com/repos/%s/issues' % REPO
AUTH = (GITHUB_USER, GITHUB_PASSWORD)
def write_issues(response, all_labels):
"output a list of issues to csv"
if not r.status_code == 200:
raise Exception(r.status_code)
for issue in r.json():
title = issue['title'].encode('utf-8')
labels = map(lambda l: l['name'], issue['labels'])
if issue['body'] == None:
body = ""
else:
body = issue['body'].encode('utf-8')
print "Writing issue {}".format(title)
if issue['milestone'] == None:
milestone = ""
else:
milestone = issue['milestone']['title']
row = [issue['number'], title, body, issue['created_at'], issue['updated_at'], milestone]
for label in all_labels:
if label in labels:
row.append(label)
else:
row.append("")
csvout.writerow(row)
# Get all the labels
r = requests.get("https://api.github.com/repos/%s/labels" % REPO, auth=AUTH).json()
labels = map(lambda l: l['name'], r)
r = requests.get(ISSUES_FOR_REPO_URL, auth=AUTH)
csvfile = '%s-issues.csv' % (REPO.replace('/', '-'))
csvout = csv.writer(open(csvfile, 'wb'))
csvout.writerow(('id', 'Title', 'Body', 'Created At', 'Updated At', 'Milestone'))
write_issues(r, labels)
#more pages? examine the 'link' header returned
if 'link' in r.headers:
pages = dict(
[(rel[6:-1], url[url.index('<')+1:-1]) for url, rel in
[link.split(';') for link in
r.headers['link'].split(',')]])
while 'last' in pages and 'next' in pages:
pages = dict(
[(rel[6:-1], url[url.index('<')+1:-1]) for url, rel in
[link.split(';') for link in
r.headers['link'].split(',')]])
r = requests.get(pages['next'], auth=AUTH)
write_issues(r, labels)
if pages['next'] == pages['last']:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment