Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Export Issues from Github repo to CSV (API v3)
"""
Exports issues from a list of repositories to individual csv files.
Uses basic authentication (Github username + password) to retrieve issues
from a repository that username has access to. Supports Github API v3.
Forked from: unbracketed/export_repo_issues_to_csv.py
"""
import argparse
import csv
from getpass import getpass
import requests
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['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'.format(name.replace('/', '-'))
with open(csvfilename, 'w', newline='') as csvfile:
csvout = csv.writer(csvfile)
csvout.writerow(['Labels', '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
parser = argparse.ArgumentParser(description="Write GitHub repository issues "
"to CSV file.")
parser.add_argument('repositories', nargs='+', help="Repository names, "
"formatted as 'username/repo'")
parser.add_argument('--all', action='store_true', help="Returns both open "
"and closed issues.")
args = parser.parse_args()
if args.all:
state = 'all'
username = input("Username for 'https://github.com': ")
password = getpass("Password for 'https://{}@github.com': ".format(username))
auth = (username, password)
for repository in args.repositories:
get_issues(repository)
@patrickfuller

This comment has been minimized.

Copy link
Owner Author

patrickfuller commented Nov 8, 2016

Edits:

  • Prompt for username/pass
  • Repositories passed as arguments
  • Python3 / PEP8
  • Option to get both open and closed issues

Help: python github_to_csv.py --help
One repo: python github_to_csv.py username/repository
Multiple repos, get open and closed: python github_to_csv.py --all username/repository username/other-repository

@ves-krd

This comment has been minimized.

Copy link

ves-krd commented Dec 27, 2016

Removed newline='' and it works fine.

Another thing to note, the username format is "username" with quotes.

@jschristie

This comment has been minimized.

Copy link

jschristie commented Sep 3, 2017

For some reason, i am not able to run the script, i get errors as below
D:\Gluu>python github_issues_to_csv.py jschristie/docs-ce-prod
Username for 'https://github.com': jschristie
Password for 'https://username@github.com':
Traceback (most recent call last):
File "github_issues_to_csv.py", line 70, in
get_issues(repository)
File "github_issues_to_csv.py", line 38, in get_issues
write_issues(r, csvout)
File "github_issues_to_csv.py", line 19, in write_issues
raise Exception(r.status_code)
Exception: 404

Can anyone throw some light on this, FYI: i am not a developer

i am using python 3.5 and have installed requests module.

@dario-javier-rick

This comment has been minimized.

Copy link

dario-javier-rick commented Mar 22, 2018

Thanks! Very usefull script

@angelleye

This comment has been minimized.

Copy link

angelleye commented May 9, 2018

Not having any luck with this. I'm getting...

Traceback (most recent call last):
  File "github_issues_to_csv.py", line 10, in <module>
    import requests
ModuleNotFoundError: No module named 'requests'

Any tips to fix this would be greatly appreciated. I'm rather shocked it's so difficult to get GitHub issues into Jira.

@raymondjplante

This comment has been minimized.

Copy link

raymondjplante commented Jul 30, 2018

@angelleye run pip install requests

@jrc

This comment has been minimized.

Copy link

jrc commented Nov 15, 2018

usage: python ./github_issues_to_csv.py <org-name>/<repo-name>

You may need to use a personal access token instead of your password: https://github.com/settings/tokens

@BrizzleRocker

This comment has been minimized.

Copy link

BrizzleRocker commented Mar 15, 2019

Just used this script and made some amendments here to handle UTF-8 and issue number: https://gist.github.com/BrizzleRocker/87780652b4ec37794dc6992935556062

@manu4387

This comment has been minimized.

Copy link

manu4387 commented Oct 23, 2019

Getting this error:

Traceback (most recent call last):
File "export repository.py", line 72, in
get_issues(repository)
File "export repository.py", line 38, in get_issues
write_issues(r, csvout)
File "export repository.py", line 19, in write_issues
raise Exception(r.status_code)
Exception: 401

Has anyone able to resolve this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.