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 \ shellcheck \
yamllint \ yamllint \
locales \ 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 \ && apt-get clean \
&& rm -rf /root/.cache/pip && rm -rf /root/.cache/pip

View File

@ -51,10 +51,8 @@ def get_gh_api(
sleep: int = 3, sleep: int = 3,
**kwargs: Any, **kwargs: Any,
) -> requests.Response: ) -> 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 get gh api with retries and failover to another token if ratelimit is exceeded
"403 rate limit exceeded" error
It sets auth automatically when ROBOT_TOKEN is already set by get_best_robot_token
""" """
def set_auth_header(): def set_auth_header():
@ -71,12 +69,10 @@ def get_gh_api(
if grt.ROBOT_TOKEN is not None: if grt.ROBOT_TOKEN is not None:
set_auth_header() set_auth_header()
header_is_set = "Authorization" in kwargs.get("headers", {}) token_is_set = "Authorization" in kwargs.get("headers", {})
retry = 1 exc = Exception("A placeholder to satisfy typing and avoid nesting")
exc = Exception("placeholder") for i in range(retries):
while retry <= retries:
try: try:
retry += 1
response = requests.get(url, **kwargs) response = requests.get(url, **kwargs)
response.raise_for_status() response.raise_for_status()
return response return response
@ -84,21 +80,20 @@ def get_gh_api(
exc = e exc = e
if ( if (
e.response.status_code == 403 e.response.status_code == 403
and b"rate limit exceeded" and b"rate limit exceeded" in e.response._content # pylint:disable=protected-access
in e.response._content # pylint:disable=protected-access and token_is_set
and not header_is_set
): ):
logging.warning( 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() set_auth_header()
retry = 1 continue
elif retry < retries:
time.sleep(sleep)
except Exception as e: except Exception as e:
exc = 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 raise exc

View File

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