Skip to content

Commit 5cfafda

Browse files
committed
Find the latest ingested coverage revision from the gcp bucket directly
Fixes #2976
1 parent b213b39 commit 5cfafda

File tree

4 files changed

+61
-12
lines changed

4 files changed

+61
-12
lines changed

bot/code_coverage_bot/hgmo.py

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import requests
77
import structlog
88

9+
from typing import Iterable
10+
11+
912
logger = structlog.get_logger(__name__)
1013

1114

@@ -56,10 +59,14 @@ def __exit__(self, type, value, traceback):
5659
logger.info("hgmo has been killed")
5760

5861
def get_pushes(
59-
self, startID=None, startDate=None, changeset=None, full=True, tipsonly=False
62+
self,
63+
startID=None,
64+
endID=None,
65+
startDate=None,
66+
changeset=None,
67+
full=True,
68+
tipsonly=False,
6069
):
61-
assert startID is not None or startDate is not None or changeset is not None
62-
6370
params = {"version": 2}
6471

6572
if full:
@@ -71,6 +78,9 @@ def get_pushes(
7178
if startID is not None:
7279
params["startID"] = startID
7380

81+
if endID is not None:
82+
params["endID"] = endID
83+
7484
if startDate is not None:
7585
params["startdate"] = startDate
7686

@@ -96,3 +106,33 @@ def get_automation_relevance_changesets(self, changeset):
96106
)
97107
r.raise_for_status()
98108
return r.json()["changesets"]
109+
110+
111+
def iter_pushes(
112+
server_address: str,
113+
) -> Iterable[tuple[int, dict]]:
114+
"""Yield pushes from newest to oldest push-id."""
115+
start_id = None
116+
end_id = None
117+
118+
with HGMO(server_address=server_address) as hgmo_server:
119+
while True:
120+
data = hgmo_server.get_pushes(
121+
startID=start_id, endID=end_id, full=False, tipsonly=True
122+
)
123+
pushes = data.get("pushes", {})
124+
125+
if not pushes:
126+
return
127+
128+
push_ids = sorted((int(push_id) for push_id in pushes.keys()), reverse=True)
129+
for push_id in push_ids:
130+
yield push_id, pushes[str(push_id)]
131+
132+
oldest_seen = push_ids[-1]
133+
if oldest_seen <= 1:
134+
return
135+
136+
# json-pushes treats startID as exclusive and endID as inclusive.
137+
end_id = oldest_seen - 1
138+
start_id = max(1, end_id - len(pushes))

bot/code_coverage_bot/hooks/cron.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class CronHook(Hook):
2424
def __init__(self, *args, **kwargs):
2525
# Retrieve latest ingested revision
2626
try:
27-
revision = uploader.gcp_latest("mozilla-central")[0]["revision"]
27+
revision = uploader.gcp_latest(config.MOZILLA_CENTRAL_REPOSITORY)
2828
except Exception as e:
2929
logger.warn("Failed to retrieve the latest reports ingested: {}".format(e))
3030
raise

bot/code_coverage_bot/hooks/crontrigger.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class CronTriggerHook(Hook):
2323
def __init__(self, *args, **kwargs):
2424
# Retrieve latest ingested revision
2525
try:
26-
revision = uploader.gcp_latest("mozilla-central")[0]["revision"]
26+
revision = uploader.gcp_latest(config.MOZILLA_CENTRAL_REPOSITORY)
2727
except Exception as e:
2828
logger.warn("Failed to retrieve the latest reports ingested: {}".format(e))
2929
raise

bot/code_coverage_bot/uploader.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
import itertools
33
import os.path
44

5-
import requests
65
import structlog
76
import zstandard as zstd
87
from google.cloud.storage.bucket import Bucket
98

109
from code_coverage_bot.secrets import secrets
1110
from code_coverage_bot.gcp import get_bucket
11+
from code_coverage_bot import hgmo
1212

1313
logger = structlog.get_logger(__name__)
1414
GCP_COVDIR_PATH = "{repository}/{revision}/{platform}:{suite}.json.zstd"
@@ -60,15 +60,24 @@ def gcp_covdir_exists(
6060
return blob.exists()
6161

6262

63-
def gcp_latest(repository):
63+
def gcp_latest(repo_url):
6464
"""
6565
List the latest reports ingested on the backend
6666
"""
67-
params = {"repository": repository}
68-
backend_host = secrets[secrets.BACKEND_HOST]
69-
resp = requests.get("{}/v2/latest".format(backend_host), params=params)
70-
resp.raise_for_status()
71-
return resp.json()
67+
assert (
68+
secrets[secrets.GOOGLE_CLOUD_STORAGE] is not None
69+
), "Missing GOOGLE_CLOUD_STORAGE secret"
70+
bucket = get_bucket(secrets[secrets.GOOGLE_CLOUD_STORAGE])
71+
72+
for push_id, push_data in hgmo.iter_pushes(server_address=repo_url):
73+
changesets: list[str] = push_data.get("changesets", [])
74+
if not changesets:
75+
continue
76+
77+
if gcp_covdir_exists(bucket, "mozilla-central", changesets[-1], "all", "all"):
78+
return changesets[-1]
79+
80+
return None
7281

7382

7483
def covdir_paths(report):

0 commit comments

Comments
 (0)