# =============================================================================
# Sync: CNB — 同步代码到腾讯 CNB 仓库
#
# 触发时机: 任何 push / 删除分支时自动运行
# 功能:
#   - push: 双向同步代码到 cnb.cool/tencent/cherry-markdown/cherry-markdown.git
#   - delete: 通过 PR trigger 删除 CNB 上对应分支
# 注意: delete job 仅限主仓库触发,防止 fork 误操作
# =============================================================================

name: 'Sync: CNB'

on:
  - push
  - delete

permissions:
  contents: read

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: false

jobs:
  sync-push:
    if: github.repository == 'Tencent/cherry-markdown'
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0
      
      - name: Sync to CNB Repository
        uses: docker://tencentcom/git-sync
        env:
          PLUGIN_TARGET_URL: "https://cnb.cool/tencent/cherry-markdown/cherry-markdown.git"
          PLUGIN_AUTH_TYPE: "https"
          PLUGIN_USERNAME: "cnb"
          PLUGIN_PASSWORD: ${{ secrets.CHERRY_MARKDOWN_CNB_SYNC_GITHUB }}
          PLUGIN_FORCE: "true"

  sync-delete-branch:
    if: ${{ github.repository == 'Tencent/cherry-markdown' && github.event_name == 'delete' && github.event.ref_type == 'branch' }}
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - uses: TDesignOteam/tdesign-pr-trigger-action@develop
        with:
          repo: 'tencent/cherry-markdown/cherry-markdown'
          token: ${{ secrets.CHERRY_MARKDOWN_CNB_SYNC_GITHUB }}
          branch: ${{ github.event.ref }}
          trigger: '/delete-cnb-branch'