-
Notifications
You must be signed in to change notification settings - Fork 0
/
codecov-status.py
executable file
·73 lines (66 loc) · 2.62 KB
/
codecov-status.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python3
"""Replacement for CodeCov GitHub App."""
import datetime
import json
import os
import sys
import time
from urllib.request import urlopen
DELAY = 5
MAX_RETRIES = 12
# 5s, 10s, 15s, 20s,...
MIN_AGE_IN_SECONDS = 10
if __name__ == "__main__":
start_time = datetime.datetime.now(tz=datetime.UTC) - datetime.timedelta(seconds=2)
if os.environ.get("GITHUB_REPOSITORY", "") and os.environ.get(
"GITHUB_REPOSITORY", ""
):
org, repo = os.environ.get("GITHUB_REPOSITORY", "").split("/")
pr = os.environ.get("GITHUB_REF_NAME", "").split("/")[0]
else:
print(
"::warning::GITHUB_REPOSITORY and GITHUB_REF_NAME are needed to determine current pull request."
)
sys.exit(0)
if not pr.isnumeric():
print(
"::warning::Codecov.io status check skipped because a pull request was not detected."
)
sys.exit(0)
retries = 0
sleep = DELAY
while retries < MAX_RETRIES:
time.sleep(sleep)
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
updatestamp = start_time
if data["updatestamp"]: # can be None or a string
updatestamp = datetime.datetime.fromisoformat(data["updatestamp"])
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
# It can happen for updatestamp to be updated but head_totals to become
# None for some time, so we keep retrying until we have something.
if updatestamp > start_time and head_cov:
break
retries += 1
sleep = DELAY * (retries + 1)
print(
f"Codecov API returned previous stats, we will retry the request in {sleep} seconds."
)
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."
if delta_coverage < 0:
print(
f"::error::Setting coverage check as failed due to coverage going down by {msg}"
)
sys.exit(2)
print(f"Setting coverage check as passed due to coverage going up by {msg}")