name: Release

on:
  workflow_dispatch:
    inputs:
      dry_run:
        description: "Run semantic-release in dry-run mode"
        required: false
        default: "false"
        type: choice
        options:
          - "false"
          - "true"

permissions:
  actions: write
  contents: write
  issues: write
  pull-requests: write

jobs:
  release:
    name: Semantic Release
    runs-on: ubuntu-latest
    timeout-minutes: 30

    steps:
      - name: Checkout from GitCode
        uses: actions/checkout@v4
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          fetch-depth: 0
          persist-credentials: true
          fetch-tags: true

      - name: Add GitCode remote and fetch
        run: |
          git remote add gitcode https://oauth2:${{ secrets.GITCODE_TOKEN }}@gitcode.com/openeuler/polymind.git
          git fetch gitcode '+refs/heads/*:refs/remotes/gitcode/*' --force
          git fetch gitcode '+refs/tags/*:refs/tags/*' --force
          echo "=== Resetting master to GitCode ==="
          git reset --hard gitcode/master
          echo "=== Current commit ==="
          git log --oneline -5
          echo "=== Tags ==="
          git tag --list | sort -V

      - name: Configure Git user
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"

      - name: Setup pnpm
        uses: pnpm/action-setup@v4
        with:
          version: 11

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 24
          cache: pnpm
          registry-url: https://registry.npmjs.org/

      - name: Install dependencies
        run: pnpm install --frozen-lockfile

      - name: Create .env for build
        run: |
          cat > .env << 'EOF'
          NEXT_PUBLIC_AGENTD_API_URL=/api
          NEXT_PUBLIC_WS_URL=/api/ws
          NEXT_PUBLIC_API_TIMEOUT=120000
          NEXT_PUBLIC_MAX_RETRY_ATTEMPTS=3
          NEXT_PUBLIC_RECONNECT_INTERVAL=3000
          NEXT_PUBLIC_MAX_RECONNECT_ATTEMPTS=5
          NEXT_WITTYHUB_API_URL=https://skillhub.openeuler.org
          NEXT_PUBLIC_AUTH_TOKEN=dev-token
          EOF

      - name: Lint
        run: pnpm lint

      - name: Build
        run: pnpm build

      - name: Release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
        run: |
          npm config set loglevel warn
          if [ "${{ inputs.dry_run }}" = "true" ]; then
            npx semantic-release --dry-run
          else
            npx semantic-release
          fi

      - name: Push tags and sync to GitHub
        if: success() && inputs.dry_run != 'true'
        run: |
          echo "=== Pushing all tags to GitHub ==="
          git push origin --tags --force
          echo "=== Force pushing master to GitHub (sync from GitCode) ==="
          git push origin master --force
          echo "=== Remote tags ==="
          git ls-remote --tags origin | grep -v '\^{}' | sort -V