- Replace username, password and repo name at the end of file
- Run
python github_issues_to_csv.py
Created
October 4, 2019 11:19
-
-
Save squallstar/3d7e3230211f600e19dd1e8bb1ddc2d1 to your computer and use it in GitHub Desktop.
GitHub issues to CSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding=utf8 | |
import csv | |
import requests | |
import sys | |
reload(sys) | |
sys.setdefaultencoding('utf8') | |
auth = None | |
state = 'open' | |
def write_issues(r, csvout): | |
"""Parses JSON response and writes to CSV.""" | |
if r.status_code != 200: | |
raise Exception(r.status_code) | |
for issue in r.json(): | |
if 'pull_request' not in issue: | |
labels = ', '.join([l['name'] for l in issue['labels']]) | |
date = issue['created_at'].split('T')[0] | |
# Change the following line to write out additional fields | |
csvout.writerow([labels, issue['number'], issue['title'], issue['state'], date, | |
issue['html_url']]) | |
def get_issues(name): | |
"""Requests issues from GitHub API and writes to CSV file.""" | |
url = 'https://api.github.com/repos/{}/issues?state={}'.format(name, state) | |
r = requests.get(url, auth=auth) | |
csvfilename = 'issues.csv' | |
with open(csvfilename, 'w') as csvfile: | |
csvout = csv.writer(csvfile) | |
csvout.writerow(['Labels', 'Number', 'Title', 'State', 'Date', 'URL']) | |
write_issues(r, csvout) | |
# Multiple requests are required if response is paged | |
if 'link' in r.headers: | |
pages = {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 = {rel[6:-1]: url[url.index('<')+1:-1] for url, rel in | |
(link.split(';') for link in | |
r.headers['link'].split(','))} | |
r = requests.get(pages['next'], auth=auth) | |
write_issues(r, csvout) | |
if pages['next'] == pages['last']: | |
break | |
username = "USERNAME" | |
password = "PASSWORD" | |
auth = (username, password) | |
get_issues("REPO") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment