Created
January 25, 2016 13:20
-
-
Save kadikraman/d54ee481ac4988c84b5c to your computer and use it in GitHub Desktop.
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 specified repository to a CSV file | |
| Uses basic authentication (Github username + password) to retrieve Issues | |
| from a repository that username has access to. | |
| INSTUCTIONS: | |
| 1. pip install requests | |
| 2. replace GITHUB_USER with your username | |
| 3. replace GITHUB_PASSWORD with your auth token (settings -> personal access tokens) | |
| 4. run as "python export_issues.py" | |
| """ | |
| import csv | |
| import requests | |
| GITHUB_USER = 'user' | |
| GITHUB_PASSWORD = 'password' | |
| REPO = 'redbadger/ct-cms' # 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" | |
| if not response.status_code == 200: | |
| raise Exception(response.status_code) | |
| for issue in response.json(): | |
| print issue | |
| csvout.writerow([issue['number'], issue['title'], issue['body'], 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) | |
| write_issues(r) | |
| if pages['next'] == pages['last']: | |
| break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment