Skip to content

Commit

Permalink
fix(get-modflow/ci): use GITHUB_TOKEN to side-step ratelimit (#1473)
Browse files Browse the repository at this point in the history
Co-authored-by: w-bonelli <wesbonelli@gmail.com>
  • Loading branch information
mwtoews and wpbonelli committed Jul 29, 2022
1 parent 4f86fcf commit eb59e10
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci-windows.yml
Expand Up @@ -104,6 +104,8 @@ jobs:
working-directory: ./autotest
run: |
pytest -v ci_prepare.py
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

# - name: Add executables directory to path on Windows (bash)
# run: |
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Expand Up @@ -163,6 +163,8 @@ jobs:
working-directory: ./autotest
run: |
pytest -v ci_prepare.py
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Add executables directory to path on Linux and MacOS
run: |
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/rtd.yml
Expand Up @@ -54,6 +54,8 @@ jobs:
mkdir -p $HOME/.local/bin
get-modflow $HOME/.local/bin
echo "$HOME/.local/bin" >> $GITHUB_PATH
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Run jupytext on tutorials
working-directory: ./.docs
Expand Down
34 changes: 30 additions & 4 deletions flopy/utils/get_modflow.py
Expand Up @@ -40,9 +40,21 @@ def get_ostag():
raise ValueError(f"platform {sys.platform!r} not supported")


def get_request(url):
"""Get urllib.request.Request, with headers.
This bears GITHUB_TOKEN if it is set as an environment variable.
"""
headers = {}
github_token = os.environ.get("GITHUB_TOKEN")
if github_token:
headers["Authorization"] = f"Bearer {github_token}"
return urllib.request.Request(url, headers=headers)


def get_avail_releases(api_url):
"""Get list of available releases."""
with urllib.request.urlopen(f"{api_url}/releases") as resp:
with urllib.request.urlopen(get_request(f"{api_url}/releases")) as resp:
result = resp.read()
releases = json.loads(result.decode())
avail_releases = ["latest"]
Expand Down Expand Up @@ -179,15 +191,29 @@ def run_main(
else:
req_url = f"{api_url}/releases/tags/{release_id}"
try:
with urllib.request.urlopen(req_url) as resp:
with urllib.request.urlopen(get_request(req_url)) as resp:
result = resp.read()
remaining = int(resp.headers["x-ratelimit-remaining"])
if remaining <= 10:
print(
f"Only {remaining} GitHub API requests remaining "
"before rate-limiting"
)
except urllib.error.HTTPError as err:
if err.code == 404:
if err.code == 401 and os.environ.get("GITHUB_TOKEN"):
raise ValueError(
"environment variable GITHUB_TOKEN is invalid"
) from err
if err.code == 403 and "rate limit exceeded" in err.reason:
raise ValueError(
"use environment variable GITHUB_TOKEN to bypass rate limit"
) from err
elif err.code == 404:
avail_releases = get_avail_releases(api_url)
raise ValueError(
f"Release {release_id!r} not found -- "
f"choose from {avail_releases}"
)
) from err
else:
raise err
release = json.loads(result.decode())
Expand Down

0 comments on commit eb59e10

Please sign in to comment.