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