name: Fetch code

on:
  workflow_call:
    inputs:
      tag:
        description: 'Tag to deploy'
        required: false
        type: string
        default: ''
      artifact:
        description: 'Artifact name'
        required: false
        type: string
        default: ''
jobs:
  fetch:
    name: Checkout code
    runs-on: ubuntu-latest

    steps:

      # Checkout the source
      - name: Checkout source
        uses: actions/checkout@v4
        with:
          ref: ${{ inputs.tag }}

      # Setup file permissions
      - name: File permissions
        working-directory: ./
        run: |
          chmod +x tools/post-install-cmd.sh

      # Set up PHP to our liking for use with WordPress, and to match the live environment.
      - name: Setup PHP environment
        uses: shivammathur/setup-php@v2
        with:
          php-version: '8.2'
          tools: composer:v2
          ini-values: memory_limit=512M
          extensions: gd, zip
        env:
          COMPOSER_AUTH_JSON: |
            {
              "http-basic": {
                "connect.advancedcustomfields.com": {
                  "username": "${{ secrets.ACF_USERNAME }}",
                  "password": "${{ secrets.ACF_PASSWORD }}"
                }
              }
            }
      # Create cache locations that work across action containers to speed up testing if and when possible.
      - name: Cache composer dependencies
        uses: actions/cache@v4
        with:
          path: ~/.composer/cache
          key: dependencies-composer-${{ hashFiles('composer.lock') }}

      - name: Cache node dependencies
        uses: actions/cache@v4
        with:
          path: ~/.npm
          key: dependencies-npm-${{ hashFiles('package-lock.json') }}

      # Setup SSH keys for accessing private repositories
      - name: SSH Key configuration
        uses: webfactory/ssh-agent@v0.9.0
        with:
          ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}

      # If the project includes any composer dependencies, install them now.
      - name: Install Composer dependencies
        working-directory: ./
        env:
          COMPOSER_AUTH: ${{ secrets.COMPOSER_AUTH }}
        run: 'composer validate && composer install --no-dev --no-progress'

      # Install translation files
      - name: Install translations via Composer
        working-directory: ./
        env:
          COMPOSER_AUTH: ${{ secrets.COMPOSER_AUTH }}
        run: 'composer run get-translations'

      # Cleanup removes files not wanted in the final production build.
      - name: Remove unwanted files
        working-directory: ./
        run: |
          rm -rf .github/
          rm -rf .dependabot/
          rm -rf .env.example
          rm -rf .gitignore
          rm -rf documentation/
          find . -name 'deprecated' -type d -prune|grep ninja|xargs rm -rf

      - name: Package repository for sharing between jobs
        working-directory: ./
        run: |
          touch code-artifact.tar.gz
          tar -czf code-artifact.tar.gz --exclude=code-artifact.tar.gz --exclude=.git --exclude=.github .

      - name: Create shared artifact
        uses: actions/upload-artifact@v4
        with:
          name: code-artifact
          path: code-artifact.tar.gz
          retention-days: 1
          overwrite: true