Skip to content

Commit

Permalink
Handle the GitHub rate limit in a better way (#374)
Browse files Browse the repository at this point in the history
Let's use the `X-RateLimit-Reset` header provided by GitHub and
sleep until the suggested time to complete the report instead of
bailing out in the middle.
  • Loading branch information
psss authored Oct 4, 2024
1 parent c068154 commit 1155927
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions did/plugins/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import json
import re
import time

import requests

Expand Down Expand Up @@ -77,16 +78,21 @@ def search(self, query):
"Defined token is not valid. "
"Either update it or remove it.")

# Handle the exceeded rate limit
if response.status_code in [403, 429]:
if response.headers.get("X-RateLimit-Remaining") == "0":
reset_time = int(response.headers["X-RateLimit-Reset"])
sleep_time = int(max(reset_time - time.time(), 0)) + 1
log.warning("GitHub rate limit exceeded, use token to speed up.")
log.warning(f"Sleeping now for {listed(sleep_time, 'second')}.")
time.sleep(sleep_time)
continue
raise ReportError(f"GitHub query failed: {response.text}")

# Parse fetched json data
try:
data = json.loads(response.text)["items"]
result.extend(data)
except KeyError:
if json.loads(response.text)["message"].startswith(
"API rate limit exceeded"):
raise ReportError(
"GitHub API rate limit exceeded. "
"Consider creating an access token.")
except requests.exceptions.JSONDecodeError as error:
log.debug(error)
raise ReportError(f"GitHub JSON failed: {response.text}.")
Expand Down

1 comment on commit 1155927

@packit-as-a-service
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on your Packit configuration the settings of the psss/did Copr project would need to be updated as follows:

field old value new value
bootstrap on default

Packit was unable to update the settings above as it is missing admin permissions on the psss/did Copr project.

To fix this you can do one of the following:

  • Grant Packit admin permissions on the psss/did Copr project on the permissions page.
  • Change the above Copr project settings manually on the settings page to match the Packit configuration.
  • Update the Packit configuration to match the Copr project settings.

Please retrigger the build, once the issue above is fixed.

Please sign in to comment.