gh api request func update

This commit is contained in:
Max Kainov 2023-10-11 16:00:48 +00:00
parent c5ebd6f072
commit 3f3fcce7e5
3 changed files with 19 additions and 23 deletions

View File

@ -19,7 +19,7 @@ RUN apt-get update && env DEBIAN_FRONTEND=noninteractive apt-get install --yes \
shellcheck \
yamllint \
locales \
&& pip3 install black==23.1.0 boto3 codespell==2.2.1 mypy==1.3.0 PyGithub unidiff pylint==2.6.2 \
&& pip3 install black==23.1.0 boto3 codespell==2.2.1 mypy==1.3.0 PyGithub pylint==2.6.2 \
&& apt-get clean \
&& rm -rf /root/.cache/pip

View File

@ -51,10 +51,8 @@ def get_gh_api(
sleep: int = 3,
**kwargs: Any,
) -> requests.Response:
"""It's a wrapper around get_with_retries that requests GH api w/o auth by
default, and falls back to the get_best_robot_token in case of receiving
"403 rate limit exceeded" error
It sets auth automatically when ROBOT_TOKEN is already set by get_best_robot_token
"""
get gh api with retries and failover to another token if ratelimit is exceeded
"""
def set_auth_header():
@ -71,12 +69,10 @@ def get_gh_api(
if grt.ROBOT_TOKEN is not None:
set_auth_header()
header_is_set = "Authorization" in kwargs.get("headers", {})
retry = 1
exc = Exception("placeholder")
while retry <= retries:
token_is_set = "Authorization" in kwargs.get("headers", {})
exc = Exception("A placeholder to satisfy typing and avoid nesting")
for i in range(retries):
try:
retry += 1
response = requests.get(url, **kwargs)
response.raise_for_status()
return response
@ -84,21 +80,20 @@ def get_gh_api(
exc = e
if (
e.response.status_code == 403
and b"rate limit exceeded"
in e.response._content # pylint:disable=protected-access
and not header_is_set
and b"rate limit exceeded" in e.response._content # pylint:disable=protected-access
and token_is_set
):
logging.warning(
"Received rate limit exception, setting the auth header and retry"
"Received rate limit exception, re-setting the auth header and retry"
)
set_auth_header()
retry = 1
elif retry < retries:
time.sleep(sleep)
continue
except Exception as e:
exc = e
if retry < retries:
time.sleep(sleep)
if i + 1 < retries:
logging.info("Exception '%s' while getting, retry %i", exc, i + 1)
time.sleep(sleep)
raise exc

View File

@ -169,10 +169,11 @@ class PRInfo:
response_json = user_orgs_response.json()
self.user_orgs = set(org["id"] for org in response_json)
self.diff_urls.append(
f"https://api.github.com/repos/{GITHUB_REPOSITORY}/"
f"compare/master...{self.head_ref}"
)
self.diff_urls.append(
f"https://api.github.com/repos/{GITHUB_REPOSITORY}/"
f"compare/master...{self.head_ref}"
)
elif "commits" in github_event:
# `head_commit` always comes with `commits`
commit_message = github_event["head_commit"]["message"] # type: str