# ═══════════════════════════════════════════════
# 🔺 Sovereign: TCS-0002∞ | Root: SYS-GLW-0001
# 📜 Copyright: 国作登字-2026-A-00037559
# ═══════════════════════════════════════════════
name: 铸渊 · 飞书SYSLOG桥接

on:
  repository_dispatch:
    types: [feishu-syslog]

jobs:
  bridge-feishu-syslog:
    name: 📡 飞书SYSLOG → syslog-inbox
    runs-on: ubuntu-latest
    permissions:
      contents: write

    steps:
      - uses: actions/checkout@v4
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          ref: main

      - name: 📥 写入 syslog-inbox
        env:
          SYSLOG_RAW: ${{ toJson(github.event.client_payload) }}
        run: |
          mkdir -p syslog-inbox
          TIMESTAMP=$(date -u +%Y%m%d-%H%M%S)
          RAW_SOURCE=$( echo "$SYSLOG_RAW" | python3 -c "import sys,json; d=json.load(sys.stdin); print(d.get('source','unknown'))" 2>/dev/null || echo 'feishu' )
          SOURCE=$( echo "$RAW_SOURCE" | tr -cd 'a-zA-Z0-9_-' | head -c 64 )
          SOURCE=${SOURCE:-feishu}
          FILENAME="syslog-inbox/${TIMESTAMP}_${SOURCE}.json"
          echo "$SYSLOG_RAW" > "$FILENAME"
          echo "📥 SYSLOG已写入: $FILENAME"
          echo "📋 内容预览:"
          cat "$FILENAME" | head -20

      - name: 提交到仓库
        run: |
          git config user.name "铸渊[bot]"
          git config user.email "zhu-yuan-bot@guanghulab.com"
          git add syslog-inbox/
          git diff --staged --quiet || git commit -m "📡 feishu-syslog: 飞书舒舒转发SYSLOG [auto-bridge]"
          git push

      - name: ✅ 桥接完成
        run: |
          echo "✅ 飞书SYSLOG已桥接到 syslog-inbox/"
          echo "📡 等待 syslog-pipeline 自动触发处理"
