Skip to content

Instantly share code, notes, and snippets.

@bitc
Created May 23, 2016 11:34
Show Gist options
  • Save bitc/a13965916d04b5c50f3109e90c8656f5 to your computer and use it in GitHub Desktop.
Save bitc/a13965916d04b5c50f3109e90c8656f5 to your computer and use it in GitHub Desktop.
Export GitHub issues of a single repository (including comments) to JSON file
import requests
import json
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)
all_issues = []
def write_issues(r):
if not r.status_code == 200:
raise Exception(r.status_code)
for issue in r.json():
if issue['comments'] > 0:
comments = get_comments(issue['comments_url'])
issue['comments_inline'] = comments
all_issues.append(issue)
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(',')]])
if 'next' in pages:
write_issues(requests.get(pages['next'], auth=AUTH))
def get_comments(url):
r = requests.get(url, auth=AUTH)
if 'link' in r.headers:
raise Exception('Paging not supported for comments download')
return r.json()
write_issues(requests.get(ISSUES_FOR_REPO_URL, auth=AUTH))
print json.dumps(all_issues, sort_keys=True, indent=4, separators=(',', ': '))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment