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