Skip to content

Instantly share code, notes, and snippets.

@chicagobuss
Created February 2, 2018 22:04
Show Gist options
  • Save chicagobuss/9d3bacebf3844205d3f4b1841462075d to your computer and use it in GitHub Desktop.
Save chicagobuss/9d3bacebf3844205d3f4b1841462075d to your computer and use it in GitHub Desktop.
github export
```#!/usr/bin/env python
"""
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 sys
if len(sys.argv) > 3:
print "Usage: ./export.py <gh user> <gh pass/token> <repo>"
GITHUB_USER = sys.argv[1]
GITHUB_PASSWORD = sys.argv[2]
REPO = sys.argv[3] # format is username/repo
ISSUES_FOR_REPO_URL = 'https://api.github.com/repos/%s/issues' % REPO
AUTH = (GITHUB_USER, GITHUB_PASSWORD)
def write_issues(response):
"output a list of issues to csv"
if not r.status_code == 200:
print r.text
raise Exception(r.status_code)
for issue in r.json():
labels = issue['labels']
for label in labels:
if label['name'] == "Client Requested":
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)
print "tried url: %s with auth: %s" % (ISSUES_FOR_REPO_URL, 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 = 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