| from __future__ import annotations | |
| import json | |
| import os | |
| import subprocess | |
| from typing import Any, Dict | |
| import requests | |
| from .registry import ToolRegistry | |
| def t_gh_dispatch(args: Dict[str, Any]) -> str: | |
| """Dispatch a GitHub Actions workflow. | |
| Args: repo (owner/name), workflow (yml filename), ref (branch/tag), inputs (dict) | |
| Requires: GITHUB_TOKEN in env. | |
| """ | |
| token = os.getenv("GITHUB_TOKEN") | |
| if not token: | |
| return json.dumps({"error": "GITHUB_TOKEN not set"}) | |
| repo = args.get("repo") | |
| workflow = args.get("workflow") | |
| ref = args.get("ref", "main") | |
| inputs = args.get("inputs") or {} | |
| if not repo or not workflow: | |
| return json.dumps({"error": "repo and workflow required"}) | |
| url = f"https://api.github.com/repos/{repo}/actions/workflows/{workflow}/dispatches" | |
| try: | |
| r = requests.post(url, headers={"Authorization": f"Bearer {token}", "Accept": "application/vnd.github+json"}, json={"ref": ref, "inputs": inputs}, timeout=20) | |
| return json.dumps({"status": r.status_code, "body": r.text}) | |
| except Exception as e: | |
| return json.dumps({"error": str(e)}) | |
| def t_docker_build_push(args: Dict[str, Any]) -> str: | |
| """Build and push a Docker image. | |
| Args: context, file (Dockerfile), tags (list), push (bool) | |
| Requires: docker CLI configured and logged in. | |
| """ | |
| context = args.get("context", ".") | |
| dockerfile = args.get("file", "Dockerfile") | |
| tags = args.get("tags") or [] | |
| push = bool(args.get("push", True)) | |
| build_cmd = ["docker", "build", "-f", dockerfile] | |
| for t in tags: | |
| build_cmd += ["-t", str(t)] | |
| build_cmd += [context] | |
| try: | |
| b = subprocess.run(build_cmd, capture_output=True, text=True) | |
| out = {"build_rc": b.returncode, "build_stdout": b.stdout[-4000:], "build_stderr": b.stderr[-4000:]} | |
| if push and tags and b.returncode == 0: | |
| push_logs = [] | |
| for t in tags: | |
| p = subprocess.run(["docker", "push", str(t)], capture_output=True, text=True) | |
| push_logs.append({"tag": t, "rc": p.returncode, "stdout": p.stdout[-2000:], "stderr": p.stderr[-2000:]}) | |
| out["push"] = push_logs | |
| return json.dumps(out) | |
| except Exception as e: | |
| return json.dumps({"error": str(e)}) | |
| def register_tools(reg: ToolRegistry) -> None: | |
| reg.register( | |
| name="gh_dispatch", | |
| description="Trigger a GitHub Actions workflow_dispatch on a repo.", | |
| parameters={"type": "object", "properties": {"repo": {"type": "string"}, "workflow": {"type": "string"}, "ref": {"type": "string"}, "inputs": {"type": "object"}}, "required": ["repo", "workflow"]}, | |
| handler=t_gh_dispatch, | |
| ) | |
| reg.register( | |
| name="docker_build_push", | |
| description="Build and optionally push a Docker image using local docker.", | |
| parameters={"type": "object", "properties": {"context": {"type": "string"}, "file": {"type": "string"}, "tags": {"type": "array", "items": {"type": "string"}}, "push": {"type": "boolean"}}}, | |
| handler=t_docker_build_push, | |
| ) | |