Skip to content

Instantly share code, notes, and snippets.

@necaris
Created January 15, 2015 19:42
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 necaris/0b935cfd26baf347e29e to your computer and use it in GitHub Desktop.
Save necaris/0b935cfd26baf347e29e to your computer and use it in GitHub Desktop.
# https://gist.github.com/unbracketed/3380407
"""
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 sys
import csv
import requests
GITHUB_USER = ''
GITHUB_PASSWORD = ''
REPO = '' # format is username/repo
LABEL = ''
PARAMS = ['GITHUB_USER', 'GITHUB_PASSWORD', 'REPO', 'LABEL']
# Add args -- user, pass, repo
for arg, name in zip(sys.argv[1:], PARAMS):
globals()[name] = arg
if not all([globals()[x] for x in PARAMS]):
print("You need to pass user, password, repo, label")
sys.exit(1)
ISSUES_FOR_REPO_URL = 'https://api.github.com/repos/%s/issues' % REPO
AUTH = (GITHUB_USER, GITHUB_PASSWORD)
SEEN_ISSUES = set()
ISSUE_COUNT = 0
def write_issues(response):
"output a list of issues to csv"
global ISSUE_COUNT
if not r.status_code == 200:
raise Exception(r.status_code)
for issue in r.json():
if issue['id'] in SEEN_ISSUES:
print "Already seen", issue['id'], "!!"
sys.exit(1)
SEEN_ISSUES.add(issue['id'])
labels = issue['labels']
if LABEL in [x['name'] for x in issue['labels']]:
ISSUE_COUNT += 1
sys.stderr.write("Wrote {} issues to CSV...\r".format(ISSUE_COUNT))
csvout.writerow([issue['number'], issue['title'].encode('utf-8'),
issue['body'].encode('utf-8'), issue['created_at'],
issue['updated_at']])
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 = {}
def update_pages(headers):
links = headers['link'].split(',')
link_pairs = [link.split(';') for link in links]
kvs = [(rel[6:-1], url[url.index('<')+1:-1]) for url, rel in link_pairs]
pages.update(dict(kvs))
update_pages(r.headers)
while ('last' in pages and 'next' in pages and pages['next'] != pages['last']):
update_pages(r.headers)
r = requests.get(pages['next'], auth=AUTH)
write_issues(r)
print "\nDone! Output in", csvfile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment