name: Run unit tests
on:
  push:
    branches:
      - main
  pull_request:
concurrency:
  group: ${{ github.ref }}
  cancel-in-progress: true
jobs:
  unit_tests:
    runs-on: ubuntu-latest
    services: &services
      postgres:
        image: postgres
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
        ports:
          - 5432
    steps:
      - uses: actions/checkout@v4
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          ref: ${{ github.head_ref }} # Checkout the actual branch, not the result if merged into the base
      - uses: ruby/setup-ruby@v1.220.0
      - run: bundle install
      - &postgres_wait
        name: Wait until Postgres is ready to accept connections
        run: |
          apt-get update && apt-get install -f -y postgresql-client
          until pg_isready -h postgres -U postgres -d postgres
          do
            sleep 1
            echo "Trying again"
          done
      - name: Load schema and run unit tests
        run: |
          bundle exec rake db:schema:load
          bundle exec rspec
        env:
          RAILS_ENV: test
          DATABASE_URL: postgres://postgres:postgres@postgres:5432/postgres
      - name: Get all migrations added
        id: changed-migration-files
        uses: tj-actions/changed-files@v45
        with:
          files: |
            db/migrate/**.rb
      - name: Redo all migrations and check there are no schema changes
        if: steps.changed-migration-files.outputs.any_changed == 'true'
        env:
          ALL_CHANGED_FILES: ${{ steps.changed-migration-files.outputs.all_changed_files }}
          RAILS_ENV: test
          DATABASE_URL: postgres://postgres:postgres@postgres:5432/postgres
        run: |
          echo ${#ALL_CHANGED_FILES[@]} migrations changed:
          for file in ${ALL_CHANGED_FILES}; do
            echo "$file"
          done

          bundle exec rake db:migrate:redo STEP=${#ALL_CHANGED_FILES[@]}
          git diff --exit-code db/schema.rb
      - name: Clean up containers generated by this flow
        if: failure()
        run: docker ps --filter network=$JOB_CONTAINER_NAME-$GITHUB_JOB-network --filter name=$JOB_CONTAINER_NAME-* --format "{{.ID}}" | xargs docker rm -f
  rubocop:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
      - uses: ruby/setup-ruby@v1.220.0
      - run: bundle install
      - run: bundle exec rubocop --force-exclusion --parallel
  check-licenses:
    runs-on: ubuntu-latest
    steps:
        - uses: actions/checkout@v4
          with:
            token: ${{ secrets.GITHUB_TOKEN }}
        - uses: ruby/setup-ruby@v1.220.0
        - name: Install project dependencies
          run: bundle install --jobs `getconf _NPROCESSORS_ONLN`
        - name: Run license finder
          run: license_finder
  copyright_notice:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          token: ${{ secrets.ACTIONS_TOKEN }}
          ref: ${{ github.head_ref }}
      - uses: VinnyBabuManjaly/copyright-action@v1.0.0
        with:
          CopyrightString: '# Copyright (C) 2024-2025 LibreWeddingPlanner contributors\n\n'
          FileType: '.rb'
          Path: 'app/, config/, db/, spec/'
          IgnorePath: 'db'
      - uses: VinnyBabuManjaly/copyright-action@v1.0.0
        with:
          CopyrightString: '<%# Copyright (C) 2024-2025 LibreWeddingPlanner contributors %>\n\n'
          FileType: '.erb'
          Path: 'app/'
      - name: Commit changes
        run: |
          git config --local user.email "bustikiller@bustikiller.com"
          git config --local user.name "Manuel Bustillo"
          git add .

          if [ -n "$(git status --porcelain)" ]; then
            echo "there are changes";
            git commit -m "Add copyright notice"
            git push
          else
            echo "no changes";
          fi

  build-static-assets:
    runs-on: ubuntu-latest
    timeout-minutes: 30
    needs:
      - unit_tests
      - rubocop
      - check-licenses
      - copyright_notice
    steps:
      - uses: actions/checkout@v4
        with:
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      
      - name: Login to the private Docker registry
        uses: docker/login-action@v3
        with:
          registry: ${{ secrets.PRIVATE_REGISTRY_HOST }}
          username: ${{ secrets.PRIVATE_REGISTRY_USERNAME }}
          password: ${{ secrets.PRIVATE_REGISTRY_TOKEN }}

      - name: Build and push intermediate stages (build)
        uses: docker/build-push-action@v6
        with:
          context: .
          target: build
          push: ${{ github.ref == 'refs/heads/main' }}
          tags: ${{ secrets.PRIVATE_REGISTRY_HOST }}/${{ env.GITHUB_REPOSITORY }}:build
          cache-from: type=registry,ref=${{ secrets.PRIVATE_REGISTRY_HOST }}/${{ env.GITHUB_REPOSITORY }}:build
          cache-to: type=inline

      - name: Build and push (final)
        uses: docker/build-push-action@v6
        with:
          context: .
          push: ${{ github.ref == 'refs/heads/main' }}
          tags: ${{ secrets.PRIVATE_REGISTRY_HOST }}/${{ env.GITHUB_REPOSITORY }}:latest
          cache-from: type=registry,ref=${{ secrets.PRIVATE_REGISTRY_HOST }}/${{ env.GITHUB_REPOSITORY }}:latest
          cache-to: type=inline