Skip to content

Commit 10d3ae1

Browse files
committed
devtools: Auto-set branch to merge to in github-merge
As we are already using the API to retrieve the pull request title, also retrieve the base branch. This makes sure that pull requests for 0.12 automatically end up in 0.12, and pull requests for master automatically end up in master, and so on. It is still possible to override the branch from the command line or using the `githubmerge.branch` git option.
1 parent 28ad4d9 commit 10d3ae1

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

contrib/devtools/github-merge.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,19 @@ def git_config_get(option, default=None):
4747
except subprocess.CalledProcessError as e:
4848
return default
4949

50-
def retrieve_pr_title(repo,pull):
50+
def retrieve_pr_info(repo,pull):
5151
'''
52-
Retrieve pull request title from github.
52+
Retrieve pull request information from github.
5353
Return None if no title can be found, or an error happens.
5454
'''
5555
try:
5656
req = Request("https://api.github.com/repos/"+repo+"/pulls/"+pull)
5757
result = urlopen(req)
5858
reader = codecs.getreader('utf-8')
5959
obj = json.load(reader(result))
60-
return obj['title']
60+
return obj
6161
except Exception as e:
62-
print('Warning: unable to retrieve pull title from github: %s' % e)
62+
print('Warning: unable to retrieve pull information from github: %s' % e)
6363
return None
6464

6565
def ask_prompt(text):
@@ -69,28 +69,28 @@ def ask_prompt(text):
6969
print("",file=stderr)
7070
return reply
7171

72-
def parse_arguments(branch):
72+
def parse_arguments():
7373
epilog = '''
7474
In addition, you can set the following git configuration variables:
7575
githubmerge.repository (mandatory),
7676
user.signingkey (mandatory),
7777
githubmerge.host (default: git@github.com),
78-
githubmerge.branch (default: master),
78+
githubmerge.branch (no default),
7979
githubmerge.testcmd (default: none).
8080
'''
8181
parser = argparse.ArgumentParser(description='Utility to merge, sign and push github pull requests',
8282
epilog=epilog)
8383
parser.add_argument('pull', metavar='PULL', type=int, nargs=1,
8484
help='Pull request ID to merge')
8585
parser.add_argument('branch', metavar='BRANCH', type=str, nargs='?',
86-
default=branch, help='Branch to merge against (default: '+branch+')')
86+
default=None, help='Branch to merge against (default: githubmerge.branch setting, or base branch for pull, or \'master\')')
8787
return parser.parse_args()
8888

8989
def main():
9090
# Extract settings from git repo
9191
repo = git_config_get('githubmerge.repository')
9292
host = git_config_get('githubmerge.host','git@github.com')
93-
branch = git_config_get('githubmerge.branch','master')
93+
opt_branch = git_config_get('githubmerge.branch',None)
9494
testcmd = git_config_get('githubmerge.testcmd')
9595
signingkey = git_config_get('user.signingkey')
9696
if repo is None:
@@ -105,9 +105,20 @@ def main():
105105
host_repo = host+":"+repo # shortcut for push/pull target
106106

107107
# Extract settings from command line
108-
args = parse_arguments(branch)
108+
args = parse_arguments()
109109
pull = str(args.pull[0])
110-
branch = args.branch
110+
111+
# Receive pull information from github
112+
info = retrieve_pr_info(repo,pull)
113+
if info is None:
114+
exit(1)
115+
title = info['title']
116+
# precedence order for destination branch argument:
117+
# - command line argument
118+
# - githubmerge.branch setting
119+
# - base branch for pull (as retrieved from github)
120+
# - 'master'
121+
branch = args.branch or opt_branch or info['base']['ref'] or 'master'
111122

112123
# Initialize source branches
113124
head_branch = 'pull/'+pull+'/head'
@@ -147,7 +158,6 @@ def main():
147158

148159
try:
149160
# Create unsigned merge commit.
150-
title = retrieve_pr_title(repo,pull)
151161
if title:
152162
firstline = 'Merge #%s: %s' % (pull,title)
153163
else:
@@ -165,7 +175,7 @@ def main():
165175
print("ERROR: Creating merge failed (already merged?).",file=stderr)
166176
exit(4)
167177

168-
print('%s#%s%s %s' % (ATTR_RESET+ATTR_PR,pull,ATTR_RESET,title))
178+
print('%s#%s%s %s %sinto %s%s' % (ATTR_RESET+ATTR_PR,pull,ATTR_RESET,title,ATTR_RESET+ATTR_PR,branch,ATTR_RESET))
169179
subprocess.check_call([GIT,'log','--graph','--topo-order','--pretty=format:'+COMMIT_FORMAT,base_branch+'..'+head_branch])
170180
print()
171181
# Run test command if configured.

0 commit comments

Comments
 (0)