name: checks

on:
  push:
    branches:
      - master
    paths-ignore:
      - '**.md'
  pull_request:
    types:
      - opened
      - synchronize
    paths-ignore:
      - '**.md'


env:
  GO_VERSION: "1.20"

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  tests:
    name: tests
    runs-on: ubuntu-latest
    timeout-minutes: 40
    strategy:
      matrix:
        pg_version: [9.6, 10, 11, 12, 13, 14, 15, 16]

    services:
      postgres:
        image: postgres:${{ matrix.pg_version }}
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: booktown
        ports:
          - 5432:5432
        options: >-
            --health-cmd pg_isready
            --health-interval 10s
            --health-timeout 5s
            --health-retries 5
    steps:
      - name: Install latest Postgres client
        run: |
          sudo rm -f /etc/apt/sources.list.d/pgdg.list
          curl --silent https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add
          echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.list
          sudo apt-get update && sudo apt-get install -y postgresql-client-16

      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - uses: actions/setup-go@v3
        with:
          go-version: ${{ env.GO_VERSION }}
      - run: go mod download
      - run: make test
        env:
          MallocNanoZone: 0 # https://github.com/golang/go/issues/49138
          PGHOST: localhost
          PGUSER: postgres
          PGPASSWORD: postgres
          PGDATABASE: booktown

  tests-windows:
    runs-on: windows-latest
    timeout-minutes: 30

    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - uses: actions/setup-go@v3
        with:
          go-version: ${{ env.GO_VERSION }}
      - run: go mod download
      - run: make test

  lint:
    runs-on: ubuntu-latest
    timeout-minutes: 10

    steps:
      - uses: actions/setup-go@v3
        with:
          go-version: ${{ env.GO_VERSION }}

      - uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: golangci-lint
        uses: golangci/golangci-lint-action@v3
        with:
          version: v1.51.2

  fmt:
    name: fmt
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - uses: actions/setup-go@v3
        with:
          go-version: ${{ env.GO_VERSION }}
      - run: go mod download
      - run: script/check_formatting.sh
