"""Workflow simulator with YAML parse and deterministic CI rule checks.""" from __future__ import annotations from typing import Dict, Optional import yaml from server.models import FileContent class WorkflowSimulator: def validate(self, workflow: Optional[FileContent], files: Dict[str, FileContent]): if workflow is None: # Not all easy tasks include workflow; keep this permissive. return {"parse_success": True, "execution_success": True} content = workflow.content try: parsed = yaml.safe_load(content) except yaml.YAMLError as exc: return { "parse_success": False, "execution_success": False, "error": f"YAML parse error: {exc}", } if not isinstance(parsed, dict): return { "parse_success": False, "execution_success": False, "error": "Workflow root must be a mapping", } jobs = parsed.get("jobs") if not isinstance(jobs, dict) or not jobs: return { "parse_success": False, "execution_success": False, "error": "Workflow must define at least one job", } has_buildx_setup = "docker/setup-buildx-action" in content has_platforms = "platforms:" in content has_docker_login = "docker login" in content has_username_secret = "secrets.DOCKER_USERNAME" in content has_password_secret = "secrets.DOCKER_PASSWORD" in content for _, job in jobs.items(): if not isinstance(job, dict): continue steps = job.get("steps", []) if not isinstance(steps, list): return { "parse_success": False, "execution_success": False, "error": "Job steps must be a list", } checkout_index = -1 build_index = -1 for idx, step in enumerate(steps): if not isinstance(step, dict): continue uses = step.get("uses", "") run = step.get("run", "") if isinstance(uses, str) and "actions/checkout" in uses: checkout_index = idx if (isinstance(run, str) and "docker build" in run) or ( isinstance(uses, str) and "docker/build-push-action" in uses ): build_index = idx if build_index != -1 and (checkout_index == -1 or checkout_index > build_index): return { "parse_success": True, "execution_success": False, "exec_error": "Checkout must happen before Docker build steps", } if has_docker_login and (not has_username_secret or not has_password_secret): return { "parse_success": True, "execution_success": False, "exec_error": "Missing secrets environment variables", } if has_platforms and not has_buildx_setup: return { "parse_success": True, "execution_success": False, "exec_error": "Multi-platform build requires docker/setup-buildx-action", } return {"parse_success": True, "execution_success": True}