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