Skip to content

Instantly share code, notes, and snippets.

@mkurz
Forked from unbracketed/export_repo_issues_to_csv.py
Last active October 11, 2015 15:19
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 mkurz/0dc84aeae171e7f7f28c to your computer and use it in GitHub Desktop.
Save mkurz/0dc84aeae171e7f7f28c to your computer and use it in GitHub Desktop.
Export Issues from Github repo to CSV (API v3)
#!/usr/bin/env python
"""
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
import json
GITHUB_USER = ''
GITHUB_PASSWORD = ''
REPO = '' # format is username/repo
ISSUES_FOR_REPO_URL = 'https://api.github.com/repos/%s/issues' % REPO + '?state=all'
AUTH = (GITHUB_USER, GITHUB_PASSWORD)
def write_issues(response):
"output a list of issues to csv"
if not r.status_code == 200:
raise Exception(r.status_code)
for issue in r.json():
if 'pull_request' in issue:
print 'PR - Do not count! (%s)' % issue['number']
continue
print issue['user']['login']
csvout.writerow([issue['user']['login']])
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'))
write_issues(r)
#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(',')]])
print "***"
print pages
while 'last' in pages and 'next' in pages:
print pages['next']
r = requests.get(pages['next'], auth=AUTH)
write_issues(r)
if pages['next'] == pages['last']:
break
pages = dict(
[(rel[6:-1], url[url.index('<')+1:-1]) for url, rel in
[link.split(';') for link in
r.headers['link'].split(',')]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment