Skip to content

Commit f1bd219

Browse files
committed
contrib: 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.
1 parent a4c5bbf commit f1bd219

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
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: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,15 @@ def git_config_get(option, default=None):
4747
except subprocess.CalledProcessError:
4848
return default
4949

50-
def retrieve_pr_info(repo,pull):
50+
def retrieve_pr_info(repo,pull,ghtoken):
5151
'''
5252
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)
57+
if ghtoken is not None:
58+
req.add_header('Authorization', 'token ' + ghtoken)
5759
result = urlopen(req)
5860
reader = codecs.getreader('utf-8')
5961
obj = json.load(reader(result))
@@ -140,6 +142,7 @@ def parse_arguments():
140142
In addition, you can set the following git configuration variables:
141143
githubmerge.repository (mandatory),
142144
user.signingkey (mandatory),
145+
user.ghtoken (default: none).
143146
githubmerge.host (default: [email protected]),
144147
githubmerge.branch (no default),
145148
githubmerge.testcmd (default: none).
@@ -158,6 +161,7 @@ def main():
158161
host = git_config_get('githubmerge.host','[email protected]')
159162
opt_branch = git_config_get('githubmerge.branch',None)
160163
testcmd = git_config_get('githubmerge.testcmd')
164+
ghtoken = git_config_get('user.ghtoken')
161165
signingkey = git_config_get('user.signingkey')
162166
if repo is None:
163167
print("ERROR: No repository configured. Use this command to set:", file=stderr)
@@ -178,7 +182,7 @@ def main():
178182
pull = str(args.pull[0])
179183

180184
# Receive pull information from github
181-
info = retrieve_pr_info(repo,pull)
185+
info = retrieve_pr_info(repo,pull,ghtoken)
182186
if info is None:
183187
sys.exit(1)
184188
title = info['title'].strip()

0 commit comments

Comments
 (0)