Skip to content

Commit

Permalink
Implement retry logic for API calls that return no head_totals (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored May 20, 2024
1 parent 61ef4bb commit 58a9d3e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
10 changes: 8 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ repos:
- prettier-plugin-toml
- prettier-plugin-sort-json
- repo: https://github.com/python-jsonschema/check-jsonschema
rev: 0.28.2
rev: 0.28.3
hooks:
- id: check-github-workflows
- repo: https://github.com/pre-commit/pre-commit-hooks.git
Expand Down Expand Up @@ -50,7 +50,7 @@ repos:
types: [file, yaml]
entry: yamllint --strict
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.4.3"
rev: "v0.4.4"
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
Expand All @@ -65,3 +65,9 @@ repos:
- id: mypy
# empty args needed in order to match mypy cli behavior
args: [--strict]
- repo: https://github.com/pycqa/pylint
rev: v3.2.2
hooks:
- id: pylint
args:
- --output-format=colorized
37 changes: 24 additions & 13 deletions codecov-status.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import sys

import time
from urllib.request import urlopen


Expand All @@ -26,19 +27,29 @@
)
sys.exit(0)

url = f"https://api.codecov.io/api/v2/github/{org}/repos/{repo}/pulls/{pr}/"
print(f"Getting codecov.io status from {url}")
with urlopen(url) as response:
response_content = response.read().decode("utf-8")
data = json.loads(response_content)
print(data, file=sys.stdout)
base_cov = 0.0
head_cov = 0.0
if data["base_totals"] is not None:
base_cov = data["base_totals"]["coverage"]
if data["head_totals"] is not None:
head_cov = data["head_totals"]["coverage"]
delta_coverage = head_cov - base_cov
retries = 0
while retries < 5:
url = f"https://api.codecov.io/api/v2/github/{org}/repos/{repo}/pulls/{pr}/"
print(f"Getting codecov.io status from {url}")
with urlopen(url) as response:
response_content = response.read().decode("utf-8")
data = json.loads(response_content)
print(data, file=sys.stdout)
base_cov = 0.0
head_cov = 0.0
if data["base_totals"] is not None:
base_cov = data["base_totals"]["coverage"]
if data["head_totals"] is not None:
head_cov = data["head_totals"]["coverage"]
delta_coverage = head_cov - base_cov
if head_cov != 0.0:
break
retries += 1
delay = retries * 30
print(
f"::notice::Codecov API returned no head_totals, we will retry the request in {delay} seconds."
)
time.sleep(delay)

msg = f"{abs(delta_coverage):.2f}% ({base_cov:.2f}% on base -> {head_cov:.2f}% on head).\n"
msg += f"See https://app.codecov.io/gh/{org}/{repo}/pull/{pr} for details."
Expand Down
8 changes: 8 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[tool.pylint."MESSAGES CONTROL"]
# increase from default is 50 which is too aggressive
max-statements = 60
disable = [
# Disabled on purpose:
"line-too-long", # covered by black
"invalid-name",
]

0 comments on commit 58a9d3e

Please sign in to comment.