|
| 1 | +import os |
| 2 | +import subprocess |
| 3 | +import sys |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | + |
| 7 | +def get_changed_files(base_sha: str, head_sha: str) -> list[str]: |
| 8 | + if not base_sha or not head_sha: |
| 9 | + return [] |
| 10 | + try: |
| 11 | + output = subprocess.check_output( |
| 12 | + ["git", "diff", "--name-only", base_sha, head_sha], |
| 13 | + text=True, |
| 14 | + stderr=subprocess.STDOUT, |
| 15 | + ) |
| 16 | + except subprocess.CalledProcessError as exc: |
| 17 | + sys.stderr.write(exc.output) |
| 18 | + return [] |
| 19 | + return [line.strip() for line in output.splitlines() if line.strip()] |
| 20 | + |
| 21 | + |
| 22 | +def main() -> None: |
| 23 | + base_sha = os.environ.get("BASE_SHA", "") |
| 24 | + head_sha = os.environ.get("HEAD_SHA", "") |
| 25 | + |
| 26 | + changed = get_changed_files(base_sha, head_sha) |
| 27 | + changed_use_cases = [p for p in changed if p.startswith("02-use-cases/")] |
| 28 | + |
| 29 | + if not changed_use_cases: |
| 30 | + print("No changes under 02-use-cases, skipping main.py checks.") |
| 31 | + return |
| 32 | + |
| 33 | + candidate_dirs: set[Path] = set() |
| 34 | + for rel_path in changed_use_cases: |
| 35 | + parts = Path(rel_path).parts |
| 36 | + if len(parts) >= 2 and parts[0] == "02-use-cases" and parts[1] != "beginner": |
| 37 | + candidate_dirs.add(Path(parts[0]) / parts[1]) |
| 38 | + |
| 39 | + if not candidate_dirs: |
| 40 | + print( |
| 41 | + "No top-level 02-use-cases/* directories detected, skipping main.py checks." |
| 42 | + ) |
| 43 | + return |
| 44 | + |
| 45 | + print("Use-case directories to check:") |
| 46 | + for d in sorted(candidate_dirs): |
| 47 | + print(f" - {d}") |
| 48 | + |
| 49 | + failed_dirs: list[Path] = [] |
| 50 | + |
| 51 | + for d in sorted(candidate_dirs): |
| 52 | + # deploy_sh = d / "deploy.sh" |
| 53 | + # if deploy_sh.is_file(): |
| 54 | + # print(f"Found deploy.sh in {d}, running it") |
| 55 | + # result = subprocess.run(["bash", "deploy.sh"], cwd=str(d)) |
| 56 | + # if result.returncode != 0: |
| 57 | + # failed_dirs.append(d) |
| 58 | + # continue |
| 59 | + |
| 60 | + agent_py = d / "agent.py" |
| 61 | + |
| 62 | + if not agent_py.is_file(): |
| 63 | + print(f"No agent.py in {d}, skipping agentkit commands now.") |
| 64 | + continue |
| 65 | + |
| 66 | + agent_name = d.name |
| 67 | + workflow_name = os.environ.get("GITHUB_WORKFLOW", "") |
| 68 | + if workflow_name == "agentkit-check": |
| 69 | + agent_name = f"check_{agent_name}" |
| 70 | + print(f"Running 'agentkit config' in {d} for agent_name={agent_name}") |
| 71 | + config_cmd = [ |
| 72 | + "agentkit", |
| 73 | + "config", |
| 74 | + "--agent_name", |
| 75 | + agent_name, |
| 76 | + "--entry_point", |
| 77 | + "agent.py", |
| 78 | + "--description", |
| 79 | + "a helpful agent", |
| 80 | + "--launch_type", |
| 81 | + "cloud", |
| 82 | + "--image_tag", |
| 83 | + "v1.0.0", |
| 84 | + "--region", |
| 85 | + "cn-beijing", |
| 86 | + ] |
| 87 | + result = subprocess.run(config_cmd, cwd=str(d)) |
| 88 | + if result.returncode != 0: |
| 89 | + failed_dirs.append(d) |
| 90 | + print(f"'agentkit config' failed in {d}, skipping launch.") |
| 91 | + continue |
| 92 | + |
| 93 | + command = os.environ.get("AGENTKIT_COMMAND", "launch") |
| 94 | + print(f"Running 'agentkit {command}' in {d}") |
| 95 | + launch_cmd = ["agentkit", command] |
| 96 | + result = subprocess.run(launch_cmd, cwd=str(d)) |
| 97 | + if result.returncode != 0: |
| 98 | + failed_dirs.append(d) |
| 99 | + |
| 100 | + if failed_dirs: |
| 101 | + sys.stderr.write( |
| 102 | + "agentkit checks failed in directories: " |
| 103 | + + ", ".join(str(d) for d in failed_dirs) |
| 104 | + + "\n" |
| 105 | + ) |
| 106 | + raise SystemExit(1) |
| 107 | + |
| 108 | + |
| 109 | +if __name__ == "__main__": |
| 110 | + main() |
0 commit comments