Skip to content

Commit

Permalink
Merge pull request #1579 from camptocamp/backport/1576-to-1.3
Browse files Browse the repository at this point in the history
[Backport 1.3] Print rate limit and fix it
  • Loading branch information
sbrunner authored Mar 6, 2024
2 parents 7b39f86 + 2ef1a74 commit 5fee62c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
19 changes: 17 additions & 2 deletions c2cciutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,19 @@ def add_authorization_header(headers: Dict[str, str]) -> Dict[str, str]:
return headers


def check_response(response: requests.Response, raise_for_status: bool = True) -> Any:
"""
Check the response and raise an exception if it's not ok.
Also print the X-Ratelimit- headers to get information about the rate limiting.
"""
for header in response.headers:
if header.lower().startswith("x-ratelimit-"):
print(f"{header}: {response.headers[header]}")
if raise_for_status:
response.raise_for_status()


def graphql(query_file: str, variables: Dict[str, Any], default: Any = None) -> Any:
"""
Get a graphql result from GitHub.
Expand Down Expand Up @@ -554,9 +567,11 @@ def graphql(query_file: str, variables: Dict[str, Any], default: Any = None) ->
),
timeout=int(os.environ.get("C2CCIUTILS_TIMEOUT", "30")),
)
if http_response.status_code == 401 and default is not None:
if http_response.status_code in (401, 403) and default is not None:
print(f"::warning::GraphQL error: {http_response.status_code}, use default value")
check_response(http_response, False)
return default
http_response.raise_for_status()
check_response(http_response)
json_response = http_response.json()

if "errors" in json_response:
Expand Down
2 changes: 1 addition & 1 deletion c2cciutils/pr_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def add_issue_link(github_event: Dict[str, Any], **kwargs: Any) -> bool:
timeout=int(os.environ.get("C2CCIUTILS_TIMEOUT", "30")),
headers=c2cciutils.add_authorization_header({}),
)
comments_response.raise_for_status()
c2cciutils.check_response(comments_response)
comments = comments_response.json()

for comment in comments:
Expand Down
2 changes: 1 addition & 1 deletion c2cciutils/scripts/pr_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def main() -> None:
timeout=int(os.environ.get("C2CCIUTILS_TIMEOUT", "30")),
headers=c2cciutils.add_authorization_header({}),
)
commits_response.raise_for_status()
c2cciutils.check_response(commits_response)
commits = commits_response.json()

full_config = c2cciutils.get_config()
Expand Down
1 change: 1 addition & 0 deletions c2cciutils/scripts/publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ def main() -> None:
headers=c2cciutils.add_authorization_header({}),
timeout=int(os.environ.get("C2CCIUTILS_TIMEOUT", "30")),
)
c2cciutils.check_response(security_response, False)
if (
security_response.ok
and docker_config.get("latest", c2cciutils.configuration.PUBLISH_DOCKER_LATEST_DEFAULT) is True
Expand Down

0 comments on commit 5fee62c

Please sign in to comment.