Skip to content

Commit bcdd31f

Browse files
committed
Merge #15165: contrib: Allow use of github API authentication in github-merge
f1bd219 contrib: Allow use of github API authentication in github-merge (Wladimir J. van der Laan) a4c5bbf contrib: Add support for http[s] URLs in github-merge (Wladimir J. van der Laan) 059a3cf contrib: Detailed reporting for http errors in github-merge (Wladimir J. van der Laan) Pull request description: Three commits I had locally for `github-merge.py`: - *Detailed reporting for http errors in github-merge*: Print detailed error, this makes it easier to diagnose github API issues. - *Add support for http[s] URLs in github-merge*: Sometimes it can be useful to use github-merge with read-only access (say, for reviewing and testing from untrusted VMs). - *Allow use of github API authentication in github-merge*: The API request limit for unauthenticated requests is quite low. I started running into rate limiting errors. The limit for authenticated requests is much higher. This patch adds an optional configuration setting `user.ghtoken` that, when set, is used to authenticate requests to the API. Tree-SHA512: ca8ae1874a787263e49d915d7cf31c0c0f50aba229c9440265bf1fda69f7e00641d1492512b93d76c17ff1766859283d640d37770acb120898736ad97efbd5c2
2 parents 16c4a53 + f1bd219 commit bcdd31f

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

contrib/devtools/README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,25 @@ Configuring the github-merge tool for the bitcoin repository is done in the foll
119119

120120
git config githubmerge.repository bitcoin/bitcoin
121121
git config githubmerge.testcmd "make -j4 check" (adapt to whatever you want to use for testing)
122-
git config --global user.signingkey mykeyid (if you want to GPG sign)
122+
git config --global user.signingkey mykeyid
123+
124+
Authentication (optional)
125+
--------------------------
126+
127+
The API request limit for unauthenticated requests is quite low, but the
128+
limit for authenticated requests is much higher. If you start running
129+
into rate limiting errors it can be useful to set an authentication token
130+
so that the script can authenticate requests.
131+
132+
- First, go to [Personal access tokens](https://github.com/settings/tokens).
133+
- Click 'Generate new token'.
134+
- Fill in an arbitrary token description. No further privileges are needed.
135+
- Click the `Generate token` button at the bottom of the form.
136+
- Copy the generated token (should be a hexadecimal string)
137+
138+
Then do:
139+
140+
git config --global user.ghtoken "pasted token"
123141

124142
Create and verify timestamps of merge commits
125143
---------------------------------------------

contrib/devtools/github-merge.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import json
2424
import codecs
2525
from urllib.request import Request, urlopen
26+
from urllib.error import HTTPError
2627

2728
# External tools (can be overridden using environment)
2829
GIT = os.getenv('GIT','git')
@@ -46,17 +47,24 @@ def git_config_get(option, default=None):
4647
except subprocess.CalledProcessError:
4748
return default
4849

49-
def retrieve_pr_info(repo,pull):
50+
def retrieve_pr_info(repo,pull,ghtoken):
5051
'''
5152
Retrieve pull request information from github.
5253
Return None if no title can be found, or an error happens.
5354
'''
5455
try:
5556
req = Request("https://api.github.com/repos/"+repo+"/pulls/"+pull)
57+
if ghtoken is not None:
58+
req.add_header('Authorization', 'token ' + ghtoken)
5659
result = urlopen(req)
5760
reader = codecs.getreader('utf-8')
5861
obj = json.load(reader(result))
5962
return obj
63+
except HTTPError as e:
64+
error_message = e.read()
65+
print('Warning: unable to retrieve pull information from github: %s' % e)
66+
print('Detailed error: %s' % error_message)
67+
return None
6068
except Exception as e:
6169
print('Warning: unable to retrieve pull information from github: %s' % e)
6270
return None
@@ -134,6 +142,7 @@ def parse_arguments():
134142
In addition, you can set the following git configuration variables:
135143
githubmerge.repository (mandatory),
136144
user.signingkey (mandatory),
145+
user.ghtoken (default: none).
137146
githubmerge.host (default: [email protected]),
138147
githubmerge.branch (no default),
139148
githubmerge.testcmd (default: none).
@@ -152,6 +161,7 @@ def main():
152161
host = git_config_get('githubmerge.host','[email protected]')
153162
opt_branch = git_config_get('githubmerge.branch',None)
154163
testcmd = git_config_get('githubmerge.testcmd')
164+
ghtoken = git_config_get('user.ghtoken')
155165
signingkey = git_config_get('user.signingkey')
156166
if repo is None:
157167
print("ERROR: No repository configured. Use this command to set:", file=stderr)
@@ -162,14 +172,17 @@ def main():
162172
print("git config --global user.signingkey <key>",file=stderr)
163173
sys.exit(1)
164174

165-
host_repo = host+":"+repo # shortcut for push/pull target
175+
if host.startswith(('https:','http:')):
176+
host_repo = host+"/"+repo+".git"
177+
else:
178+
host_repo = host+":"+repo
166179

167180
# Extract settings from command line
168181
args = parse_arguments()
169182
pull = str(args.pull[0])
170183

171184
# Receive pull information from github
172-
info = retrieve_pr_info(repo,pull)
185+
info = retrieve_pr_info(repo,pull,ghtoken)
173186
if info is None:
174187
sys.exit(1)
175188
title = info['title'].strip()

0 commit comments

Comments
 (0)