##// END OF EJS Templates
some fixs
Matthias BUSSONNIER -
Show More
@@ -73,10 +73,14 b" def post_gist(content, description='', filename='file', auth=False):"
73 73 response_data = json.loads(response.text)
74 74 return response_data['html_url']
75 75
76 def get_pull_request(project, num, httpv2=False):
77 if httpv2 :
76 def get_pull_request(project, num, github_api=3):
77 """get pull request info by number
78
79 github_api : version of github api to use
80 """
81 if github_api==2 :
78 82 url = "http://github.com/api/v2/json/pulls/{project}/{num}".format(project=project, num=num)
79 else:
83 elif github_api == 3:
80 84 url = "https://api.github.com/repos/{project}/pulls/{num}".format(project=project, num=num)
81 85 response = requests.get(url)
82 86 response.raise_for_status()
@@ -84,8 +88,12 b' def get_pull_request(project, num, httpv2=False):'
84 88 return json.loads(response.text)['pull']
85 89 return json.loads(response.text)
86 90
87 def get_pulls_list(project,httpv2=False):
88 if not httpv2 :
91 def get_pulls_list(project, github_api=3):
92 """get pull request list
93
94 github_api : version of github api to use
95 """
96 if github_api == 3 :
89 97 url = "https://api.github.com/repos/{project}/pulls".format(project=project)
90 98 else :
91 99 url = "http://github.com/api/v2/json/pulls/{project}".format(project=project)
@@ -2,12 +2,13 b''
2 2 # -*- coding: utf-8 -*-
3 3 """
4 4 Usage:
5 python test_pr.py 1657
5 python git-mpr.py -m 1657
6 6 """
7 7 from __future__ import print_function
8 8
9 9 import re
10 10 import requests
11 import argparse
11 12 from subprocess import call, check_call, check_output, CalledProcessError
12 13 import sys
13 14
@@ -17,7 +18,14 b" ipy_repository = 'git://github.com/ipython/ipython.git'"
17 18 gh_project="ipython/ipython"
18 19 not_merged={}
19 20
20 def get_branch(repo, branch, owner, mergeable):
21
22 def merge_branch(repo, branch, owner, mergeable):
23 """try to merge the givent branch into the current one
24
25 If something does not goes smoothly, merge is aborted
26
27 Returns True if merge sucessfull, False otherwise
28 """
21 29 merged_branch = "%s-%s" % (owner, branch)
22 30 # Delete the branch first
23 31 try :
@@ -27,36 +35,40 b' def get_branch(repo, branch, owner, mergeable):'
27 35 return False
28 36 return True
29 37
30 def merge_pr(num,httpv2=False):
38
39 def merge_pr(num,github_api=3):
40 """ try to merge the branch of PR `num` into current branch
41
42 github_api : use github api v2 (to bypass https and issues with proxy) to find the
43 remote branch that should be merged by it's number
44 """
31 45 # Get Github authorisation first, so that the user is prompted straight away
32 46 # if their login is needed.
33 47
34 pr = gh_api.get_pull_request(gh_project, num, httpv2)
35 if(httpv2):
48 pr = gh_api.get_pull_request(gh_project, num, github_api)
49 if github_api == 2:
36 50 repo = pr['head']['repository']['url']
37 owner=pr['head']['user']['name'],
38 else :
39 repo=pr['head']['repo']['clone_url']
40 owner=pr['head']['repo']['owner']['login'],
51 owner = pr['head']['user']['name']
52 elif github_api == 2 :
53 repo = pr['head']['repo']['clone_url']
54 owner = pr['head']['repo']['owner']['login']
41 55
42 branch=pr['head']['ref']
43 mergeable = get_branch(repo=repo,
56 branch = pr['head']['ref']
57 mergeable = merge_branch(repo=repo,
44 58 branch=branch,
45 59 owner=owner,
46 60 mergeable=pr['mergeable'],
47 61 )
48 62 if not mergeable :
49 63 cmd = "git pull "+repo+" "+branch
50 not_merged[str(num)]=cmd
64 not_merged[str(num)] = cmd
51 65 print("==============================================================================")
52 66 print("Something went wrong merging this branch, you can try it manually by runngin :")
53 67 print(cmd)
54 68 print("==============================================================================")
55 69
56 70
57
58 if __name__ == '__main__':
59 import argparse
71 def main(*args):
60 72 parser = argparse.ArgumentParser(
61 73 description="""
62 74 Merge (one|many) github pull request by their number.\
@@ -65,7 +77,7 b" if __name__ == '__main__':"
65 77 and continue to the next if any.
66 78 """
67 79 )
68 parser.add_argument('-v2','--githubapiv2', action='store_const', const=True)
80 parser.add_argument('-v2','--githubapiv2', action='store_const', const=2)
69 81
70 82 grp = parser.add_mutually_exclusive_group()
71 83 grp.add_argument(
@@ -87,11 +99,15 b" if __name__ == '__main__':"
87 99 metavar='pr-number')
88 100 not_merged = {};
89 101 args = parser.parse_args()
90 ghv2 = args.githubapiv2
102 if args.githubapiv2 == 2 :
103 github_api = 2
104 else :
105 github_api = 3
106
91 107 if(args.list):
92 pr_list = gh_api.get_pulls_list(gh_project, ghv2)
108 pr_list = gh_api.get_pulls_list(gh_project, github_api)
93 109 for pr in pr_list :
94 mergeable = gh_api.get_pull_request(gh_project, pr['number'],httpv2=ghv2)['mergeable']
110 mergeable = gh_api.get_pull_request(gh_project, pr['number'],github_api=github_api)['mergeable']
95 111
96 112 ismgb = u"√" if mergeable else " "
97 113 print(u"* #{number} [{ismgb}]: {title}".format(
@@ -107,7 +123,7 b" if __name__ == '__main__':"
107 123
108 124 elif args.merge:
109 125 for num in args.merge :
110 merge_pr(num,httpv2=ghv2)
126 merge_pr(num, github_api=github_api)
111 127
112 128 if not_merged :
113 129 print('*************************************************************************************')
@@ -115,3 +131,6 b" if __name__ == '__main__':"
115 131 for num,cmd in not_merged.items() :
116 132 print( "PR {num}: {cmd}".format(num=num,cmd=cmd))
117 133 print('*************************************************************************************')
134
135 if __name__ == '__main__':
136 main()
General Comments 0
You need to be logged in to leave comments. Login now