Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tedsteinmann/b93ed8253848a2ea78b65d1013e67c5e to your computer and use it in GitHub Desktop.
Save tedsteinmann/b93ed8253848a2ea78b65d1013e67c5e 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.
"""
import csv
import requests
import datetime
#Update your personal API token -- obtain from: https://github.com/settings/tokens
PERSONAL_TOKEN = '[ENTER YOUR PERSONAL API KEY HERE]'
headers = {'Authorization': 'token %s' % PERSONAL_TOKEN }
# Update your filter here. Filter is who owns the issue. State is open, closed, or all.
params_payload = {'filter' : 'all', 'state' : 'all' }
# Specify the repo
REPO = '[ENTER YOUR REPO HERE]' # format is username/repo OR organization/repo
TODAY = datetime.date.today()
ISSUES_FOR_REPO_URL = 'https://api.github.com/repos/%s/issues' % REPO
# add the def write_issues() here
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():
csvout.writerow([issue['number'], issue['title'].encode('utf-8'), issue['body'].encode('utf-8'), issue['state'], issue['created_at'], issue['updated_at'], issue['closed_at']])
r = requests.get(ISSUES_FOR_REPO_URL, params=params_payload, headers=headers)
csvfile = '%s-issues-%s.csv' % (REPO.replace('/', '-'), TODAY)
csvout = csv.writer(open(csvfile, 'wb'))
csvout.writerow(('id', 'Title', 'Body','State', 'Created At', 'Updated At', 'Closed At'))
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'], headers=headers)
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