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