Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import io
import json
import re
import base64
import argparse
import requests
# PYTHON 3
parser = argparse.ArgumentParser()
parser.add_argument('--owner', type=str) # matrix-org
parser.add_argument('--repo', type=str) # synapse
parser.add_argument('--login', type=str) # turt2live:PersonalAccessTokenHere
parser.add_argument('--ref', type=int) # 2603
args = parser.parse_args()
def get_page(url, page, headers=None):
if headers is None:
headers = {}
if args.login is not None:
headers['authorization'] = "Basic %s" % base64.b64encode(args.login.encode('UTF-8')).decode('UTF-8')
url = url + str(page)
resp = requests.get(url, headers=headers)
pages = 1
for link in resp.links.values():
if link['rel'] == 'last':
pages = re.search('page=(\d+)', link['url']).group(1)
val = resp.json()
if not isinstance(val, list):
print(val) # This is just to dump the error in the log
raise Exception("Error calling %s" % url)
return val, pages
def get_issues(owner, repo):
issues = list()
gh_url = 'https://api.github.com/repos/%s/%s/issues?state=all&page=' % (owner, repo)
items, pages = get_page(gh_url, 1)
print("Got %s items for %s%s" % (len(items), gh_url, 1))
issues.extend(items)
for page in range(2, int(pages) + 1):
items, _ = get_page(gh_url, page)
print("Got %s items for %s%s" % (len(items), gh_url, page))
issues.extend(items)
return issues
def get_timeline(owner, repo, issue):
timeline = list()
gh_url = 'https://api.github.com/repos/%s/%s/issues/%s/timeline?page=' % (owner, repo, issue)
headers = {"accept": "application/vnd.github.mockingbird-preview"}
items, pages = get_page(gh_url, 1, headers)
timeline.extend(items)
for page in range(2, int(pages) + 1):
items, _ = get_page(gh_url, page, headers)
timeline.extend(items)
return timeline
print("Getting issues...")
issues = get_issues(args.owner, args.repo)
print("Found %s issues" % len(issues))
references = list()
for issue in issues:
print("Issue #%s:" % issue['number'])
timeline = get_timeline(args.owner, args.repo, issue['number'])
for record in timeline:
#print("\t%s" % record['event'])
if record['event'] == 'cross-referenced' and record['source']['type'] == 'issue':
if record['source']['issue']['number'] == args.ref:
print("\tReferenced from %s" % args.ref)
references.append(issue['url'])
print("Writing %s references to references.json" % len(references))
f = open('references.json', 'w')
f.write(json.dumps(references))
f.close()
requests
argparse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.