name: Elixir CI

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  quality:
    name: Build and test
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgis/postgis:17-3.5
        env:
          POSTGRES_PASSWORD: postgres
          POSTGRES_USER: postgres
          POSTGRES_DB: aprsme_test
        ports:
          - 5432:5432
        options: >-
          --health-cmd "pg_isready -U postgres"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
    env:
      MIX_ENV: test

    steps:
      - name: Configure git for submodules
        run: |
          git config --global url."https://oauth2:${{ secrets.FORGEJO_TOKEN }}@git.mcintire.me/".insteadOf "ssh://git@git.mcintire.me:2222/"

      - name: Checkout
        uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Set up Elixir
        uses: http://forgejo:3000/graham/setup-beam@v1
        with:
          version-type: strict
          elixir-version: '1.20.2'
          otp-version: '29.0'

      - name: Restore dependency and build caches
        uses: https://code.forgejo.org/actions/cache@v4
        with:
          path: |
            deps
            _build
          key: ${{ runner.os }}-mix-${{ hashFiles('**/mix.lock') }}
          restore-keys: |
            ${{ runner.os }}-mix-

      - name: Install Hex and Rebar
        run: |
          mix local.hex --force
          mix local.rebar --force

      - name: Fetch dependencies
        run: mix deps.get

      - name: Check formatting
        run: mix format --check-formatted

      - name: Compile (warnings as errors)
        run: mix compile --warnings-as-errors

      - name: Run Credo
        run: mix credo --strict

      - name: Run tests
        run: mix test

      - name: Run Sobelow
        run: mix sobelow --config

      - name: Run Hex Audit
        run: mix hex.audit

  dialyzer:
    name: Dialyzer
    runs-on: ubuntu-latest
    env:
      MIX_ENV: dev

    steps:
      - name: Configure git for submodules
        run: |
          git config --global url."https://oauth2:${{ secrets.FORGEJO_TOKEN }}@git.mcintire.me/".insteadOf "ssh://git@git.mcintire.me:2222/"

      - name: Checkout
        uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Set up Elixir
        uses: http://forgejo:3000/graham/setup-beam@v1
        with:
          version-type: strict
          elixir-version: '1.20.2'
          otp-version: '29.0'

      - name: Restore dependency and build caches
        uses: https://code.forgejo.org/actions/cache@v4
        with:
          path: |
            deps
            _build
          key: ${{ runner.os }}-dialyzer-${{ hashFiles('**/mix.lock') }}
          restore-keys: |
            ${{ runner.os }}-dialyzer-

      - name: Install Hex and Rebar
        run: |
          mix local.hex --force
          mix local.rebar --force

      - name: Fetch dependencies
        run: mix deps.get

      - name: Run Dialyzer
        run: mix dialyzer

  build-and-push:
    name: Build and Push Docker Image
    runs-on: ubuntu-latest
    needs: [quality, dialyzer]
    if: github.event_name == 'push'

    steps:
      - name: Configure git for submodules
        run: |
          git config --global url."https://oauth2:${{ secrets.FORGEJO_TOKEN }}@git.mcintire.me/".insteadOf "ssh://git@git.mcintire.me:2222/"

      - name: Checkout code
        uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Generate image tag
        id: tag
        run: |
          TIMESTAMP=$(date +%s)
          SHORT_SHA=$(git rev-parse --short=7 HEAD | cut -c1-7)
          TAG="main-${TIMESTAMP}-${SHORT_SHA}"
          echo "tag=${TAG}" >> $GITHUB_OUTPUT

      - name: Install Docker CLI
        run: |
          apt-get update && apt-get install -y docker.io

      - name: Build and Push
        run: |
          echo "${{ secrets.FORGEJO_TOKEN }}" | docker login git.mcintire.me -u graham --password-stdin
          docker build \
            -t "git.mcintire.me/graham/aprs.me:${{ steps.tag.outputs.tag }}" \
            -t "git.mcintire.me/graham/aprs.me:latest" \
            .
          docker push "git.mcintire.me/graham/aprs.me:${{ steps.tag.outputs.tag }}"
          docker push "git.mcintire.me/graham/aprs.me:latest"
