Skip to content

Instantly share code, notes, and snippets.

@mjog
Created March 17, 2016 10:46
Show Gist options
  • Save mjog/9aac9d5ec6e22910c80a to your computer and use it in GitHub Desktop.
Save mjog/9aac9d5ec6e22910c80a to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
#
# Downloads all issues with comments for a single GitHub repo.
#
# - Requires Python 3 and the Requests library.
# - Merges comments into issues and saves as a single JSON file.
# - Uses basic auth so it works with both personal and organsiation repos.
#
import json
import requests
repo = '' # Name of the target repository
org = '' # Repo owner - user or org
user = '' # User account login, same as org if for a personal account
secret = '' # User account Password
def main():
global repo, org
print('Listing issues...', end='', flush=True)
issues = list_all(
'https://api.github.com/repos/{}/{}/issues'.format(org,repo),
state='all'
)
print(' found:', len(issues))
for issue in issues:
number = str(issue['number'])
print('Listing comments for issue #{}...'.format(number), end='', flush=True)
comments = list_all(issue['comments_url'])
print(' found:', len(comments))
issue['comments'] = comments
filename = '{}_{}_github_issues.json'.format(org, repo)
print('Writing to: ', filename)
json.dump(issues, open(filename, 'w'), indent=4)
def list_all(url, **query_args):
global user, secret
query_args['per_page'] = '100'
items = []
page = 1
while True:
query_args['page'] = str(page)
response = requests.get(url, params=query_args, auth=(user,secret))
#print(response.url)
new_items = response.json()
if len(new_items) == 0:
break
items += new_items
page += 1
return items
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment