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