Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save prateek032/06273e179bb034800c61 to your computer and use it in GitHub Desktop.
Save prateek032/06273e179bb034800c61 to your computer and use it in GitHub Desktop.
"""
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 json
GITHUB_USER = 'USER NAME'
GITHUB_PASSWORD = 'PASSWORD'
REPO = 'REPO NAME' # format is username/repo
ISSUES_FOR_REPO_URL = 'https://api.github.com/repos/%s/issues' % REPO
AUTH = (GITHUB_USER, GITHUB_PASSWORD)
arg="?state=all"
def write_issues(r):
"output a list of issues to csv"
if not r.status_code == 200:
raise Exception(r.status_code)
for issue in r.json():
Tag=[]
labels = issue['labels']
for label in labels:
Tag.append(label['name'])
csvout.writerow([issue['number'], issue['title'].encode('utf-8'),Tag,issue['state'] ,issue['created_at'], issue['closed_at']])
r = requests.get(ISSUES_FOR_REPO_URL+arg)
csvfile = '%s-issues.csv' % (REPO.replace('/', '-'))
csvfileo=open(csvfile, 'wb')
csvout = csv.writer(csvfileo)
csvout.writerow(('id', 'Title','Tag','State','Open Date', 'Close Date'))
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
pages = dict(
[(rel[6:-1], url[url.index('<')+1:-1]) for url, rel in
[link.split(';') for link in
r.headers['link'].split(',')]])
csvfileo.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment