1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-18 17:53:47 +01:00

[ci] Group all PR builds, isolate direct changes for full validation on dev (#11193)

This commit is contained in:
J. Nick Koston
2025-10-14 19:49:14 -10:00
committed by GitHub
parent 66263b40e1
commit 1b0ca3360e
6 changed files with 230 additions and 78 deletions

View File

@@ -31,6 +31,7 @@ Options:
from __future__ import annotations
import argparse
from functools import cache
import json
import os
from pathlib import Path
@@ -45,7 +46,6 @@ from helpers import (
changed_files,
get_all_dependencies,
get_components_from_integration_fixtures,
parse_list_components_output,
root_path,
)
@@ -212,6 +212,24 @@ def _any_changed_file_endswith(branch: str | None, extensions: tuple[str, ...])
return any(file.endswith(extensions) for file in changed_files(branch))
@cache
def _component_has_tests(component: str) -> bool:
"""Check if a component has test files.
Cached to avoid repeated filesystem operations for the same component.
Args:
component: Component name to check
Returns:
True if the component has test YAML files
"""
tests_dir = Path(root_path) / "tests" / "components" / component
if not tests_dir.exists():
return False
return any(tests_dir.glob("test.*.yaml"))
def main() -> None:
"""Main function that determines which CI jobs to run."""
parser = argparse.ArgumentParser(
@@ -228,23 +246,37 @@ def main() -> None:
run_clang_format = should_run_clang_format(args.branch)
run_python_linters = should_run_python_linters(args.branch)
# Get changed components using list-components.py for exact compatibility
# Get both directly changed and all changed components (with dependencies) in one call
script_path = Path(__file__).parent / "list-components.py"
cmd = [sys.executable, str(script_path), "--changed"]
cmd = [sys.executable, str(script_path), "--changed-with-deps"]
if args.branch:
cmd.extend(["-b", args.branch])
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
changed_components = parse_list_components_output(result.stdout)
component_data = json.loads(result.stdout)
directly_changed_components = component_data["directly_changed"]
changed_components = component_data["all_changed"]
# Filter to only components that have test files
# Components without tests shouldn't generate CI test jobs
tests_dir = Path(root_path) / "tests" / "components"
changed_components_with_tests = [
component for component in changed_components if _component_has_tests(component)
]
# Get directly changed components with tests (for isolated testing)
# These will be tested WITHOUT --testing-mode in CI to enable full validation
# (pin conflicts, etc.) since they contain the actual changes being reviewed
directly_changed_with_tests = [
component
for component in changed_components
if (component_test_dir := tests_dir / component).exists()
and any(component_test_dir.glob("test.*.yaml"))
for component in directly_changed_components
if _component_has_tests(component)
]
# Get dependency-only components (for grouped testing)
dependency_only_components = [
component
for component in changed_components_with_tests
if component not in directly_changed_components
]
# Build output
@@ -255,7 +287,11 @@ def main() -> None:
"python_linters": run_python_linters,
"changed_components": changed_components,
"changed_components_with_tests": changed_components_with_tests,
"directly_changed_components_with_tests": directly_changed_with_tests,
"dependency_only_components_with_tests": dependency_only_components,
"component_test_count": len(changed_components_with_tests),
"directly_changed_count": len(directly_changed_with_tests),
"dependency_only_count": len(dependency_only_components),
}
# Output as JSON

View File

@@ -185,18 +185,32 @@ def main():
"-c",
"--changed",
action="store_true",
help="List all components required for testing based on changes",
help="List all components required for testing based on changes (includes dependencies)",
)
parser.add_argument(
"--changed-direct",
action="store_true",
help="List only directly changed components (without dependencies)",
)
parser.add_argument(
"--changed-with-deps",
action="store_true",
help="Output JSON with both directly changed and all changed components",
)
parser.add_argument(
"-b", "--branch", help="Branch to compare changed files against"
)
args = parser.parse_args()
if args.branch and not args.changed:
parser.error("--branch requires --changed")
if args.branch and not (
args.changed or args.changed_direct or args.changed_with_deps
):
parser.error(
"--branch requires --changed, --changed-direct, or --changed-with-deps"
)
if args.changed:
# When --changed is passed, only get the changed files
if args.changed or args.changed_direct or args.changed_with_deps:
# When --changed* is passed, only get the changed files
changed = changed_files(args.branch)
# If any base test file(s) changed, there's no need to filter out components
@@ -210,8 +224,25 @@ def main():
# Get all component files
files = get_all_component_files()
for c in get_components(files, args.changed):
print(c)
if args.changed_with_deps:
# Return JSON with both directly changed and all changed components
import json
directly_changed = get_components(files, False)
all_changed = get_components(files, True)
output = {
"directly_changed": directly_changed,
"all_changed": all_changed,
}
print(json.dumps(output))
elif args.changed_direct:
# Return only directly changed components (without dependencies)
for c in get_components(files, False):
print(c)
else:
# Return all changed components (with dependencies) - default behavior
for c in get_components(files, args.changed):
print(c)
if __name__ == "__main__":

