name: Elixir CI

on:
  push:
    branches: [ "main", "dev" ]
    tags: ['v*']
  pull_request:
    branches: [ "main", "dev" ]

permissions:
  contents: read

jobs:
  build:

    name: Build and test
    runs-on: ubuntu-latest

    services:
      db:
        image: postgres:15
        ports: ['5432:5432']
        env:
          POSTGRES_PASSWORD: claper
          POSTGRES_USER: claper
          POSTGRES_DB: claper
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
    env:
      ImageOS: ubuntu20
      MIX_ENV: test
      DATABASE_URL: postgresql://claper:claper@localhost:5432/claper
      SECRET_KEY_BASE: QMQE4ypfy0IC1LZI/fygZNvXHPjLslnr49EE7ftcL1wgAC0MwMLdKCVJyrvXPu8z
      BASE_URL: http://localhost:4000

    steps:
    - uses: actions/checkout@v4
      with:
        fetch-depth: 0
    - name: Set up Elixir
      uses: erlef/setup-beam@v1
      with:
        elixir-version: '1.16.2'
        otp-version: '26'
    - name: Restore dependencies cache
      uses: actions/cache@v3
      with:
        path: deps
        key: ${{ runner.os }}-mix-${{ hashFiles('**/mix.lock') }}
        restore-keys: ${{ runner.os }}-mix-
    - name: Install dependencies
      run: mix deps.get
    - name: Check Formatting
      run: mix format --check-formatted
    - name: Check Credo Warnings
      run: mix credo diff --from-git-merge-base origin/main
    - name: Run tests
      run: mix test