Skip to content

Instantly share code, notes, and snippets.

@saiyam1814
Created January 15, 2018 16:16
Show Gist options
  • Save saiyam1814/7bdea28eea4510de4505b56df8d0f5b7 to your computer and use it in GitHub Desktop.
Save saiyam1814/7bdea28eea4510de4505b56df8d0f5b7 to your computer and use it in GitHub Desktop.
Exports Issues from a GIT repository to a CSV file Raw
"""
Exports Issues from a GIT repository to a CSV file
you can also change the status from open to all or closed to get specific issue list
TO USE:
-use your GitHub Usernam and Token in GITHUB_USER and GITHUB_PASSWORD respectively
-give the repository name in REPO
-run the file python gitissues.py
You are all set !!
"""
import csv
import requests
import json
GITHUB_USER = 'Username'
GITHUB_PASSWORD = 'GithubToken'
REPO = 'RepoName'
ISSUES_FOR_REPO_URL = 'https://github.com/api/v3/repos/%s/issues?state=open' % 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)
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:
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