Created
June 3, 2015 06:32
-
-
Save sid24ss/d186e054a5c792de639d to your computer and use it in GitHub Desktop.
Exports *all* issues from a github repository into a `pickle` file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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 cPickle as pickle | |
GITHUB_USER = '' | |
GITHUB_PASSWORD = '' | |
REPO = '' # format is username/repo | |
ISSUES_FOR_REPO_URL = 'https://api.github.com/repos/%s/issues?state=all' % REPO | |
AUTH = (GITHUB_USER, GITHUB_PASSWORD) | |
f = open('issues-dump.p', 'wb') | |
def write_issues(response): | |
"output a list of issues to csv" | |
if not r.status_code == 200: | |
raise Exception(r.status_code) | |
print 'writing ...' | |
print len(r.json()) | |
pickle.dump(r.json(), f) | |
r = requests.get(ISSUES_FOR_REPO_URL, auth=AUTH) | |
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(',')]]) | |
while 'last' in pages and 'next' in pages: | |
print 'getting page : ' + pages['next'] | |
r = requests.get(pages['next'], auth=AUTH) | |
write_issues(r) | |
pages = dict( | |
[(rel[6:-1], url[url.index('<')+1:-1]) for url, rel in | |
[link.split(';') for link in | |
r.headers['link'].split(',')]]) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment