Skip to content

Instantly share code, notes, and snippets.

@mryoshio
Created February 8, 2019 03:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mryoshio/d83cf5854b10347f4d127a8329cf6e3a to your computer and use it in GitHub Desktop.
Save mryoshio/d83cf5854b10347f4d127a8329cf6e3a to your computer and use it in GitHub Desktop.
"""
ref. https://gist.github.com/mike-ward/23dab967feaf71bde2c1
Exports Issues from a specified repository to a CSV file
- Github API v3: https://developer.github.com/v3/gists/
- ZenHub API: https://github.com/ZenHubIO/API
"""
import csv
import requests
import sys
GH_REPO = '<repo name>' # format is username/repo
GH_ISSUES_URL = 'https://api.github.com/repos/%s/issues' % GH_REPO
GH_TOKEN = '<github personal access token>'
GH_HEADERS = { 'Authorization': 'token %s' % GH_TOKEN }
GH_PARAMS = { 'state': 'open' }
ZH_REPO = '<repo id in zenhub>'
ZH_TOKEN = '<zenhub api token>'
ZH_ISSUE_URL = 'https://api.zenhub.io/p1/repositories/%s/issues' % ZH_REPO
ZH_HEADERS = { 'X-Authentication-Token': ZH_TOKEN }
def write_issues(gh_response):
"output a list of issues to csv"
if not gh_response.status_code == 200:
print(gh_response.text, flush=True)
raise Exception(gh_response.status_code)
for issue in gh_response.json():
labels = [x['name'] for x in issue['labels']]
pipeline = pipeline_from_zenhub(issue['number'])
print((issue['number'], labels, pipeline))
csvout.writerow([pipeline, issue['number'], issue['title'], issue['body'], issue['created_at'], issue['updated_at'], *labels])
def pipeline_from_zenhub(issue_no):
zh_url = ZH_ISSUE_URL + '/%d' % issue_no
zh_res = requests.get(zh_url, headers=ZH_HEADERS)
if not zh_res.status_code == 200:
print('zenhub api with %s' % zh_url, flush=True)
raise Exception(zh_res.status_code)
return zh_res.json()['pipeline']['name']
def extract_paging(gh_response):
return dict(
[(rel[6:-1], url[url.index('<')+1:-1]) for url, rel in
[link.split(';') for link in
gh_response.headers['link'].split(',')]])
filename = '%s-issues.csv' % (GH_REPO.replace('/', '-'))
with open(filename, 'w', encoding='utf_8_sig') as csvfile:
r = requests.get(GH_ISSUES_URL, headers=GH_HEADERS, params=GH_PARAMS)
csvout = csv.writer(csvfile)
csvout.writerow(('pipeline', 'id', 'title', 'body', 'created_at', 'updated_at', 'labels'))
write_issues(r)
pages = extract_paging(r)
print(pages)
#more pages? examine the 'link' header returned
if 'link' in r.headers:
while pages['last'] != pages['next']:
r = requests.get(pages['next'], headers=GH_HEADERS)
write_issues(r)
pages = extract_paging(r)
print(pages, flush=True)
r = requests.get(pages['next'], headers=GH_HEADERS)
write_issues(r)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment