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