-
-
Save andydempster/87780652b4ec37794dc6992935556062 to your computer and use it in GitHub Desktop.
Export Issues from Github repo to CSV (API v3)
This file contains hidden or 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
""" | |
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 | |
# encoding=utf8 | |
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'.format(name.replace('/', '-')) | |
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 | |
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) |
@manu4387 - what are the steps to reproduce? On OSX you may need to try different versions of python - try running the command using python3
instead of python
OK let me try that.
…On Thu, 24 Oct 2019 at 17:10, Andy Dempster ***@***.***> wrote:
@manu4387 <https://github.com/manu4387> - what are the steps to
reproduce? On OSX you may need to try different versions of python - try
running the command using python3 instead of python
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://gist.github.com/87780652b4ec37794dc6992935556062?email_source=notifications&email_token=ANOUZL2N637ZI7DR6E474ODQQGCSXA5CNFSM4JECCHOKYY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAF3BQY#gistcomment-3064588>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ANOUZL6GJZ7QV5N4H5KRLN3QQGCSXANCNFSM4JECCHOA>
.
--
Manu Agarwal
Senior Implementation Consultant, Siren
A Unit 3, GTC, Mervue Business Park, Galway, H91 CR20, Ireland
P +353 (0)91 704 885 <+353+(0)91+704+885>
M +91-7620037800 <+91-7620037800>
E [email protected] <[email protected]>
W www.siren.io
<http://www.siren.io?utm_source=WiseStamp&utm_medium=email&utm_term=&utm_content=&utm_campaign=signature>
<https://twitter.com/sirensearch?utm_source=WiseStamp&utm_medium=email&utm_term=&utm_content=&utm_campaign=signature>
<https://www.youtube.com/channel/UCKGsC-vD28r7hW6T9QspKPA?utm_source=WiseStamp&utm_medium=email&utm_term=&utm_content=&utm_campaign=signature>
<https://vimeo.com/sirenio?utm_source=WiseStamp&utm_medium=email&utm_term=&utm_content=&utm_campaign=signature>
<https://www.facebook.com/sirensearch?utm_source=WiseStamp&utm_medium=email&utm_term=&utm_content=&utm_campaign=signature>
<https://www.linkedin.com/company/11117365?utm_source=WiseStamp&utm_medium=email&utm_term=&utm_content=&utm_campaign=signature>
I am getting this error with the above code:
Traceback (most recent call last):
File "C:\Data\GISData\PythonCode\githubExtract\test3.py", line 14, in
reload(sys)
NameError: name 'reload' is not defined
Scripts python github_issues_to_csv.py
usage: github_issues_to_csv.py [-h] [--all] repositories [repositories ...]
github_issues_to_csv.py: error: too few arguments
➜ Scripts python3 github_issues_to_csv.py
Traceback (most recent call last):
File "github_issues_to_csv.py", line 14, in <module>
reload(sys)
NameError: name 'reload' is not defined
➜ Scripts
Looking at the 2to3 automated updater
reload
Converts reload() to importlib.reload().
So if you're running v3 try that - otherwise run in Python 2
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
Getting this error