name: Go CI/CD with goctl and Swagger

on:
  # release:
  #   types: [published]
  push:
    branches:
      - develop

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Install goctl
        run: |
          curl -L https://github.com/zeromicro/go-zero/releases/download/tools%2Fgoctl%2Fv1.7.2/goctl-v1.7.2-linux-amd64.tar.gz -o goctl-v1.7.2-linux-amd64.tar.gz
          tar -xvzf goctl-v1.7.2-linux-amd64.tar.gz
          chmod +x goctl
          sudo mv goctl /usr/local/bin/goctl
          goctl --version

      - name: Install goctl-swagger
        run: |
          curl -L https://github.com/tensionc/goctl-swagger/releases/download/v1.0.1/goctl-swagger-v1.0.1-linux-amd64.tar.gz -o goctl-swagger.tar.gz
          tar -xvzf goctl-swagger.tar.gz
          chmod +x goctl-swagger
          sudo mv goctl-swagger /usr/local/bin/

      - name: Generate Swagger file
        run: |
          mkdir -p swagger
          goctl api plugin -plugin goctl-swagger='swagger -filename common.json -pack Response -response "[{\"name\":\"code\",\"type\":\"integer\",\"description\":\"状态码\"},{\"name\":\"msg\",\"type\":\"string\",\"description\":\"消息\"},{\"name\":\"data\",\"type\":\"object\",\"description\":\"数据\",\"is_data\":true}]";' -api ./apis/swagger_common.api -dir ./swagger
          goctl api plugin -plugin goctl-swagger='swagger -filename user.json -pack Response -response "[{\"name\":\"code\",\"type\":\"integer\",\"description\":\"状态码\"},{\"name\":\"msg\",\"type\":\"string\",\"description\":\"消息\"},{\"name\":\"data\",\"type\":\"object\",\"description\":\"数据\",\"is_data\":true}]";' -api ./apis/swagger_user.api -dir ./swagger
          goctl api plugin -plugin goctl-swagger='swagger -filename admin.json -pack Response -response "[{\"name\":\"code\",\"type\":\"integer\",\"description\":\"状态码\"},{\"name\":\"msg\",\"type\":\"string\",\"description\":\"消息\"},{\"name\":\"data\",\"type\":\"object\",\"description\":\"数据\",\"is_data\":true}]";' -api ./apis/swagger_admin.api -dir ./swagger
          goctl api plugin -plugin goctl-swagger='swagger -filename ppanel.json -pack Response -response "[{\"name\":\"code\",\"type\":\"integer\",\"description\":\"状态码\"},{\"name\":\"msg\",\"type\":\"string\",\"description\":\"消息\"},{\"name\":\"data\",\"type\":\"object\",\"description\":\"数据\",\"is_data\":true}]";' -api ppanel.api -dir ./swagger
          goctl api plugin -plugin goctl-swagger='swagger -filename node.json -pack Response -response "[{\"name\":\"code\",\"type\":\"integer\",\"description\":\"状态码\"},{\"name\":\"msg\",\"type\":\"string\",\"description\":\"消息\"},{\"name\":\"data\",\"type\":\"object\",\"description\":\"数据\",\"is_data\":true}]";' -api ./apis/swagger_node.api -dir ./swagger
          

      - name: Verify Swagger file
        run: |
          test -f ./swagger/common.json
          test -f ./swagger/user.json
          test -f ./swagger/admin.json

      - name: Checkout target repository
        uses: actions/checkout@v4
        with:
          repository: perfect-panel/ppanel-docs
          token: ${{ secrets.GH_TOKEN }}
          path: ppanel-docs
          persist-credentials: true

      - name: Verify or create public/swagger directory
        run: |
          mkdir -p ./ppanel-docs/public/swagger

      - name: Copy Swagger files
        run: |
          cp -rf swagger/* ppanel-docs/public/swagger
          cd ppanel-docs

      - name: Check for file changes
        run: |
          cd ppanel-docs
          git add .
          git status
          if [ "$(git status --porcelain)" ]; then
          echo "Changes detected in the doc repository."
          git config user.name "GitHub Actions"
          git config user.email "actions@ppanel.dev"
          git commit -m "Update Swagger files"
          git push
          else
          echo "No changes detected."
          exit 0
          fi

