Skip to content

Instantly share code, notes, and snippets.

@byrnehollander
Created October 3, 2017 17:10
Show Gist options
  • Save byrnehollander/3c9a1ec113f72f8386df0c345cab6be5 to your computer and use it in GitHub Desktop.
Save byrnehollander/3c9a1ec113f72f8386df0c345cab6be5 to your computer and use it in GitHub Desktop.
import csv
import requests
import datetime
import os
# follow https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/ to generate token
GITHUB_TOKEN = ''
REPO = 'seatgeek/tixcast' # format is username/repo
ISSUES_FOR_REPO_URL = 'https://api.github.com/repos/%s/issues' % REPO
LABEL = "team: checkout"
params_payload = {'state' : 'open', 'type': 'issue' }
def write_issues(response, csvout):
"output a list of issues to csv"
i = 0
for issue in response.json():
labels = issue['labels']
label_string = ''
for label in labels:
if label['name'] == LABEL:
i += 1
label_string = "%s, %s" % (label_string, label['name'])
print issue['html_url'].split('/')[-1] + ", "
csvout.writerow([issue['html_url'].split('/')[-1]])
print "Saved %i issues" % i
def get_issues(url):
kwargs = {
'headers': {
'Content-Type': 'application/vnd.github.v3.raw+json',
'User-Agent': 'GitHub issue exporter'
},
'params': params_payload
}
if GITHUB_TOKEN != '':
kwargs['headers']['Authorization'] = 'token %s' % GITHUB_TOKEN
else:
print("No token provided... quitting")
quit()
resp = requests.get(url, **kwargs)
if resp.status_code != 200:
print("status code %i" % resp.status_code)
raise Exception(resp.status_code)
return resp
def next_page(response):
#more pages? examine the 'link' header returned
if 'link' in response.headers:
pages = dict(
[(rel[6:-1], url[url.index('<')+1:-1]) for url, rel in
[link.split(';') for link in
response.headers['link'].split(',')]])
# import ipdb; ipdb.set_trace()
if 'last' in pages and 'next' in pages:
return pages['next']
return None
def process(csvfile, csvout, url=ISSUES_FOR_REPO_URL):
resp = get_issues(url)
write_issues(resp, csvout)
next_ = next_page(resp)
if next_ is not None:
process(csvfile, csvout, next_)
def main():
# str(datetime.datetime.now()
csvfile = 'team-checkout-issues.txt'
csvout = csv.writer(open(csvfile, 'wb'), delimiter=',')
process(csvfile, csvout)
print "Done!"
main()
# with open('team-checkout-issues.txt') as f:
# content = f.read().splitlines()
#
# list = []
#
# for line in content:
# list.insert(0, int(line))
#
# print list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment