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