name: CI

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

env:
  NPM_TOKEN: ${{ github.actor == 'dependabot[bot]' && secrets.NPM_DEPLOYER_TOKEN || secrets.NPM_PUBLISHER_TOKEN }}
  GH_TOKEN: ${{ secrets.GH_TOKEN }}
  CI_REPO_NAME: ${{ github.repository }}
  CI_COMMIT_ID: ${{ github.sha }}

jobs:
  test:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:14-alpine
        env:
          POSTGRES_USER: developer
          POSTGRES_PASSWORD: development_secret
          POSTGRES_DB: programexecutor
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
        ports:
          - 5432:5432

    steps:
      - name: 'Install Cloud SDK'
        uses: google-github-actions/setup-gcloud@v2.1.4
        with:
          install_components: 'beta,pubsub-emulator'
      - name: 'start pubsub emulator'
        run: |
          gcloud beta emulators pubsub start --project=some-project-id --host-port=0.0.0.0:8085 &
      - name: Check out repository
        uses: actions/checkout@v4
      - name: Use Node 24.12.0 version
        uses: actions/setup-node@v3
        with:
          node-version: "24.12.0"
      - name: Set NPM token
        run: npm config set '//registry.npmjs.org/:_authToken' "${{ env.NPM_TOKEN }}"
      - name: npm dependencies
        run: npm install
      - name: npm test
        run: npm test
        env:
          POSTGRES_USER: developer
          POSTGRES_HOST: localhost
          POSTGRES_PASSWORD: development_secret
          POSTGRES_PORT: 5432
          POSTGRES_DATABASE: programexecutor
          PUBSUB_EMULATOR_HOST: 0.0.0.0:8085
          PUBSUB_PROJECT_ID: some-project-id
          PUBSUB_TIMEOUT: 2000

  dependabot-auto-merge:
    if: ${{ vars.DEPENDABOT_AUTO_MERGE == 'true' }}

    needs: test
    runs-on: ubuntu-latest

    permissions:
      pull-requests: write
      contents: write
    
    steps:
      - uses: fastify/github-action-merge-dependabot@v3
        with:
          github-token: ${{ secrets.DEPENDABOT_AUTOMERGE_TOKEN }}
          target-development: 'minor'
          target-production: 'minor'
  deploy:
    name: deploy
    needs: [test]
    runs-on: ubuntu-latest
    if: ${{ github.ref == 'refs/heads/main' }}
    permissions:
      contents: write
      issues: write
    steps:
      - name: Check out repository
        uses: actions/checkout@v3
      - name: Use newest Node version
        uses: actions/setup-node@v3
        with:
          node-version: "lts/*"
      - name: Set NPM token
        run: npm config set '//registry.npmjs.org/:_authToken' "${{ env.NPM_TOKEN }}"
      - name: npm dependencies
        run: npm install
      - name: Cache node_modules
        id: cache_node_modules
        uses: actions/cache@v3
        with:
          path: node_modules
          key: node_modules-${{ hashFiles('**/package-lock.json') }}
      - name: Publish package
        run: CI=true npm run semantic-release
