Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pixelead0/05ab70c9b5da8e50f0d9a88a08392092 to your computer and use it in GitHub Desktop.
Save pixelead0/05ab70c9b5da8e50f0d9a88a08392092 to your computer and use it in GitHub Desktop.
Export Issues from Github repo to CSV (API v3)
"""
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.
reference: https://developer.github.com/v3/issues/
"""
import csv
import requests
GITHUB_USER = ''
GITHUB_PASSWORD = ''
REPO = 'username/repo' # format is username/repo
STATE ='all' # State is open, closed, or all.
ISSUES_FOR_REPO_URL = 'https://api.github.com/repos/%s/issues?state=%s' % (REPO, STATE)
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():
try:
milestone = issue['milestone']['title'] + ': ' + issue['milestone']['description']
except:
milestone = ''
labels = issue['labels']
csvLabel =''
for label in labels:
csvLabel = csvLabel + label['name'] + '; '
csvout.writerow([
issue['state'],
issue['number'],
issue['title'].encode('utf-8'),
issue['comments'],
issue['created_at'],
issue['updated_at'],
issue['closed_at'],
issue['html_url'].encode('utf-8'),
csvLabel.encode('utf-8'),
milestone.encode('utf-8'),
issue['body'].encode('utf-8')
])
r = requests.get(ISSUES_FOR_REPO_URL, auth=AUTH)
csvfile = '%s-issues.csv' % (REPO.replace('/', '-'))
csvout = csv.writer(open(csvfile, 'wb'))
csvout.writerow((
'state',
'id',
'Title',
'comments',
'Created At',
'Updated At',
'Closed at',
'url',
'Labels',
'Milestone',
'Body'
))
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:
r = requests.get(pages['next'], auth=AUTH)
write_issues(r)
if pages['next'] == pages['last']:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment