Skip to content

Commit

Permalink
contrib: Allow use of github API authentication in github-merge
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
laanwj committed Jan 14, 2019
1 parent a4c5bbf commit f1bd219
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
20 changes: 19 additions & 1 deletion contrib/devtools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,25 @@ Configuring the github-merge tool for the bitcoin repository is done in the foll

git config githubmerge.repository bitcoin/bitcoin
git config githubmerge.testcmd "make -j4 check" (adapt to whatever you want to use for testing)
git config --global user.signingkey mykeyid (if you want to GPG sign)
git config --global user.signingkey mykeyid

Authentication (optional)
--------------------------

The API request limit for unauthenticated requests is quite low, but the
limit for authenticated requests is much higher. If you start running
into rate limiting errors it can be useful to set an authentication token
so that the script can authenticate requests.

- First, go to [Personal access tokens](https://github.com/settings/tokens).
- Click 'Generate new token'.
- Fill in an arbitrary token description. No further privileges are needed.
- Click the `Generate token` button at the bottom of the form.
- Copy the generated token (should be a hexadecimal string)

Then do:

git config --global user.ghtoken "pasted token"

Create and verify timestamps of merge commits
---------------------------------------------
Expand Down
8 changes: 6 additions & 2 deletions contrib/devtools/github-merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ def git_config_get(option, default=None):
except subprocess.CalledProcessError:
return default

def retrieve_pr_info(repo,pull):
def retrieve_pr_info(repo,pull,ghtoken):
'''
Retrieve pull request information from github.
Return None if no title can be found, or an error happens.
'''
try:
req = Request("https://api.github.com/repos/"+repo+"/pulls/"+pull)
if ghtoken is not None:
req.add_header('Authorization', 'token ' + ghtoken)
result = urlopen(req)
reader = codecs.getreader('utf-8')
obj = json.load(reader(result))
Expand Down Expand Up @@ -140,6 +142,7 @@ def parse_arguments():
In addition, you can set the following git configuration variables:
githubmerge.repository (mandatory),
user.signingkey (mandatory),
user.ghtoken (default: none).
githubmerge.host (default: git@github.com),
githubmerge.branch (no default),
githubmerge.testcmd (default: none).
Expand All @@ -158,6 +161,7 @@ def main():
host = git_config_get('githubmerge.host','git@github.com')
opt_branch = git_config_get('githubmerge.branch',None)
testcmd = git_config_get('githubmerge.testcmd')
ghtoken = git_config_get('user.ghtoken')
signingkey = git_config_get('user.signingkey')
if repo is None:
print("ERROR: No repository configured. Use this command to set:", file=stderr)
Expand All @@ -178,7 +182,7 @@ def main():
pull = str(args.pull[0])

# Receive pull information from github
info = retrieve_pr_info(repo,pull)
info = retrieve_pr_info(repo,pull,ghtoken)
if info is None:
sys.exit(1)
title = info['title'].strip()
Expand Down

0 comments on commit f1bd219

Please sign in to comment.