Skip to content

Instantly share code, notes, and snippets.

@troyp
Forked from unbracketed/export_repo_issues_to_csv.py
Last active November 12, 2017 00:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save troyp/8182286ebd34284be2d6cff26ec00ffe to your computer and use it in GitHub Desktop.
Save troyp/8182286ebd34284be2d6cff26ec00ffe to your computer and use it in GitHub Desktop.
Export Issues from Github repo to CSV (API v3)
user_data.sh
"""
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.
"""
from __future__ import print_function
import csv
import requests
import sys
import os
GITHUB_USER = os.environ["GITHUB_USER"]
GITHUB_PASSWORD = os.environ["GITHUB_PASSWORD"]
REPO = os.environ["REPO"] # 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"
response.raise_for_status()
for issue in response.json():
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)
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)
try:
write_issues(r)
except requests.HTTPError as err:
print(err, file=sys.stderr)
if pages['next'] == pages['last']:
break
#!/bin/bash
scriptdir=`dirname $(readlink -f $0)`
source "$scriptdir"/user_data.sh;
export REPO="$1"; # format is username/repo
python "$scriptdir"/export_repo_issues_to_csv.py
export GITHUB_USER=
export GITHUB_PASSWORD=
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment