name: CI | Publish npm repository package
on:
  push:
    branches: ['master']
  pull_request:
    branches: ['master']
  workflow_dispatch:
    inputs:
      change_summary:
        description: 'Change summary to commit and forward: its JSON, or a plain message'
        required: false
        default: ''
      deploy_conf_id:
        description: 'Engine conf ID to dispatch after build'
        required: false
        default: ''
      deploy_type:
        description: 'Deployment type for the engine conf CD (sync-and-deploy or init)'
        required: false
        default: ''
permissions:
  contents: write
  packages: write
  id-token: write
jobs:
  pwa-microservices-template:
    if: |
      (github.repository == 'underpostnet/engine' || github.repository == 'underpost/engine')
      && (
        startsWith(github.event.head_commit.message, 'ci(package-pwa-microservices-template')
        || github.event_name == 'workflow_dispatch'
      )
    name: Update npm repo package Jobs
    runs-on: ubuntu-latest
    container:
      image: quay.io/rockylinux/rockylinux:9
    permissions:
      contents: write
      packages: write
      id-token: write
    steps:
      # Install git first. Checkout without git downloads an archive that has no .git.
      - name: Install required packages
        run: |
          dnf install -y sudo tar gzip bzip2 git
          dnf install -y curl --allowerasing

      - name: Clone repository
        uses: actions/checkout@v7

      # The checkout belongs to another user than the container root. Git refuses it without this.
      - name: Trust the checkout in git
        run: git config --global --add safe.directory '*'

      - name: Install Node.js
        run: |
          curl -fsSL https://rpm.nodesource.com/setup_24.x | bash -
          dnf install nodejs -y

      - name: Install dependencies
        run: npm install

      # Placed after the install because it resolves the summary through the CLI, which needs the
      # dependencies. The dispatch input wins; a push carries the summary in its commit trailer.
      - name: Resolve the change summary
        env:
          # Never interpolated into the script: a change summary is free text, which a shell
          # expansion would corrupt or execute.
          CHANGE_SUMMARY: ${{ github.event.inputs.change_summary || github.event.head_commit.message }}
        run: |
          CHANGE_SUMMARY=$(node bin release --change-summary --fallback "Update repository")
          CHANGE_MESSAGE=$(CHANGE_SUMMARY="$CHANGE_SUMMARY" node bin release --change-summary --format message)
          {
            echo "CHANGE_SUMMARY=$CHANGE_SUMMARY"
            echo "CHANGE_MESSAGE<<CHANGE_SUMMARY_EOF"
            echo "$CHANGE_MESSAGE"
            echo "CHANGE_SUMMARY_EOF"
          } >> "$GITHUB_ENV"
          echo "[INFO] Change summary: $CHANGE_SUMMARY"

      - name: Set git global credentials
        env:
          GITHUB_USERNAME: ${{ github.repository_owner }}
          GITHUB_EMAIL: ${{ secrets.GIT_EMAIL }}
        run: |
          : "${GITHUB_EMAIL:?the GIT_EMAIL Actions secret is not set}"
          git config --global credential.helper ""
          git config --global user.name "$GITHUB_USERNAME"
          git config --global user.email "$GITHUB_EMAIL"

      - name: Clone github package repository
        env:
          GITHUB_TOKEN: ${{ secrets.GIT_AUTH_TOKEN }}
          GITHUB_USERNAME: ${{ github.repository_owner }}
        run: |
          cd .. && node engine/bin clone underpostnet/pwa-microservices-template
          cd engine
          npm run build:template
          cd ../pwa-microservices-template
          git remote set-url origin git@github.com:underpostnet/pwa-microservices-template.git
          git add .
          if git diff --cached --quiet; then
            echo "[INFO] No template changes to publish; skipping commit and push."
          else
            node ../engine/bin cmt . ci package-pwa-microservices-template-ghpkg "$CHANGE_MESSAGE"
            node ../engine/bin push . underpostnet/pwa-microservices-template
          fi

      - name: Dispatch all engine CIs
        env:
          # Never interpolated into the script: a dispatch input is free text.
          DEPLOY_CONF_ID: ${{ github.event.inputs.deploy_conf_id }}
          DEPLOY_TYPE: ${{ github.event.inputs.deploy_type }}
          GITHUB_TOKEN: ${{ secrets.GIT_AUTH_TOKEN }}
        run: |
          for ENGINE in engine-core engine-prototype engine-cyberia engine-lampp engine-test; do
            SYNC="false"
            DT=""
            if [ "$ENGINE" = "$DEPLOY_CONF_ID" ]; then
              SYNC="true"
              DT="$DEPLOY_TYPE"
            fi
            echo "[INFO] Dispatching ${ENGINE}.ci.yml (sync=$SYNC, deploy_type=$DT)"
            # Every engine-* repo commits the same change summary.
            node bin github dispatch underpostnet/engine "${ENGINE}.ci.yml" --ref master \
              --input sync="$SYNC" --input deploy_type="$DT" --input change_summary="$CHANGE_SUMMARY"
          done
