Skip to content

Commit

Permalink
Use GitHub token file to avoid rate limiting
Browse files Browse the repository at this point in the history
Closes #18.
  • Loading branch information
homebysix committed Feb 12, 2019
1 parent 4957585 commit c22fdba
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/).

## [1.1.1] - Unreleased

### Added
- Recipe Robot now uses the AutoPkg GitHub token file at ~/.autopkg_gh_token, if it exists. This can help you avoid rate limiting if you're creating many GitHub recipes in a short time. (#18)


## [1.1.0] - 2019-02-12

Expand Down Expand Up @@ -185,6 +190,7 @@ All notable changes to this project will be documented in this file. This projec

- Initial public release of Recipe Robot (beta).

[1.1.1]: https://github.com/homebysix/recipe-robot/compare/v1.1.0...HEAD
[1.1.0]: https://github.com/homebysix/recipe-robot/compare/v1.0.5...v1.1.0
[1.0.5]: https://github.com/homebysix/recipe-robot/compare/v1.0.4...v1.0.5
[1.0.4]: https://github.com/homebysix/recipe-robot/compare/v1.0.3...v1.0.4
Expand Down
42 changes: 30 additions & 12 deletions scripts/recipe_robot_lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,16 @@ def inspect_download_url(input_path, args, facts):
return facts


def github_urlopen(url, github_token):
'''For a given URL, return the urlopen response after adding the GitHub token.'''

req = Request(url)
if github_token:
req.add_header('Authorization', 'token ' + github_token)

return urlopen(req)


def inspect_github_url(input_path, args, facts):
"""Process a GitHub URL
Expand All @@ -1124,16 +1134,27 @@ def inspect_github_url(input_path, args, facts):
else:
facts["inspections"].append("github_url")

# Use AutoPkg GitHub token, if file exists.
# TODO: Also check for GITHUB_TOKEN preference.
github_token_file = os.path.expanduser('~/.autopkg_gh_token')
github_token = None
if os.path.isfile(github_token_file):
try:
with open(github_token_file, 'r') as tokenfile:
github_token = tokenfile.read().strip()
except IOError as err:
facts['warnings'].append('Couldn\'t read GitHub token file at '
'{}.'.format(github_token_file))

# Grab the GitHub repo path.
github_repo = ""
robo_print("Getting GitHub repo...", LogLevel.VERBOSE)
# Matches all these examples:
# [x] https://github.com/jbtule/cdto/releases/download/2_6_0/cdto_2_6.zip
# [x] https://github.com/lindegroup/autopkgr
# [x] https://raw.githubusercontent.com/macmule/AutoCasperNBI/master/README.md
# [X] https://api.github.com/repos/macmule/AutoCasperNBI
# [X] https://hjuutilainen.github.io/munkiadmin/
github_repo = ""
# - https://github.com/jbtule/cdto/releases/download/2_6_0/cdto_2_6.zip
# - https://github.com/lindegroup/autopkgr
# - https://raw.githubusercontent.com/macmule/AutoCasperNBI/master/README.md
# - https://api.github.com/repos/macmule/AutoCasperNBI
# - https://hjuutilainen.github.io/munkiadmin/
parsed_url = urlparse(input_path)
path = parsed_url.path.split("/")
path.remove("")
Expand Down Expand Up @@ -1161,9 +1182,6 @@ def inspect_github_url(input_path, args, facts):
"Try using the --ignore-existing flag if you want me to "
"create recipes for myself.")

# TODO(Elliot): How can we use GitHub tokens to prevent rate
# limiting? (#18)

# Use GitHub API to obtain information about the repo and
# releases.
repo_api_url = "https://api.github.com/repos/%s" % github_repo
Expand All @@ -1172,9 +1190,9 @@ def inspect_github_url(input_path, args, facts):

# Download the information from the GitHub API.
try:
raw_json_repo = urlopen(repo_api_url).read()
raw_json_release = urlopen(releases_api_url).read()
raw_json_user = urlopen(user_api_url).read()
raw_json_repo = github_urlopen(repo_api_url, github_token).read()
raw_json_release = github_urlopen(releases_api_url, github_token).read()
raw_json_user = github_urlopen(user_api_url, github_token).read()
except HTTPError as err:
if err.code == 403:
facts["warnings"].append(
Expand Down

0 comments on commit c22fdba

Please sign in to comment.