Skip to content

Instantly share code, notes, and snippets.

@richmahn
Last active June 25, 2020 19:49
Show Gist options
  • Save richmahn/7aa5b71bf4a4db6f2f068f8ce78207f5 to your computer and use it in GitHub Desktop.
Save richmahn/7aa5b71bf4a4db6f2f068f8ce78207f5 to your computer and use it in GitHub Desktop.
Github issues for tc-create-app
import csv
import requests
# Change URL with the options found at https://developer.github.com/v3/issues/
url = 'https://api.github.com/repos/unfoldingword/tc-create-app/issues?state=closed&labels=essential&sort=created&direction=asc'
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([issue['number'], issue['title'], labels, issue['state'], date,
issue['html_url']])
def get_issues():
"""Requests issues from GitHub API and writes to CSV file."""
r = requests.get(url)
csvfilename = 'issues.csv'
with open(csvfilename, 'w') as csvfile:
csvout = csv.writer(csvfile)
csvout.writerow(['#', 'Title', 'Labels', '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'])
write_issues(r, csvout)
if pages['next'] == pages['last']:
break
get_issues()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment