-
-
Save rosenhouse/88d13703c5734c7e89e9 to your computer and use it in GitHub Desktop.
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 OAuth2 tokens (https://developer.github.com/v3/#authentication) to retrieve Issues | |
from a repository that username has access to. Supports Github API v3. | |
""" | |
import csv | |
import requests | |
GITHUB_TOKEN='' | |
REPO = '' # format is username/repo | |
ISSUES_FOR_REPO_URL = 'https://api.github.com/repos/%s/issues' % REPO | |
def write_issues(csvout, response): | |
"output a list of issues to csv" | |
if not response.status_code == 200: | |
raise Exception(r.status_code) | |
for issue in response.json(): | |
csvout.writerow([issue['number'], issue['title'].encode('utf-8'), issue['body'].encode('utf-8'), issue['created_at'], issue['updated_at']]) | |
def authenticated_get(url): | |
return requests.get(url, headers={"Authorization":"token " + GITHUB_TOKEN}) | |
def write_all(): | |
csvfile = '%s-issues.csv' % (REPO.replace('/', '-')) | |
csvout = csv.writer(open(csvfile, 'wb')) | |
csvout.writerow(('id', 'Title', 'Body', 'Created At', 'Updated At')) | |
r = authenticated_get(ISSUES_FOR_REPO_URL) | |
write_issues(csvout, 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: | |
r = authenticated_get(pages['next']) | |
write_issues(csvout, r) | |
if pages['next'] == pages['last']: | |
break | |
if __name__ == "__main__": | |
write_all() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment