name: "Upstream Sync"

on:
  schedule:
    - cron: "0 * * * *"
  workflow_dispatch:

jobs:
  sync_latest_from_upstream:
    name: Sync latest commits from upstream repo
    runs-on: ubuntu-latest
    if: ${{ github.event.repository.name != 'AwesomeGadgets' || github.event.repository.owner != 'AnYiEE' }}
    steps:
      - name: Checkout target repo
        uses: actions/checkout@v4
      - name: Sync upstream changes (1/2)
        id: sync
        uses: aormsby/Fork-Sync-With-Upstream-action@v3.4.1
        with:
          target_sync_branch: master
          target_repo_token: ${{ secrets.GITHUB_TOKEN }}
          upstream_sync_branch: master
          upstream_sync_repo: AnYiEE/AwesomeGadgets
      - name: Sync check (1/2)
        if: ${{ success() }}
        run: |
          echo "[Success] 同步成功。"
          exit 0
      - name: Retry sync upstream changes
        if: ${{ steps.sync.conclusion == 'failure' }}
        run: |
          echo "[Error] 同步失败，可能是由于本地仓库 pnpm-lock.yaml 文件和上游冲突，正尝试自动合并更改。"
      - name: Install pnpm
        if: ${{ steps.sync.conclusion == 'failure' }}
        uses: pnpm/action-setup@v4
        with:
          version: latest
          run_install: false
      - name: Get pnpm store directory
        if: ${{ steps.sync.conclusion == 'failure' }}
        shell: bash
        run: |
          echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
      - name: Setup pnpm cache
        if: ${{ steps.sync.conclusion == 'failure' }}
        uses: actions/cache@v4
        with:
          path: ${{ env.STORE_PATH }}
          key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-pnpm-store-
      - name: Install dependencies
        if: ${{ steps.sync.conclusion == 'failure' }}
        run: pnpm i
      - name: Sync upstream changes (2/2)
        if: ${{ steps.sync.conclusion == 'failure' }}
        uses: aormsby/Fork-Sync-With-Upstream-action@v3.4.1
        with:
          target_sync_branch: master
          target_repo_token: ${{ secrets.GITHUB_TOKEN }}
          upstream_sync_branch: master
          upstream_sync_repo: AnYiEE/AwesomeGadgets
      - name: Sync check (2/2)
        if: ${{ steps.sync.conclusion == 'failure' && failure() }}
        run: |
          echo "[Error] 由于上游仓库 workflow 文件变更或无法自动合并更改，请手动 Sync Fork 一次或手动合并更改。"
          exit 1
