# ═══════════════════════════════════════════════
# 🔺 Sovereign: TCS-0002∞ | Root: SYS-GLW-0001
# 📜 Copyright: 国作登字-2026-A-00037559
# ═══════════════════════════════════════════════
name: 铸渊 · Bridge E · GitHub Changes → Notion

# 管道 E：main 分支有 push 或 PR 创建/关闭 → 变更摘要同步到 Notion「📋 GitHub 变更日志」数据库
#
# 依赖 Secrets（仓库 Settings → Secrets and variables → Actions）：
#   NOTION_TOKEN    Notion 集成 token
#
# CHANGES_DB_ID 已内置默认值（e740b77aa6bd4ac0a2e8a75f678fba98）
# 如需覆盖，可在 Secrets 中添加 CHANGES_DB_ID

on:
  push:
    branches: [main]
  pull_request:
    types: [opened, closed]

jobs:
  bridge-changes-to-notion:
    name: 📡 Bridge E · GitHub Changes → Notion
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: read

    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 2   # 获取前一次 commit 以便 diff

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'

      # ── commit push ──────────────────────────────────────
      - name: 📡 同步 commit 变更到 Notion
        if: github.event_name == 'push'
        env:
          NOTION_TOKEN:     ${{ secrets.ZY_NOTION_TOKEN }}
          CHANGES_DB_ID:    ${{ secrets.ZY_NOTION_CHANGELOG_DB }}
          EVENT_TYPE:       commit
          COMMIT_SHA:       ${{ github.sha }}
          COMMIT_MSG:       ${{ github.event.head_commit.message }}
          COMMITTER:        ${{ github.event.head_commit.author.name || github.actor }}
          COMMIT_TIMESTAMP: ${{ github.event.head_commit.timestamp }}
          BRANCH:           ${{ github.ref_name }}
        run: |
          # git diff 输出换行分隔的变更文件列表（包含新增、修改、删除）
          # COMMIT_TIMESTAMP/COMMITTER fallback: GitHub 表达式 || 在空字符串时也会取右侧值
          CHANGED=$(git diff --name-only HEAD~1 HEAD 2>/dev/null || true)
          export CHANGED_FILES="$CHANGED"
          node scripts/notion-bridge.js changes

      # ── PR 事件 ───────────────────────────────────────────
      - name: 📡 同步 PR 变更到 Notion
        if: github.event_name == 'pull_request'
        env:
          NOTION_TOKEN:     ${{ secrets.ZY_NOTION_TOKEN }}
          CHANGES_DB_ID:    ${{ secrets.ZY_NOTION_CHANGELOG_DB }}
          EVENT_TYPE:       pr
          PR_NUMBER:        ${{ github.event.pull_request.number }}
          PR_TITLE:         ${{ github.event.pull_request.title }}
          PR_ACTION:        ${{ github.event.action }}
          PR_MERGED:        ${{ github.event.pull_request.merged }}
          COMMITTER:        ${{ github.event.pull_request.user.login }}
          COMMIT_TIMESTAMP: ${{ github.event.pull_request.updated_at }}
          COMMIT_SHA:       ${{ github.event.pull_request.head.sha }}
          COMMIT_MSG:       ${{ github.event.pull_request.title }}
          BRANCH:           ${{ github.event.pull_request.head.ref }}
        run: node scripts/notion-bridge.js changes

