File size: 3,476 Bytes
85b7ac8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | """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}
|