Skip to content

Instantly share code, notes, and snippets.

@shonenada
Created September 6, 2016 09:24
Show Gist options
  • Save shonenada/8c44f903e021527e746027249010dcd0 to your computer and use it in GitHub Desktop.
Save shonenada/8c44f903e021527e746027249010dcd0 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
import base64
import json
import os
import pprint
import sys
import urllib2
GITHUB_API = 'https://api.github.com'
def get_env():
keys = ('GH_USERNAME', 'GH_TOKEN')
return {key: os.environ.get(key, None) for key in keys}
def get_pr_commits(owner, repo, number):
url = '{base}/repos/{owner}/{repo}/pulls/{number}/commits'.format(
base=GITHUB_API, owner=owner, repo=repo, number=number)
envs = get_env()
assert envs['GH_USERNAME']
assert envs['GH_TOKEN']
str_to_encode = '%s:%s' % (envs['GH_USERNAME'], envs['GH_TOKEN'])
basic_str = base64.encodestring(str_to_encode).replace('\n', '')
req = urllib2.Request(url)
req.add_header('Authorization', 'Basic %s' % basic_str)
resp = urllib2.urlopen(req)
data = resp.read()
commits = json.loads(data)
return commits
def main():
args = sys.argv
if not len(args) == 4:
return
commits = get_pr_commits(*args[1:])
shas = [each['sha'] for each in commits]
for each in shas:
print each
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment