Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(ci): recover jest shard identity in flat junit downloads (#74003)

Raúl Negrón-Oterob5ffd5a3827e
diff --git a/.github/scripts/report_test_timings.py b/.github/scripts/report_test_timings.py
index e8fdd94f1aca..336ec498f235 100644
--- a/.github/scripts/report_test_timings.py
+++ b/.github/scripts/report_test_timings.py
@@ -106,6 +106,10 @@ class ArtifactInfo:
# Which workflow run attempt uploaded the artifact (from the `-attempt<N>` name suffix); 1
# when unsuffixed, i.e. every artifact predating re-run-aware uploads.
attempt: int = 1
+ # True when the JUnit files sat directly in the download root (download-artifact extracts a
+ # single matching artifact without a per-artifact subdirectory), so suite/segment/group came
+ # from the meaningless download dir name and should be recovered from the JUnit filename.
+ flat: bool = False
@dataclass(frozen=True)
@@ -166,7 +170,10 @@ def derive_suite_segment_and_group(artifact_dir_name: str) -> tuple[str, str, in
def collect_artifact_infos(artifacts_root: Path) -> list[ArtifactInfo]:
- artifact_dirs = sorted(d for d in artifacts_root.iterdir() if d.is_dir()) or [artifacts_root]
+ artifact_dirs = sorted(d for d in artifacts_root.iterdir() if d.is_dir())
+ flat = not artifact_dirs
+ if flat:
+ artifact_dirs = [artifacts_root]
parsed: list[tuple[Path, str, str, int | None, int]] = []
for d in artifact_dirs:
base_name, attempt = split_attempt_suffix(d.name)
@@ -184,7 +191,13 @@ def collect_artifact_infos(artifacts_root: Path) -> list[ArtifactInfo]:
return [
ArtifactInfo(
- path=d, suite=suite, segment=segment, group=group, total=shard_totals.get((suite, segment)), attempt=attempt
+ path=d,
+ suite=suite,
+ segment=segment,
+ group=group,
+ total=shard_totals.get((suite, segment)),
+ attempt=attempt,
+ flat=flat,
)
for d, suite, segment, group, attempt in parsed
]
@@ -300,6 +313,32 @@ def product_shard_info(info: ArtifactInfo, junit_filename: str) -> ArtifactInfo:
return replace(info, suite="product", segment=product, total=None)
+_FLAT_JEST_JUNIT_NAME = re.compile(r"^junit-(?P<segment>.+)-(?P<chunk>\d+)\.xml$")
+
+
+def flat_shard_info(info: ArtifactInfo, junit_filename: str, runner: Runner) -> ArtifactInfo:
+ """Recover Jest shard identity from the JUnit filename when the download landed flat.
+
+ download-artifact extracts a single matching artifact directly into the download path on any
+ workflow attempt, so the artifact name cannot provide its job identity or attempt. The JUnit
+ filename supplies the Jest job identity, and the workflow context supplies the attempt. Recover
+ both so re-run filtering and recovery joins use the same identity as subdirectory downloads.
+ """
+ if not info.flat or runner != "jest":
+ return info
+ match = _FLAT_JEST_JUNIT_NAME.match(junit_filename)
+ if match is None:
+ return info
+ return replace(
+ info,
+ suite="frontend",
+ segment=match.group("segment"),
+ group=int(match.group("chunk")),
+ total=None,
+ attempt=current_run_attempt(),
+ )
+
+
def parse_iso_utc(value: str) -> datetime | None:
"""Parse an ISO 8601 timestamp (pytest emits naive); treat as UTC."""
if not value:
@@ -433,7 +472,7 @@ def parse_shard(
wall_seconds = (end - start).total_seconds()
testcase_seconds = sum(t.duration_seconds for t in tests)
return Shard(
- info=product_shard_info(info, xml_path.name),
+ info=flat_shard_info(product_shard_info(info, xml_path.name), xml_path.name, runner),
junit_filename=xml_path.name,
start=start,
end=end,
diff --git a/.github/scripts/test_report_test_timings.py b/.github/scripts/test_report_test_timings.py
index de282f876890..dc73a00c4232 100644
--- a/.github/scripts/test_report_test_timings.py
+++ b/.github/scripts/test_report_test_timings.py
@@ -249,6 +249,45 @@ def test_collect_jest_shard_marks_tolerated_failures_as_quarantined(tmp_path: Pa
assert report_test_timings.should_emit(shard.tests[0], float("inf")) is True
+ "runner,filename,run_attempt,recovered,expected_attempt",
+ [
+ # download-artifact extracts a single matching artifact flat into the download root, so
+ # identity must come from the filename and current workflow attempt.
+ ("jest", "junit-EE-1.xml", 2, ("frontend", "EE", 1), 2),
+ # A filename without a `-<chunk>` suffix and non-jest runners keep the directory-derived
+ # fallback identity.
+ ("jest", "junit-report.xml", 2, None, 1),
+ ("pytest", "junit-EE-1.xml", 2, None, 1),
+ ],
+)
+def test_flat_download_shard_identity(
+ runner: str,
+ filename: str,
+ run_attempt: int,
+ recovered: tuple[str, str, int] | None,
+ expected_attempt: int,
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ _write_shard_xml(
+ tmp_path,
+ filename=filename,
+ timestamp="2026-05-04T10:00:00",
+ time="1.0",
+ body='<testcase classname="suite flaky" name="suite flaky" time="0.1" file="src/flaky.test.ts"><failure message="x"/></testcase>',
+ )
+ monkeypatch.setenv("GITHUB_RUN_ATTEMPT", str(run_attempt))
+
+ shard = report_test_timings.collect_shards(tmp_path, runner)[0]
+
+ expected = recovered or report_test_timings.derive_suite_segment_and_group(tmp_path.name)
+ assert (shard.info.suite, shard.info.segment, shard.info.group) == expected
+ assert shard.info.attempt == expected_attempt
+ if recovered:
+ assert report_test_timings.job_trace_key(shard.info) == "frontend:EE:1"
+
+
def test_load_jest_quarantine_signals_rejects_oversized_artifacts(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None: