Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Kebiled/7b035d7518fdfd50d07e2a285aff3977 to your computer and use it in GitHub Desktop.
Save Kebiled/7b035d7518fdfd50d07e2a285aff3977 to your computer and use it in GitHub Desktop.
Export Issues from Github repo to CSV (API v3)
"""
Exports Issues from a list of repositories to individual CSV files
Uses basic authentication (Github username + password) to retrieve Issues
from a repository that username has access to. Supports Github API v3.
Derived from https://gist.github.com/Billy-/96b16b7682a19a562b277c1ab52547a5
"""
import csv
import requests
import json
GITHUB_USER = ''
GITHUB_PASSWORD = ''
REPO_LIST = [''] # format is 'username/repo'
AUTH = (GITHUB_USER, GITHUB_PASSWORD)
txtout = open('data.json', 'w')
issues = 0
def write_issues(r, csvout):
"output a list of issues to csv"
if not r.status_code == 200:
raise Exception(r.status_code)
json.dump(r.json(), txtout, indent=4)
for issue in r.json():
print issue['number']
if 'pull_request' not in issue:
global issues
issues += 1
csvout.writerow([issue['number'], issue['title'].encode('utf-8'), issue['body'].encode('utf-8'), issue['created_at'], issue['updated_at']])
else:
print '%s is PR' % issue['number']
def get_issues(repo_name):
issues = 0
issues_for_repo_url = 'https://api.github.com/repos/%s/issues' % repo_name
r = requests.get(issues_for_repo_url, auth=AUTH)
csvfilename = '%s-issues.csv' % (repo_name.replace('/', '-'))
csvfile = open(csvfilename, 'wb')
csvout = csv.writer(csvfile)
csvout.writerow(('id', 'Title', 'Body', 'Created At', 'Updated At'))
print 'Now exporting issues from %s' % (repo_name.split('/')[1])
write_issues(r, csvout)
# 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:
pages = dict(
[(rel[6:-1], url[url.index('<')+1:-1]) for url, rel in
[link.split(';') for link in
r.headers['link'].split(',')]])
print pages['next']
r = requests.get(pages['next'], auth=AUTH)
write_issues(r, csvout)
if pages['next'] == pages['last']:
break
csvout.writerow(['Total', issues])
csvfile.close()
for repo in REPO_LIST:
get_issues(repo)
txtout.close()
"""
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.
Derived from https://gist.github.com/Billy-/96b16b7682a19a562b277c1ab52547a5
"""
import csv
import requests
import json
GITHUB_USER = ''
GITHUB_PASSWORD = ''
REPO = '' # format is username/repo
ISSUES_FOR_REPO_URL = 'https://api.github.com/repos/%s/issues' % REPO
AUTH = (GITHUB_USER, GITHUB_PASSWORD)
txtout = open('data.json', 'w')
def write_issues(response):
"output a list of issues to csv"
if not r.status_code == 200:
raise Exception(r.status_code)
json.dump(r.json(), txtout, indent=4)
for issue in r.json():
print issue['number']
if 'pull_request' not in issue:
global issues
issues += 1
csvout.writerow([issue['number'], issue['title'].encode('utf-8'), issue['body'].encode('utf-8'), issue['created_at'], issue['updated_at']])
else:
print '%s is PR' % issue['number']
issues = 0
r = requests.get(ISSUES_FOR_REPO_URL, auth=AUTH)
csvfilename = '%s-issues.csv' % (REPO.replace('/', '-'))
csvfile = open(csvfilename, 'wb')
csvout = csv.writer(csvfile)
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 = 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:
pages = dict(
[(rel[6:-1], url[url.index('<')+1:-1]) for url, rel in
[link.split(';') for link in
r.headers['link'].split(',')]])
print pages['next']
r = requests.get(pages['next'], auth=AUTH)
write_issues(r)
if pages['next'] == pages['last']:
break
csvout.writerow(['Total', issues])
csvfile.close()
txtout.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment