Heimdall Gitea / Detect CI scope (pull_request) Successful in 5s
Heimdall Gitea / Build and tests (pull_request) Successful in 19m48s
Heimdall Gitea / Build and scan agent image (pull_request) Skipped
Heimdall Gitea / Build and scan NVIDIA Agent image (pull_request) Skipped
Heimdall Gitea / Build and scan server image (pull_request) Successful in 8m34s
Heimdall Gitea / Publish versioned installer release (pull_request) Skipped
Heimdall Gitea / CI policy (pull_request) Successful in 2s
Keep the locked solution restore for dependency auditing, then build only affected test projects and uncovered changed source projects. Retain full solution builds for broad changes and non-PR validation. Refs #314
115 lines
4.5 KiB
Python
115 lines
4.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Choose affected test projects for a PR; test everything on main and tags."""
|
|
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
import xml.etree.ElementTree as ET
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
TESTS = sorted(path for path in (ROOT / "tests").glob("*/*.csproj") if path.stem != "Heimdall.TestCommon")
|
|
SOURCES = sorted((ROOT / "src").glob("*/*.csproj"))
|
|
ALL_PROJECTS = [*SOURCES, *(ROOT / "tests").glob("*/*.csproj")]
|
|
|
|
|
|
def references(project: Path) -> set[Path]:
|
|
tree = ET.parse(project)
|
|
return {
|
|
(project.parent / item.attrib["Include"].replace("\\", "/")).resolve()
|
|
for item in tree.iter("ProjectReference")
|
|
}
|
|
|
|
|
|
def referenced_projects(project: Path, graph: dict[Path, set[Path]]) -> set[Path]:
|
|
result: set[Path] = set()
|
|
pending = list(graph.get(project, set()))
|
|
while pending:
|
|
dependency = pending.pop()
|
|
if dependency in result:
|
|
continue
|
|
result.add(dependency)
|
|
pending.extend(graph.get(dependency, set()))
|
|
return result
|
|
|
|
|
|
def build_targets(affected: set[Path], suites: list[str], graph: dict[Path, set[Path]],
|
|
mode: str) -> list[str]:
|
|
if mode == "all":
|
|
return ["Heimdall.slnx"]
|
|
|
|
suite_projects = [project for project in TESTS if project.stem in suites]
|
|
covered_sources = set().union(*(referenced_projects(project, graph) for project in suite_projects)) if suite_projects else set()
|
|
targets = [*suite_projects, *(project for project in SOURCES
|
|
if project.resolve() in affected and project.resolve() not in covered_sources)]
|
|
return sorted({project.resolve().relative_to(ROOT).as_posix() for project in targets})
|
|
|
|
|
|
def changed_files() -> list[str] | None:
|
|
if os.environ.get("GITEA_EVENT_NAME") != "pull_request":
|
|
return None
|
|
result = subprocess.run(
|
|
["git", "diff", "--name-only", "origin/main...HEAD"],
|
|
cwd=ROOT, text=True, capture_output=True, check=False,
|
|
)
|
|
return result.stdout.splitlines() if result.returncode == 0 else None
|
|
|
|
|
|
def plan(changed: list[str] | None) -> dict[str, object]:
|
|
if changed is None:
|
|
suites = [p.stem for p in TESTS]
|
|
return {"mode": "all", "changed": [], "suites": suites,
|
|
"build_projects": build_targets(set(), suites, {}, "all")}
|
|
|
|
all_paths = {p.resolve() for p in ALL_PROJECTS}
|
|
graph = {project: references(project) for project in all_paths}
|
|
changed_projects: set[Path] = set()
|
|
broad = False
|
|
for name in changed:
|
|
parts = Path(name).parts
|
|
if parts and parts[0] in {"src", "tests"} and len(parts) >= 3:
|
|
if name.endswith(".csproj"):
|
|
broad = True
|
|
matches = list((ROOT / parts[0] / parts[1]).glob("*.csproj"))
|
|
changed_projects.update(p.resolve() for p in matches)
|
|
elif parts and parts[0] in {"src", "tests"}:
|
|
broad = True
|
|
elif name.startswith(("scripts/ci/", "docker/", "models/")) or name in {
|
|
"Heimdall.slnx", "global.json", "Directory.Build.props",
|
|
"Directory.Build.targets", "GitVersion.yml", "GitVersion.yaml",
|
|
}:
|
|
broad = True
|
|
|
|
if not broad and changed and not changed_projects and all(
|
|
name.startswith(("docs/", "skills/"))
|
|
or name in {"readme.md", "AGENTS.md", ".gitea/PULL_REQUEST_TEMPLATE.md"}
|
|
for name in changed
|
|
):
|
|
return {"mode": "affected", "changed": changed, "suites": [], "build_projects": []}
|
|
|
|
if broad or (changed and not changed_projects):
|
|
suites = [p.stem for p in TESTS]
|
|
return {"mode": "all", "changed": changed, "suites": suites,
|
|
"build_projects": build_targets(set(), suites, graph, "all")}
|
|
|
|
affected = set(changed_projects)
|
|
while True:
|
|
additions = {project for project, refs in graph.items() if refs & affected}
|
|
if additions <= affected:
|
|
break
|
|
affected |= additions
|
|
|
|
suites = [p.stem for p in TESTS if p.resolve() in affected]
|
|
projects = build_targets(affected, suites, graph, "affected")
|
|
return {"mode": "affected", "changed": changed, "suites": suites,
|
|
"build_projects": projects}
|
|
|
|
|
|
if __name__ == "__main__":
|
|
result = plan(changed_files())
|
|
output = ROOT / "artifacts" / "tests" / "plan.json"
|
|
output.parent.mkdir(parents=True, exist_ok=True)
|
|
output.write_text(json.dumps(result, indent=2) + "\n")
|
|
print(f"Test plan: {result['mode']} — {', '.join(result['suites']) or 'no .NET suite'}")
|
|
print(f"Build targets: {', '.join(result['build_projects']) or 'none'}")
|