name: Check Bazel/Erlang.mk Equivalence
on:
  workflow_call:
    inputs:
      ref:
        required: true
        type: string
      erlang_version:
        required: true
        type: string
      elixir_version:
        required: true
        type: string
      project_version:
        required: true
        type: string
  workflow_dispatch:
    inputs:
      erlang_version:
        description: 'OTP version to build with'
        required: true
        default: "26.2"
      elixir_version:
        description: 'Elixir version to build with'
        required: true
        default: "1.15"
      project_version:
        description: 'PROJECT_VERSION used for make'
        required: true
        default: "4.0.0"
env:
  erlang_version: ${{ inputs.erlang_version || github.event.inputs.erlang_version }}
  elixir_version: ${{ inputs.elixir_version || github.event.inputs.elixir_version }}
  VERSION: ${{ inputs.project_version || github.event.inputs.project_version }}
  PLUGINS: amqp10_common amqp10_client rabbitmq_amqp1_0 rabbitmq_auth_backend_cache rabbitmq_auth_backend_http rabbitmq_auth_backend_ldap rabbitmq_auth_backend_oauth2 rabbitmq_auth_mechanism_ssl rabbitmq_consistent_hash_exchange rabbitmq_event_exchange rabbitmq_federation rabbitmq_jms_topic_exchange rabbitmq_mqtt rabbitmq_random_exchange rabbitmq_recent_history_exchange rabbitmq_sharding rabbitmq_shovel rabbitmq_stomp rabbitmq_stream rabbitmq_trust_store rabbitmq_web_dispatch rabbitmq_management_agent rabbitmq_management rabbitmq_prometheus rabbitmq_federation_management rabbitmq_shovel_management rabbitmq_stream_management rabbitmq_top rabbitmq_tracing rabbitmq_web_mqtt rabbitmq_web_mqtt_examples rabbitmq_web_stomp rabbitmq_web_stomp_examples rabbitmq_aws rabbitmq_peer_discovery_common rabbitmq_peer_discovery_aws rabbitmq_peer_discovery_k8s rabbitmq_peer_discovery_consul rabbitmq_peer_discovery_etcd
  EXTRA_PLUGINS: accept amqp_client aten base64url cowboy cowlib credentials_obfuscation cuttlefish eetcd enough gen_batch_server getopt gun jose observer_cli osiris prometheus quantile_estimator ra ranch recon redbug seshat stdout_formatter syslog sysmon_handler systemd thoas
jobs:
  build-with-bazel:
    name: bazel build package-generic-unix.tar.xz
    runs-on: ubuntu-latest
    timeout-minutes: 15
    steps:
    - name: CHECKOUT REPOSITORY
      uses: actions/checkout@v4
      with:
        ref: ${{ inputs.ref || github.ref }}
    - name: CONFIGURE ERLANG
      uses: erlef/setup-beam@v1.17
      with:
        otp-version: ${{ env.erlang_version }}
        elixir-version: ${{ env.elixir_version }}
    - name: CONFIGURE BAZEL
      run: |
        cat << EOF >> user.bazelrc
          build --disk_cache=
          build --color=yes
        EOF
    - name: BUILD package-generic-unix.tar.xz
      run: |
        bazelisk build //:package-generic-unix
    - name: RESOLVE ARCHIVES_DIR
      run: |
        echo "archives_dir=$(readlink -f bazel-bin)" >> $GITHUB_ENV
    - name: UPLOAD package-generic-unix.tar.xz
      uses: actions/upload-artifact@v4.3.2
      with:
        name: bazel-package-generic-unix-${{ env.VERSION }}.tar.xz
        path: ${{ env.archives_dir }}/package-generic-unix.tar.xz
        if-no-files-found: error

  build-with-make:
    name: make package-generic-unix.tar.xz
    runs-on: ubuntu-latest
    timeout-minutes: 15
    steps:
    - name: CHECKOUT REPOSITORY
      uses: actions/checkout@v4
      with:
        path: rabbitmq
        ref: ${{ inputs.ref || github.ref }}
    - name: CONFIGURE ERLANG
      uses: erlef/setup-beam@v1.17
      with:
        otp-version: ${{ env.erlang_version }}
        elixir-version: ${{ env.elixir_version }}
    - name: BUILD package-generic-unix.tar.xz
      env:
        MAKE: make
      run: |
        $MAKE -C rabbitmq \
          source-dist \
          PACKAGES_DIR="$PWD/PACKAGES" \
          PLUGINS="$PLUGINS" \
          PROJECT_VERSION="$VERSION"
        $MAKE -C rabbitmq/packaging \
          package-generic-unix \
          PACKAGES_DIR="$PWD/PACKAGES" \
          VERSION="$VERSION"
    - name: UPLOAD package-generic-unix.tar.xz
      uses: actions/upload-artifact@v4.3.2
      with:
        name: make-package-generic-unix-${{ env.VERSION }}.tar.xz
        path: PACKAGES/rabbitmq-server-generic-unix-*.tar.xz
        if-no-files-found: error

  compare:
    needs:
    - build-with-bazel
    - build-with-make
    name: Compare package-generic-unix.tar.xz
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
    - name: CHECKOUT REPOSITORY
      uses: actions/checkout@v4
      with:
        path: rabbitmq-server
        ref: ${{ inputs.ref || github.ref }}
    - name: CONFIGURE ERLANG
      uses: erlef/setup-beam@v1.17
      with:
        otp-version: ${{ env.erlang_version }}
        elixir-version: ${{ env.elixir_version }}
    - name: DOWNLOAD bazel-package-generic-unix.tar.xz
      uses: actions/download-artifact@v4
      with:
        name: bazel-package-generic-unix-${{ env.VERSION }}.tar.xz
    - name: DOWNLOAD make-package-generic-unix.tar.xz
      uses: actions/download-artifact@v4
      with:
        name: make-package-generic-unix-${{ env.VERSION }}.tar.xz
    - name: EXPAND & COMPARE
      run: |
        mkdir bazel
        pushd bazel
          tar -xf ${{ github.workspace }}/package-generic-unix.tar.xz
          find . | sort > ${{ github.workspace }}/bazel.manifest
        popd

        mkdir make
        pushd make
          tar -xf ${{ github.workspace }}/rabbitmq-server-generic-unix-*.tar.xz
          # delete an empty directory
          rm -d rabbitmq_server-*/plugins/rabbitmq_random_exchange-*/include
          find . | sort > ${{ github.workspace }}/make.manifest
        popd

        tree -L 3 bazel
        tree -L 3 make

        sleep 1

        set -x

        ./rabbitmq-server/tools/compare_dist.sh make bazel
