name: AppFlowy-Cloud Integrations

on:
  push:
    branches: [ main ]
  pull_request:
    types: [ opened, synchronize, reopened ]
    branches: [ main ]

env:
  SQLX_VERSION: 0.7.1
  SQLX_FEATURES: "rustls,postgres"

jobs:
  test:
    name: Docker
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
        with:
          workspaces: |
            AppFlowy-Cloud

      - name: Copy and rename dev.env to .env
        run: cp dev.env .env

      - name: Replace values in .env
        run: |
          # log level
          sed -i 's|RUST_LOG=.*|RUST_LOG=trace|' .env
          
          sed -i 's/GOTRUE_SMTP_USER=.*/GOTRUE_SMTP_USER=${{ secrets.GOTRUE_SMTP_USER }}/' .env
          sed -i 's/GOTRUE_SMTP_PASS=.*/GOTRUE_SMTP_PASS=${{ secrets.GOTRUE_SMTP_PASS }}/' .env
          sed -i 's/GOTRUE_SMTP_ADMIN_EMAIL=.*/GOTRUE_SMTP_ADMIN_EMAIL=${{ secrets.GOTRUE_SMTP_ADMIN_EMAIL }}/' .env
          sed -i 's/GOTRUE_EXTERNAL_GOOGLE_ENABLED=.*/GOTRUE_EXTERNAL_GOOGLE_ENABLED=true/' .env

      - name: Disable appflowyinc images
        run: |
          sed -i '/image: appflowyinc\/appflowy_cloud:/d' docker-compose.yml
          sed -i '/image: appflowyinc\/admin_frontend:/d' docker-compose.yml

      - name: Run Docker-Compose
        run: |
          docker compose up -d 

      - name: Run tests
        run: |
          cargo install sqlx-cli --version=${{ env.SQLX_VERSION }} --features ${{ env.SQLX_FEATURES }} --no-default-features --locked
          RUST_LOG=debug cargo test

