Skip to content

Instantly share code, notes, and snippets.

@dwoz
Last active August 29, 2015 14:14
Show Gist options
  • Save dwoz/2a89f1c9b9f5159b2a15 to your computer and use it in GitHub Desktop.
Save dwoz/2a89f1c9b9f5159b2a15 to your computer and use it in GitHub Desktop.
Dump github issues to csv
#!/usr/bin/env python
import requests
import json
from requests.auth import HTTPBasicAuth
import sys
import StringIO
import csv
from getpass import getpass
# Set this from the app in github.com
CLIENT_ID='d14ca90754898baed003'
CLIENT_KEY=''
def main():
fp = StringIO.StringIO()
csv_writer = csv.writer(fp)
csv_writer.writerow(['Repository', 'Issue Title', 'Issue Description',])
print "Enter your github.com credentials.\n"
name = raw_input("Username: ")
password = getpass("Password: ")
resp = requests.post(
"https://api.github.com/authorizations",
data=json.dumps(
{
'scopes': ['gist', 'repo', 'user', 'admin:org', 'read:org', 'write:org',],
'note': 'DevOps Issues app',
'note_url': 'https://traxtech.com',
'client_id': CLIENT_ID,
'client_secret': CLIENT_KEY,
}
),
auth=HTTPBasicAuth(name, password)
)
assert resp.status_code == 201, resp.status_code
#print json.dumps(resp.json(), sort_keys=True, indent=4, separators=(',', ': ',))
token = resp.json()['token']
headers = {'Authorization': 'token {}'.format(token)}
resp = requests.get("https://api.github.com/orgs/TraxTechnologies/repos?per_page=100", headers=headers)
assert resp.status_code == 200, resp.status_code
# TODO: Need to add support for github's implimintation of results paging by
# looking at the "Link" header to determin results urls
#for k, v in resp.headers.items():
# print k, v
#print "Count", len(resp.json())
count = 0
for i in resp.json():
print "Getting issues from repo", i['name'], i['owner']['login']
resp = requests.get(
"https://api.github.com/repos/{}/{}/issues".format(i['owner']['login'], i['name']),
headers=headers, params={'labels': 'bug'}
)
assert resp.status_code == 200, resp.status_code
#print json.dumps(resp.json(), sort_keys=True, indent=4, separators=(',', ': ',))
for issue in resp.json():
count += 1
csv_writer.writerow([i['name'], issue['title'], issue['body']])
fp.seek(0)
with open('issues.csv', 'w') as out:
out.write(fp.read())
print 'total bug issues', count
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment