name: CI | Publish github repository package
on:
  push:
    branches: ['master']
  pull_request:
    branches: ['master']
  workflow_dispatch:
    inputs:
      conf_id:
        description: 'Conf id of the canonical engine github package repo (engine-ghpkg-<conf_id>)'
        required: false
        default: 'cyberia'
      message:
        description: 'Commit message for the canonical package build'
        required: false
        default: ''
permissions:
  contents: write
  packages: write
  id-token: write
jobs:
  pwa-microservices-template-ghpk:
    if: >-
      github.repository == 'underpostnet/pwa-microservices-template' &&
       (
         startsWith(github.event.head_commit.message, 'ci(package-pwa-microservices-template-ghpkg)')
         || 
         github.event_name == 'workflow_dispatch'
       )
    name: Update github repo package Jobs
    runs-on: ubuntu-latest
    container:
      image: quay.io/rockylinux/rockylinux:9
    permissions:
      contents: write
      packages: write
      id-token: write
    steps:
      - uses: actions/checkout@v7

      - name: Install required packages
        run: |
          dnf install -y sudo tar gzip bzip2 git
          dnf install -y curl --allowerasing

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

      - name: Install dependencies and set repo configuration
        run: |
          npm install
          node ./bin package --rename @underpostnet/underpost
          node ./bin package --set-repo underpostnet/pwa-microservices-template-ghpkg

      # Placed after the install because it resolves the payload through the CLI, which needs the
      # dependencies. The push that triggers this job carries the payload the engine propagated.
      - name: Capture commit message
        env:
          # Never interpolated into the script: the propagated payload is multi-line and a shell
          # expansion would corrupt entries holding `$` and execute anything in `$(...)`.
          PROPAGATED_MESSAGE: ${{ github.event.head_commit.message }}
        run: |
          LAST_COMMIT_MESSAGE=$(node ./bin cmt --propagate-msg 2>/dev/null || echo "")
          if [ -z "$LAST_COMMIT_MESSAGE" ]; then
            LAST_COMMIT_MESSAGE="update package"
          fi
          {
            echo "LAST_COMMIT_MESSAGE<<PROPAGATION_EOF"
            echo "$LAST_COMMIT_MESSAGE"
            echo "PROPAGATION_EOF"
          } >> $GITHUB_ENV
          echo "[INFO] Commit message: $LAST_COMMIT_MESSAGE"

      - name: Set git global credentials
        run: |
          git config --global --add safe.directory '*'
          git config --global credential.helper ""
          git config --global user.name 'underpostnet'
          git config --global user.email 'fcoverdugoa@underpost.net'

      - name: Clone and Push to github package repository
        env:
          GITHUB_TOKEN: ${{ secrets.GIT_AUTH_TOKEN }}
          GITHUB_USERNAME: ${{ github.repository_owner }}
        run: |
          node bin clone --bare underpostnet/pwa-microservices-template-ghpkg
          rm -rf ./.git
          mv ./pwa-microservices-template-ghpkg.git ./.git
          pwd
          git remote set-url origin git@github.com:underpostnet/pwa-microservices-template-ghpkg.git
          git init
          git config user.name 'underpostnet'
          git config user.email 'fcoverdugoa@underpost.net'
          git add .
          git status
          git commit -m "$LAST_COMMIT_MESSAGE"
          node bin push . underpostnet/pwa-microservices-template-ghpkg

  engine-ghpkg:
    if: github.repository == 'underpostnet/engine' && github.event_name == 'workflow_dispatch'
    name: Build engine ${{ github.event.inputs.conf_id || 'cyberia' }} ghpkg repository
    runs-on: ubuntu-latest
    container:
      image: quay.io/rockylinux/rockylinux:9
    permissions:
      contents: write
      packages: write
      id-token: write
    steps:
      - uses: actions/checkout@v7

      - name: Install required packages
        run: |
          dnf install -y sudo tar gzip bzip2 git
          dnf install -y curl --allowerasing

      - 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

      - name: Resolve canonical github package repo
        run: |
          CONF_ID="${{ github.event.inputs.conf_id }}"
          CONF_ID="${CONF_ID:-cyberia}"
          echo "CONF_ID=$CONF_ID" >> $GITHUB_ENV
          echo "GHPKG_REPO=engine-ghpkg-$CONF_ID" >> $GITHUB_ENV
          echo "PACKAGE_NAME=@underpostnet/$CONF_ID" >> $GITHUB_ENV
          echo "[INFO] Canonical github package repo: underpostnet/engine-ghpkg-$CONF_ID"

      - name: Capture commit message
        env:
          # Never interpolated into the script: the propagated payload is multi-line and a shell
          # expansion would corrupt entries holding `$` and execute anything in `$(...)`.
          PROPAGATED_MESSAGE: ${{ github.event.inputs.message }}
        run: |
          LAST_COMMIT_MESSAGE=$(node bin cmt --propagate-msg 2>/dev/null || echo "")
          if [ -z "$LAST_COMMIT_MESSAGE" ]; then
            LAST_COMMIT_MESSAGE="Update canonical github package repo"
          fi
          {
            echo "LAST_COMMIT_MESSAGE<<PROPAGATION_EOF"
            echo "$LAST_COMMIT_MESSAGE"
            echo "PROPAGATION_EOF"
          } >> $GITHUB_ENV
          echo "[INFO] Commit message: $LAST_COMMIT_MESSAGE"

      - name: Set git global credentials
        run: |
          git config --global --add safe.directory '*'
          git config --global credential.helper ""
          git config --global user.name 'underpostnet'
          git config --global user.email 'development@underpost.net'

      - name: Mirror engine-$CONF_ID and Push to canonical github package repository
        env:
          GITHUB_TOKEN: ${{ secrets.GIT_AUTH_TOKEN }}
          GITHUB_USERNAME: ${{ github.repository_owner }}
        run: |
          cd .. && node engine/bin clone underpostnet/engine-$CONF_ID
          node engine/bin clone --bare underpostnet/$GHPKG_REPO
          cd engine-$CONF_ID
          rm -rf ./.git
          mv ../$GHPKG_REPO.git ./.git
          node ../engine/bin package --rename "$PACKAGE_NAME"
          node ../engine/bin package --set-repo underpostnet/$GHPKG_REPO
          git init
          git config user.name 'underpostnet'
          git config user.email 'development@underpost.net'
          git add .
          if git diff --cached --quiet; then
            echo "[INFO] No changes to publish; skipping commit and push."
          else
            git commit -m "$LAST_COMMIT_MESSAGE"
            node ../engine/bin push . underpostnet/$GHPKG_REPO
          fi
