🔧 chore(tsl): add playbook sync workflow
This commit is contained in:
@@ -0,0 +1,66 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
# Build the minimal TSL playbook bundle and publish its *expanded* contents to
|
||||||
|
# the tsl-playbook branch. The branch tree mirrors the bundle root directly
|
||||||
|
# (AGENTS.md, docs/, skills/) — there is no wrapping tsl-playbook/ directory.
|
||||||
|
|
||||||
|
REPO_DIR="${REPO_DIR:-$(pwd)}"
|
||||||
|
TARGET_BRANCH="${TARGET_BRANCH:-tsl-playbook}"
|
||||||
|
BUILD_SCRIPT="${BUILD_SCRIPT:-scripts/build_tsl_playbook.py}"
|
||||||
|
COMMIT_AUTHOR_NAME="${COMMIT_AUTHOR_NAME:-ci[bot]}"
|
||||||
|
COMMIT_AUTHOR_EMAIL="${COMMIT_AUTHOR_EMAIL:-ci-bot@local}"
|
||||||
|
|
||||||
|
cd "$REPO_DIR"
|
||||||
|
|
||||||
|
git config user.name "$COMMIT_AUTHOR_NAME"
|
||||||
|
git config user.email "$COMMIT_AUTHOR_EMAIL"
|
||||||
|
|
||||||
|
source_sha="$(git rev-parse HEAD)"
|
||||||
|
source_short="$(git rev-parse --short HEAD)"
|
||||||
|
|
||||||
|
# Build the bundle into a temp dir *outside* the repo so that resetting the
|
||||||
|
# target branch's working tree can never clobber the freshly built artifact.
|
||||||
|
build_dir="$(mktemp -d)"
|
||||||
|
cleanup() {
|
||||||
|
rm -rf "$build_dir"
|
||||||
|
}
|
||||||
|
trap cleanup EXIT
|
||||||
|
|
||||||
|
bundle="$build_dir/tsl-playbook"
|
||||||
|
python3 "$BUILD_SCRIPT" --output "$bundle"
|
||||||
|
|
||||||
|
# Check out (or create) the target branch. It is a content-only branch that
|
||||||
|
# shares no history with main, so an orphan branch keeps it clean.
|
||||||
|
if git show-ref --verify --quiet "refs/remotes/origin/$TARGET_BRANCH"; then
|
||||||
|
git fetch origin "$TARGET_BRANCH"
|
||||||
|
git checkout -B "$TARGET_BRANCH" "origin/$TARGET_BRANCH"
|
||||||
|
else
|
||||||
|
git checkout --orphan "$TARGET_BRANCH"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Replace the entire tracked tree with the bundle contents. `git rm` clears
|
||||||
|
# tracked files; then copy the expanded bundle to the repo root.
|
||||||
|
git rm -rf --quiet . >/dev/null 2>&1 || true
|
||||||
|
|
||||||
|
# Copy bundle contents (including dotfiles) to the repo root.
|
||||||
|
cp -R "$bundle"/. "$REPO_DIR"/
|
||||||
|
|
||||||
|
git add -A
|
||||||
|
|
||||||
|
if git diff --cached --quiet; then
|
||||||
|
echo "No tsl-playbook changes to publish."
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
git commit -m ":package: deps(tsl): sync tsl-playbook from ${source_short}
|
||||||
|
|
||||||
|
Source-Commit: ${source_sha}"
|
||||||
|
|
||||||
|
TOKEN="${WORKFLOW:-}"
|
||||||
|
if [ -n "$TOKEN" ] && [ -n "${GITHUB_SERVER_URL:-}" ] && [ -n "${GITHUB_REPOSITORY:-}" ]; then
|
||||||
|
git remote set-url origin "https://oauth2:${TOKEN}@${GITHUB_SERVER_URL#https://}/${GITHUB_REPOSITORY}.git"
|
||||||
|
fi
|
||||||
|
|
||||||
|
git push origin "$TARGET_BRANCH"
|
||||||
|
echo "✅ Published tsl-playbook @ ${source_short}"
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
name: 📦 Sync TSL Playbook
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
|
concurrency:
|
||||||
|
group: sync-tsl-playbook-${{ github.repository }}
|
||||||
|
cancel-in-progress: true
|
||||||
|
|
||||||
|
env:
|
||||||
|
WORKSPACE_DIR: "/home/workspace"
|
||||||
|
TARGET_BRANCH: "tsl-playbook"
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
sync:
|
||||||
|
name: 📦 Build and publish tsl-playbook
|
||||||
|
runs-on: ubuntu-22.04
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: 📥 准备仓库
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
echo "========================================"
|
||||||
|
echo "📦 准备仓库到 WORKSPACE_DIR"
|
||||||
|
echo "========================================"
|
||||||
|
|
||||||
|
REPO_NAME="${{ github.event.repository.name }}"
|
||||||
|
TOKEN="${{ secrets.WORKFLOW }}"
|
||||||
|
mkdir -p "$WORKSPACE_DIR"
|
||||||
|
REPO_DIR="$(mktemp -d "$WORKSPACE_DIR/${REPO_NAME}.XXXXXX")"
|
||||||
|
if [ -n "$TOKEN" ]; then
|
||||||
|
REPO_URL="https://oauth2:${TOKEN}@${GITHUB_SERVER_URL#https://}/${GITHUB_REPOSITORY}.git"
|
||||||
|
else
|
||||||
|
REPO_URL="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git"
|
||||||
|
fi
|
||||||
|
|
||||||
|
git clone "$REPO_URL" "$REPO_DIR"
|
||||||
|
|
||||||
|
git -C "$REPO_DIR" fetch origin main
|
||||||
|
git -C "$REPO_DIR" checkout -B main origin/main
|
||||||
|
|
||||||
|
git config --global --add safe.directory "$REPO_DIR"
|
||||||
|
echo "REPO_DIR=$REPO_DIR" >> "$GITHUB_ENV"
|
||||||
|
echo "✅ 仓库准备完成"
|
||||||
|
|
||||||
|
- name: 📦 Build and publish tsl-playbook
|
||||||
|
shell: bash
|
||||||
|
env:
|
||||||
|
WORKFLOW: ${{ secrets.WORKFLOW }}
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
cd "$REPO_DIR"
|
||||||
|
|
||||||
|
echo "========================================"
|
||||||
|
echo "🔨 Build bundle and sync $TARGET_BRANCH"
|
||||||
|
echo "========================================"
|
||||||
|
|
||||||
|
TARGET_BRANCH="$TARGET_BRANCH" bash .gitea/ci/sync_tsl_playbook.sh
|
||||||
|
|
||||||
|
- name: 🧹 清理临时仓库
|
||||||
|
if: always()
|
||||||
|
run: |
|
||||||
|
rm -rf "$REPO_DIR"
|
||||||
@@ -0,0 +1,96 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""Build the minimal TSL playbook bundle."""
|
||||||
|
import argparse
|
||||||
|
import shutil
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
|
DEFAULT_OUTPUT = Path("tmp") / "tsl-playbook"
|
||||||
|
API_REFERENCE_BULLET = (
|
||||||
|
"- 查 API / 函数的名字、参数、返回值和示例时,使用 "
|
||||||
|
"`tsl-api-reference` skill;已知名走 `--name`,未知名走 `--kw`。"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def copy_tree(src: Path, dst: Path) -> None:
|
||||||
|
shutil.copytree(src, dst)
|
||||||
|
|
||||||
|
|
||||||
|
def build_agents_text(ruleset_path: Path) -> str:
|
||||||
|
text = ruleset_path.read_text(encoding="utf-8")
|
||||||
|
text = text.replace("# TSL 智能体规则", "# TSL Agent Instructions", 1)
|
||||||
|
if API_REFERENCE_BULLET not in text:
|
||||||
|
marker = (
|
||||||
|
"- 需要任何 TSL 语法、文件模型、函数、模块或金融数据事实时,"
|
||||||
|
"第一跳统一进 `docs/tsl/index.md`,由它路由到具体页;"
|
||||||
|
"不在本文件内猜文件路径,也不全目录搜索。"
|
||||||
|
)
|
||||||
|
text = text.replace(marker, f"{marker}\n{API_REFERENCE_BULLET}", 1)
|
||||||
|
return text.rstrip("\n") + "\n"
|
||||||
|
|
||||||
|
|
||||||
|
def ensure_sources(repo_root: Path) -> tuple[Path, Path, Path]:
|
||||||
|
docs_tsl = repo_root / "docs" / "tsl"
|
||||||
|
skill = repo_root / "skills" / "tsl-api-reference"
|
||||||
|
ruleset = repo_root / "rulesets" / "tsl" / "index.md"
|
||||||
|
missing = [str(path) for path in (docs_tsl, skill, ruleset) if not path.exists()]
|
||||||
|
if missing:
|
||||||
|
raise FileNotFoundError("missing source path(s): " + ", ".join(missing))
|
||||||
|
return docs_tsl, skill, ruleset
|
||||||
|
|
||||||
|
|
||||||
|
def clean_output(output: Path, repo_root: Path) -> None:
|
||||||
|
resolved = output.resolve()
|
||||||
|
forbidden = {
|
||||||
|
repo_root.resolve(),
|
||||||
|
(repo_root / "docs").resolve(),
|
||||||
|
(repo_root / "skills").resolve(),
|
||||||
|
(repo_root / "rulesets").resolve(),
|
||||||
|
}
|
||||||
|
if resolved in forbidden:
|
||||||
|
raise ValueError(f"refusing to replace source directory: {output}")
|
||||||
|
if output.exists():
|
||||||
|
shutil.rmtree(output)
|
||||||
|
output.mkdir(parents=True)
|
||||||
|
|
||||||
|
|
||||||
|
def build(output: Path, repo_root: Path) -> None:
|
||||||
|
docs_tsl, skill, ruleset = ensure_sources(repo_root)
|
||||||
|
clean_output(output, repo_root)
|
||||||
|
|
||||||
|
(output / "AGENTS.md").write_text(
|
||||||
|
build_agents_text(ruleset), encoding="utf-8", newline="\n"
|
||||||
|
)
|
||||||
|
copy_tree(docs_tsl, output / "docs" / "tsl")
|
||||||
|
copy_tree(skill, output / "skills" / "tsl-api-reference")
|
||||||
|
|
||||||
|
|
||||||
|
def main(argv=None) -> int:
|
||||||
|
if hasattr(sys.stdout, "reconfigure"):
|
||||||
|
sys.stdout.reconfigure(encoding="utf-8")
|
||||||
|
parser = argparse.ArgumentParser(description=__doc__)
|
||||||
|
parser.add_argument(
|
||||||
|
"--output",
|
||||||
|
default=str(DEFAULT_OUTPUT),
|
||||||
|
help=f"output directory (default: {DEFAULT_OUTPUT.as_posix()})",
|
||||||
|
)
|
||||||
|
args = parser.parse_args(argv)
|
||||||
|
|
||||||
|
repo_root = Path(__file__).resolve().parents[1]
|
||||||
|
output = Path(args.output)
|
||||||
|
if not output.is_absolute():
|
||||||
|
output = repo_root / output
|
||||||
|
|
||||||
|
try:
|
||||||
|
build(output, repo_root)
|
||||||
|
except (FileNotFoundError, ValueError, OSError) as exc:
|
||||||
|
print(f"ERROR: {exc}", file=sys.stderr)
|
||||||
|
return 1
|
||||||
|
|
||||||
|
print(f"wrote {output}")
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
raise SystemExit(main())
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
import tempfile
|
||||||
|
import unittest
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
|
ROOT = Path(__file__).resolve().parents[1]
|
||||||
|
SCRIPT = ROOT / "scripts" / "build_tsl_playbook.py"
|
||||||
|
|
||||||
|
|
||||||
|
class BuildTslPlaybookTests(unittest.TestCase):
|
||||||
|
def test_builds_minimal_tsl_playbook_tree(self):
|
||||||
|
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||||
|
output = Path(tmp_dir) / "tsl-playbook"
|
||||||
|
|
||||||
|
result = subprocess.run(
|
||||||
|
[sys.executable, str(SCRIPT), "--output", str(output)],
|
||||||
|
capture_output=True,
|
||||||
|
text=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(result.returncode, 0, msg=result.stderr)
|
||||||
|
self.assertEqual(
|
||||||
|
sorted(path.name for path in output.iterdir()),
|
||||||
|
["AGENTS.md", "docs", "skills"],
|
||||||
|
)
|
||||||
|
self.assertFalse((output / "playbook.toml").exists())
|
||||||
|
self.assertFalse((output / ".agents").exists())
|
||||||
|
self.assertFalse((output / "docs" / "index.md").exists())
|
||||||
|
|
||||||
|
self.assertTrue((output / "docs" / "tsl" / "index.md").is_file())
|
||||||
|
self.assertTrue(
|
||||||
|
(output / "skills" / "tsl-api-reference" / "SKILL.md").is_file()
|
||||||
|
)
|
||||||
|
self.assertTrue(
|
||||||
|
(
|
||||||
|
output
|
||||||
|
/ "skills"
|
||||||
|
/ "tsl-api-reference"
|
||||||
|
/ "scripts"
|
||||||
|
/ "lookup.py"
|
||||||
|
).is_file()
|
||||||
|
)
|
||||||
|
|
||||||
|
agents_text = (output / "AGENTS.md").read_text(encoding="utf-8")
|
||||||
|
self.assertIn("# TSL Agent Instructions", agents_text)
|
||||||
|
self.assertIn("docs/tsl/index.md", agents_text)
|
||||||
|
self.assertIn("tsl-api-reference", agents_text)
|
||||||
|
self.assertNotIn(".agents/index.md", agents_text)
|
||||||
|
self.assertNotIn(".agents/tsl/index.md", agents_text)
|
||||||
|
|
||||||
|
source_docs = count_files(ROOT / "docs" / "tsl")
|
||||||
|
output_docs = count_files(output / "docs" / "tsl")
|
||||||
|
self.assertEqual(output_docs, source_docs)
|
||||||
|
|
||||||
|
source_skill = count_files(ROOT / "skills" / "tsl-api-reference")
|
||||||
|
output_skill = count_files(output / "skills" / "tsl-api-reference")
|
||||||
|
self.assertEqual(output_skill, source_skill)
|
||||||
|
|
||||||
|
|
||||||
|
def count_files(path: Path) -> int:
|
||||||
|
return sum(1 for item in path.rglob("*") if item.is_file())
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
Reference in New Issue
Block a user