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