View File

@@ -56,6 +56,7 @@ def create_intelligent_batches(
components: list[str],
tests_dir: Path,
batch_size: int = 40,
directly_changed: set[str] | None = None,
) -> list[list[str]]:
"""Create batches optimized for component grouping.
@@ -63,6 +64,7 @@ def create_intelligent_batches(
components: List of component names to batch
tests_dir: Path to tests/components directory
batch_size: Target size for each batch
directly_changed: Set of directly changed components (for logging only)
Returns:
List of component batches (lists of component names)
@@ -94,10 +96,17 @@ def create_intelligent_batches(
for component in components_with_tests:
# Components that can't be grouped get unique signatures
# This includes both manually curated ISOLATED_COMPONENTS and
# automatically detected non_groupable components
# This includes:
# - Manually curated ISOLATED_COMPONENTS
# - Automatically detected non_groupable components
# - Directly changed components (passed via --isolate in CI)
# These can share a batch/runner but won't be grouped/merged
if component in ISOLATED_COMPONENTS or component in non_groupable:
is_isolated = (
component in ISOLATED_COMPONENTS
or component in non_groupable
or (directly_changed and component in directly_changed)
)
if is_isolated:
signature_groups[f"isolated_{component}"].append(component)
continue
@@ -187,6 +196,10 @@ def main() -> int:
default=Path("tests/components"),
help="Path to tests/components directory",
)
parser.add_argument(
"--directly-changed",
help="JSON array of directly changed component names (for logging only)",
)
parser.add_argument(
"--output",
"-o",
@@ -208,11 +221,21 @@ def main() -> int:
print("Components must be a JSON array", file=sys.stderr)
return 1
# Parse directly changed components list from JSON (if provided)
directly_changed = None
if args.directly_changed:
try:
directly_changed = set(json.loads(args.directly_changed))
except json.JSONDecodeError as e:
print(f"Error parsing directly-changed JSON: {e}", file=sys.stderr)
return 1
# Create intelligent batches
batches = create_intelligent_batches(
components=components,
tests_dir=args.tests_dir,
batch_size=args.batch_size,
directly_changed=directly_changed,
)
# Convert batches to space-separated strings for CI
@@ -238,13 +261,37 @@ def main() -> int:
isolated_count = sum(
1
for comp in all_batched_components
if comp in ISOLATED_COMPONENTS or comp in non_groupable
if comp in ISOLATED_COMPONENTS
or comp in non_groupable
or (directly_changed and comp in directly_changed)
)
groupable_count = actual_components - isolated_count
print("\n=== Intelligent Batch Summary ===", file=sys.stderr)
print(f"Total components requested: {len(components)}", file=sys.stderr)
print(f"Components with test files: {actual_components}", file=sys.stderr)
# Show breakdown of directly changed vs dependencies
if directly_changed:
direct_count = sum(
1 for comp in all_batched_components if comp in directly_changed
)
dep_count = actual_components - direct_count
direct_comps = [
comp for comp in all_batched_components if comp in directly_changed
]
dep_comps = [
comp for comp in all_batched_components if comp not in directly_changed
]
print(
f" - Direct changes: {direct_count} ({', '.join(sorted(direct_comps))})",
file=sys.stderr,
)
print(
f" - Dependencies: {dep_count} ({', '.join(sorted(dep_comps))})",
file=sys.stderr,
)
print(f" - Groupable (weight=1): {groupable_count}", file=sys.stderr)
print(f" - Isolated (weight=10): {isolated_count}", file=sys.stderr)
if actual_components < len(components):

View File

@@ -365,6 +365,7 @@ def run_grouped_component_tests(
build_dir: Path,
esphome_command: str,
continue_on_fail: bool,
additional_isolated: set[str] | None = None,
) -> tuple[set[tuple[str, str]], list[str], list[str], dict[str, str]]:
"""Run grouped component tests.
@@ -376,6 +377,7 @@ def run_grouped_component_tests(
build_dir: Path to build directory
esphome_command: ESPHome command (config/compile)
continue_on_fail: Whether to continue on failure
additional_isolated: Additional components to treat as isolated (not grouped)
Returns:
Tuple of (tested_components, passed_tests, failed_tests, failed_commands)
@@ -397,6 +399,17 @@ def run_grouped_component_tests(
# Track why components can't be grouped (for detailed output)
non_groupable_reasons = {}
# Merge additional isolated components with predefined ones
# ISOLATED COMPONENTS are tested individually WITHOUT --testing-mode
# This is critical because:
# - Grouped tests use --testing-mode which disables pin conflict checks and other validation
# - These checks are disabled to allow config merging (multiple components in one build)
# - For directly changed components (via --isolate), we need full validation to catch issues
# - Dependencies are safe to group since they weren't modified in the PR
all_isolated = set(ISOLATED_COMPONENTS.keys())
if additional_isolated:
all_isolated.update(additional_isolated)
# Group by (platform, bus_signature)
for component, platforms in component_buses.items():
if component not in all_tests:
@@ -404,7 +417,7 @@ def run_grouped_component_tests(
# Skip components that must be tested in isolation
# These are shown separately and should not be in non_groupable_reasons
if component in ISOLATED_COMPONENTS:
if component in all_isolated:
continue
# Skip base bus components (these test the bus platforms themselves)
@@ -453,15 +466,28 @@ def run_grouped_component_tests(
print("\nGrouping Plan:")
print("-" * 80)
# Show isolated components (must test individually due to known issues)
isolated_in_tests = [c for c in ISOLATED_COMPONENTS if c in all_tests]
# Show isolated components (must test individually due to known issues or direct changes)
isolated_in_tests = [c for c in all_isolated if c in all_tests]
if isolated_in_tests:
print(
f"\n{len(isolated_in_tests)} components must be tested in isolation (known build issues):"
)
for comp in sorted(isolated_in_tests):
reason = ISOLATED_COMPONENTS[comp]
print(f" - {comp}: {reason}")
predefined_isolated = [c for c in isolated_in_tests if c in ISOLATED_COMPONENTS]
additional_in_tests = [
c for c in isolated_in_tests if c in (additional_isolated or set())
]
if predefined_isolated:
print(
f"\n{len(predefined_isolated)} components must be tested in isolation (known build issues):"
)
for comp in sorted(predefined_isolated):
reason = ISOLATED_COMPONENTS[comp]
print(f" - {comp}: {reason}")
if additional_in_tests:
print(
f"\n{len(additional_in_tests)} components tested in isolation (directly changed in PR):"
)
for comp in sorted(additional_in_tests):
print(f" - {comp}")
# Show base bus components (test the bus platform implementations)
base_bus_in_tests = [c for c in BASE_BUS_COMPONENTS if c in all_tests]
@@ -733,6 +759,7 @@ def test_components(
esphome_command: str,
continue_on_fail: bool,
enable_grouping: bool = True,
isolated_components: set[str] | None = None,
) -> int:
"""Test components with optional intelligent grouping.
@@ -742,6 +769,10 @@ def test_components(
esphome_command: ESPHome command (config/compile)
continue_on_fail: Whether to continue on failure
enable_grouping: Whether to enable component grouping
isolated_components: Set of component names to test in isolation (not grouped).
These are tested WITHOUT --testing-mode to enable full validation
(pin conflicts, etc). This is used in CI for directly changed components
to catch issues that would be missed with --testing-mode.
Returns:
Exit code (0 for success, 1 for failure)
@@ -788,6 +819,7 @@ def test_components(
build_dir=build_dir,
esphome_command=esphome_command,
continue_on_fail=continue_on_fail,
additional_isolated=isolated_components,
)
# Then run individual tests for components not in groups
@@ -912,18 +944,30 @@ def main() -> int:
action="store_true",
help="Disable component grouping (test each component individually)",
)
parser.add_argument(
"--isolate",
help="Comma-separated list of components to test in isolation (not grouped with others). "
"These are tested WITHOUT --testing-mode to enable full validation. "
"Used in CI for directly changed components to catch pin conflicts and other issues.",
)
args = parser.parse_args()
# Parse component patterns
component_patterns = [p.strip() for p in args.components.split(",")]
# Parse isolated components
isolated_components = None
if args.isolate:
isolated_components = {c.strip() for c in args.isolate.split(",") if c.strip()}
return test_components(
component_patterns=component_patterns,
platform_filter=args.target,
esphome_command=args.esphome_command,
continue_on_fail=args.continue_on_fail,
enable_grouping=not args.no_grouping,
isolated_components=isolated_components,
)