From 29be02deabea7730a9b9b520f108231452b91387 2013-07-20 00:58:42 From: MinRK Date: 2013-07-20 00:58:42 Subject: [PATCH] add auth and paged request to gh_api --- diff --git a/tools/gh_api.py b/tools/gh_api.py index ac1568c..75a2ce7 100644 --- a/tools/gh_api.py +++ b/tools/gh_api.py @@ -7,6 +7,8 @@ except NameError: pass import os +import re +import sys import requests import getpass @@ -86,21 +88,56 @@ def post_gist(content, description='', filename='file', auth=False): response_data = json.loads(response.text) return response_data['html_url'] -def get_pull_request(project, num): +def get_pull_request(project, num, auth=False): """get pull request info by number """ url = "https://api.github.com/repos/{project}/pulls/{num}".format(project=project, num=num) - response = requests.get(url) + if auth: + header = make_auth_header() + else: + header = None + response = requests.get(url, headers=header) response.raise_for_status() return json.loads(response.text, object_hook=Obj) -def get_pulls_list(project): +element_pat = re.compile(r'<(.+?)>') +rel_pat = re.compile(r'rel=[\'"](\w+)[\'"]') + +def get_paged_request(url, headers=None): + """get a full list, handling APIv3's paging""" + results = [] + while True: + print("fetching %s" % url, file=sys.stderr) + response = requests.get(url, headers=headers) + response.raise_for_status() + results.extend(response.json()) + if 'next' in response.links: + url = response.links['next']['url'] + else: + break + return results + +def get_pulls_list(project, state="closed", auth=False): """get pull request list """ - url = "https://api.github.com/repos/{project}/pulls".format(project=project) - response = requests.get(url) - response.raise_for_status() - return json.loads(response.text) + url = "https://api.github.com/repos/{project}/pulls?state={state}&per_page=100".format(project=project, state=state) + if auth: + headers = make_auth_header() + else: + headers = None + pages = get_paged_request(url, headers=headers) + return pages + +def get_issues_list(project, state="closed", auth=False): + """get pull request list + """ + url = "https://api.github.com/repos/{project}/pulls?state={state}&per_page=100".format(project=project, state=state) + if auth: + headers = make_auth_header() + else: + headers = None + pages = get_paged_request(url, headers=headers) + return pages # encode_multipart_formdata is from urllib3.filepost # The only change is to iter_fields, to enforce S3's required key ordering