Skip to content

Instantly share code, notes, and snippets.

@justjkk
Created May 2, 2012 20:13
Show Gist options
  • Save justjkk/2580033 to your computer and use it in GitHub Desktop.
Save justjkk/2580033 to your computer and use it in GitHub Desktop.
Backup github issues
#!/usr/bin/env python
"""
Backup github issues
Input: Read from settings.py
Output: JSON data written to stdout
Usage: ./github-issues.py >xyz-issues.json
"""
from __future__ import print_function
import httplib
import json
import sys
try:
from settings import TOKEN, USER, REPO
except ImportError:
print('Create settings.py with TOKEN, USER, REPO', file=sys.stderr)
sys.exit(0)
headers = {
"Authorization": "token " + TOKEN
}
conn = httplib.HTTPSConnection("api.github.com")
def assert_200(response):
"""Assert 200 response or print response body"""
assert response.status == 200, response.read()
def info(*message, **kwargs):
"""Print Info messages to stderr"""
print(*message, file=sys.stderr, **kwargs)
def get_issues(user, repo):
"""Get Issues from github(without comments)
:param user - User or Organization name
:param repo - Repository name
:returns List of open and closed issues
"""
issues = []
open_issues_url = '/repos/{user}/{repo}/issues?state=open'.format(
user=user,
repo=repo)
info('Getting open issues', end=' ')
while(True):
conn.request('GET', open_issues_url, None, headers)
response = conn.getresponse()
link = response.getheader('link')
assert_200(response)
info('.', end='')
issues += json.load(response)
if not link:
break
for link_item in link.split(','):
if 'rel="next"' in link_item:
open_issues_url = link_item.strip()[1:-13]
break # Go fetch the next page
else:
break # Break out of while
info() # New line
closed_issues_url = '/repos/{user}/{repo}/issues?state=closed'.format(
user=user,
repo=repo)
info('Getting closed issues', end=' ')
while(True):
conn.request('GET', closed_issues_url, None, headers)
response = conn.getresponse()
link = response.getheader('link')
assert_200(response)
info('.', end='')
issues += json.load(response)
if not link:
break
for link_item in link.split(','):
if 'rel="next"' in link_item:
closed_issues_url = link_item.strip()[1:-13]
break # Go fetch the next page
else:
break # Break out of while
info() # New line
info('Got', len(issues), 'issues')
return issues
def get_issues_with_comments(user, repo):
"""Get Issues from github with comments
This function calls `get_issues` and adds comments list under
'comments_data' key
:param user - User or Organization name
:param repo - Repository name
:returns List of open and closed issues with comments
"""
issues = get_issues(user, repo)
info('Getting comments for issues', end=' ')
for issue in issues:
conn.request('GET',
issue['url'][len('https://api.github.com'):] + '/comments',
None,
headers)
response = conn.getresponse()
assert_200(response)
info('.', end='')
data = json.load(response)
issue['comments_data'] = data
info() # New line
return issues
if __name__ == '__main__':
result = get_issues_with_comments(USER, REPO)
print(json.dumps(result, indent=4))
info('Done')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment