From f50baeec22f91ac6cbe836c97e1d6ac5ae947e75 2012-06-11 18:41:29 From: Matthias BUSSONNIER Date: 2012-06-11 18:41:29 Subject: [PATCH] some fixs --- diff --git a/tools/gh_api.py b/tools/gh_api.py index 828592b..1f2dd2b 100644 --- a/tools/gh_api.py +++ b/tools/gh_api.py @@ -73,10 +73,14 @@ 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, httpv2=False): - if httpv2 : +def get_pull_request(project, num, github_api=3): + """get pull request info by number + + github_api : version of github api to use + """ + if github_api==2 : url = "http://github.com/api/v2/json/pulls/{project}/{num}".format(project=project, num=num) - else: + elif github_api == 3: url = "https://api.github.com/repos/{project}/pulls/{num}".format(project=project, num=num) response = requests.get(url) response.raise_for_status() @@ -84,8 +88,12 @@ def get_pull_request(project, num, httpv2=False): return json.loads(response.text)['pull'] return json.loads(response.text) -def get_pulls_list(project,httpv2=False): - if not httpv2 : +def get_pulls_list(project, github_api=3): + """get pull request list + + github_api : version of github api to use + """ + if github_api == 3 : url = "https://api.github.com/repos/{project}/pulls".format(project=project) else : url = "http://github.com/api/v2/json/pulls/{project}".format(project=project) diff --git a/tools/git-mpr.py b/tools/git-mpr.py index 9e0ca32..e9c0d04 100755 --- a/tools/git-mpr.py +++ b/tools/git-mpr.py @@ -2,12 +2,13 @@ # -*- coding: utf-8 -*- """ Usage: - python test_pr.py 1657 + python git-mpr.py -m 1657 """ from __future__ import print_function import re import requests +import argparse from subprocess import call, check_call, check_output, CalledProcessError import sys @@ -17,7 +18,14 @@ ipy_repository = 'git://github.com/ipython/ipython.git' gh_project="ipython/ipython" not_merged={} -def get_branch(repo, branch, owner, mergeable): + +def merge_branch(repo, branch, owner, mergeable): + """try to merge the givent branch into the current one + + If something does not goes smoothly, merge is aborted + + Returns True if merge sucessfull, False otherwise + """ merged_branch = "%s-%s" % (owner, branch) # Delete the branch first try : @@ -27,36 +35,40 @@ def get_branch(repo, branch, owner, mergeable): return False return True -def merge_pr(num,httpv2=False): + +def merge_pr(num,github_api=3): + """ try to merge the branch of PR `num` into current branch + + github_api : use github api v2 (to bypass https and issues with proxy) to find the + remote branch that should be merged by it's number + """ # Get Github authorisation first, so that the user is prompted straight away # if their login is needed. - pr = gh_api.get_pull_request(gh_project, num, httpv2) - if(httpv2): + pr = gh_api.get_pull_request(gh_project, num, github_api) + if github_api == 2: repo = pr['head']['repository']['url'] - owner=pr['head']['user']['name'], - else : - repo=pr['head']['repo']['clone_url'] - owner=pr['head']['repo']['owner']['login'], + owner = pr['head']['user']['name'] + elif github_api == 2 : + repo = pr['head']['repo']['clone_url'] + owner = pr['head']['repo']['owner']['login'] - branch=pr['head']['ref'] - mergeable = get_branch(repo=repo, + branch = pr['head']['ref'] + mergeable = merge_branch(repo=repo, branch=branch, owner=owner, mergeable=pr['mergeable'], ) if not mergeable : cmd = "git pull "+repo+" "+branch - not_merged[str(num)]=cmd + not_merged[str(num)] = cmd print("==============================================================================") print("Something went wrong merging this branch, you can try it manually by runngin :") print(cmd) print("==============================================================================") - -if __name__ == '__main__': - import argparse +def main(*args): parser = argparse.ArgumentParser( description=""" Merge (one|many) github pull request by their number.\ @@ -65,7 +77,7 @@ if __name__ == '__main__': and continue to the next if any. """ ) - parser.add_argument('-v2','--githubapiv2', action='store_const', const=True) + parser.add_argument('-v2','--githubapiv2', action='store_const', const=2) grp = parser.add_mutually_exclusive_group() grp.add_argument( @@ -87,11 +99,15 @@ if __name__ == '__main__': metavar='pr-number') not_merged = {}; args = parser.parse_args() - ghv2 = args.githubapiv2 + if args.githubapiv2 == 2 : + github_api = 2 + else : + github_api = 3 + if(args.list): - pr_list = gh_api.get_pulls_list(gh_project, ghv2) + pr_list = gh_api.get_pulls_list(gh_project, github_api) for pr in pr_list : - mergeable = gh_api.get_pull_request(gh_project, pr['number'],httpv2=ghv2)['mergeable'] + mergeable = gh_api.get_pull_request(gh_project, pr['number'],github_api=github_api)['mergeable'] ismgb = u"√" if mergeable else " " print(u"* #{number} [{ismgb}]: {title}".format( @@ -107,7 +123,7 @@ if __name__ == '__main__': elif args.merge: for num in args.merge : - merge_pr(num,httpv2=ghv2) + merge_pr(num, github_api=github_api) if not_merged : print('*************************************************************************************') @@ -115,3 +131,6 @@ if __name__ == '__main__': for num,cmd in not_merged.items() : print( "PR {num}: {cmd}".format(num=num,cmd=cmd)) print('*************************************************************************************') + +if __name__ == '__main__': + main()