name: Test

on:
  pull_request:
  push:
    branches:
      - master
    tags: ["*"]

jobs:
  test:
    strategy:
      matrix:
        go-version: [1.22.x]
    runs-on: ubuntu-20.04
    services:
      postgres:
        image: postgres:15
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: root
          POSTGRES_DB: postgres
        ports:
          - 5432:5432
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
    steps:
      - name: Install Go
        uses: actions/setup-go@v2
        with:
          go-version: ${{ matrix.go-version }}
      - name: Checkout code
        uses: actions/checkout@v2
        with:
          skip-unshallow: "true"
      - name: Check gofmt
        run: |
          set -x

          if [ ! -z $(gofmt -l .) ]
          then
            echo 'Make sure to run "gofmt -s -w ." before commit!' && exit 1
          fi
      - name: Check go vet
        run: |
          set -x
          go vet ./...
      - name: Run static check
        run: |
          set -x
          go install honnef.co/go/tools/cmd/staticcheck@latest
          go install github.com/nishanths/exhaustive/cmd/exhaustive@latest
          make static
      - name: Check gosec
        run: |
          set -x
          go install github.com/securego/gosec/v2/cmd/gosec@latest
          make sec
      - name: Init Database
        run: psql -f hack/init_postgres.sql postgresql://postgres:root@localhost:5432/postgres
      - name: Run migrations
        run: make migrate_dev
      - name: Lint and test
        run: make test
      - name: Cleanup coverage
        run: |
          set -x

          # since Go 1.20 these source files need to be deleted from the
          # coverage profile as they contain legacy or untestable code (like
          # `main` package)

          sed -i '/^github.com\/supabase\/auth\/client/d' coverage.out
          sed -i '/^github.com\/supabase\/auth\/cmd/d' coverage.out
          sed -i '/^github.com\/supabase\/auth\/docs/d' coverage.out
          sed -i '/^github.com\/supabase\/auth\/main/d' coverage.out

      - uses: shogo82148/actions-goveralls@v1
        with:
          path-to-profile: coverage.out