name: "promote testing to stable"
description: "Promote testing packages to stable."
inputs:
  artifactory_token:
    description: "Artifactory token"
    required: true
  module_name:
    description: "Module name"
    required: true
  distrib:
    description: "The distribution used for packaging"
    required: true
  major_version:
    description: "Centreon packaged major version"
    required: true
  minor_version:
    description: "Centreon package minor version"
    required: true
  stability:
    description: "The package stability (stable, testing, unstable)"
    required: true
  github_ref_name:
    description: "Release base ref name for push event"
    required: true
  release_type:
    description: "Type of release (hotfix, release)"
    required: true
  is_cloud:
    description: "Release context (cloud or not cloud)"
    required: true

runs:
  using: "composite"
  steps:
    - name: Validate inputs
      uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
      with:
        script: |
          if (! ['release', 'hotfix'].includes('${{ inputs.release_type }}')) {
            throw new Error('release_type input must be defined (release or hotfix)');
          }

    - uses: jfrog/setup-jfrog-cli@ff5cb544114ffc152db9cea1cd3d5978d5074946 # v4.5.11
      env:
        JF_URL: https://packages.centreon.com
        JF_ACCESS_TOKEN: ${{ inputs.artifactory_token }}

    - name: Parse distrib name
      id: parse-distrib
      uses: ./.github/actions/parse-distrib
      with:
        distrib: ${{ inputs.distrib }}

    - name: Promote RPM packages to stable
      if: ${{ steps.parse-distrib.outputs.package_extension == 'rpm' }}
      run: |
        set -eux

        # DEBUG
        echo "[DEBUG] - Major version: ${{ inputs.major_version }}"
        echo "[DEBUG] - Minor version: ${{ inputs.minor_version }}"
        echo "[DEBUG] - Distrib: ${{ inputs.distrib }}"
        echo "[DEBUG] - is_cloud: ${{ inputs.is_cloud }}"
        echo "[DEBUG] - release_type: ${{ inputs.release_type }}"
        echo "[DEBUG] - stability: ${{ inputs.stability}}"

        # Cloud specific promote
        # delivery by default to onprem, override to internal if is_cloud is true
        if [[ ${{ inputs.is_cloud }} == "true" ]]; then
          ROOT_REPO_PATH="rpm-standard-internal"
        else
          ROOT_REPO_PATH="rpm-standard"
        fi

        # Search for testing packages candidate for promote
        for ARCH in "noarch" "x86_64"; do
          SEARCH_REPO_PATH="${{ inputs.major_version }}/${{ inputs.distrib }}/testing-${{ inputs.release_type }}/$ARCH/${{ inputs.module_name }}"

          echo "[DEBUG] - Get path of $ARCH testing artifacts to promote to stable."
          SRC_PATHS=$(jf rt search --include-dirs "$ROOT_REPO_PATH/$SEARCH_REPO_PATH/*.rpm" | jq -r '.[].path')

          if [[ ${SRC_PATHS[@]} ]]; then
            for SRC_PATH in ${SRC_PATHS[@]}; do
              echo "[DEBUG] - Source path found: $SRC_PATH"
            done
          else
            echo "[DEBUG] - No source path found."
            continue
          fi

          # Build target path based on ARCH
          echo "[DEBUG] - Build $ARCH target path."
          TARGET_PATH="$ROOT_REPO_PATH/${{ inputs.major_version }}/${{ inputs.distrib }}/stable/$ARCH/RPMS/${{ inputs.module_name }}/"
          echo "[DEBUG] - Target path: $TARGET_PATH"

          # Download candidates for promote
          echo "[DEBUG] - Promoting $ARCH testing artifacts to stable."
          for ARTIFACT in ${SRC_PATHS[@]}; do
            echo "[DEBUG] - Downloading $ARTIFACT from TESTING."
            jf rt download $ARTIFACT --flat
          done

          DRY_RUN_FLAG="--dry-run"
          if [ "${{ inputs.stability }}" == "stable" ]; then
            DRY_RUN_FLAG=""
          fi

          # Upload previously downloaded candidates to TARGET_PATH
          for ARTIFACT_DL in $(find . -maxdepth 1 -type f -name '*.rpm' -printf '%f\n'); do
            echo "[DEBUG] - Promoting (upload) $ARTIFACT_DL to stable $TARGET_PATH."
            jf rt upload "$ARTIFACT_DL" "$TARGET_PATH" --flat $DRY_RUN_FLAG
          done

          if [ "${{ inputs.stability }}" == "stable" ]; then
            curl -H "Authorization: Bearer ${{ inputs.artifactory_token }}" -X POST "https://centreon.jfrog.io/artifactory/api/yum/$ROOT_REPO_PATH?path=/${{ inputs.major_version }}/${{ inputs.distrib }}/stable/$ARCH&async=1"
          fi

          # Cleanup before next round of candidates
          mkdir -p /tmp/promoted-packages
          mv *.rpm /tmp/promoted-packages/ >/dev/null 2>&1 || true
        done

      shell: bash

    - name: Promote DEB packages to stable
      if: ${{ steps.parse-distrib.outputs.package_extension == 'deb' && inputs.is_cloud == 'false' }}
      run: |
        set -eux

        echo "[DEBUG] - Major version: ${{ inputs.major_version }}"
        echo "[DEBUG] - Minor version: ${{ inputs.minor_version }}"
        echo "[DEBUG] - Distrib: ${{ inputs.distrib }}"

        # Define ROOT_REPO_PATH for debian
        # There is no cloud ROOT_REPO_PATH for debian, only onprem
        # Should there be a need to deploy debian to cloud repositories, please use the same condition as RPM promotion
        ROOT_REPO_PATH_PREFIX=""
        if [[ "${{ steps.parse-distrib.outputs.distrib_family }}" == "debian" ]]; then
          ROOT_REPO_PATH_PREFIX="apt-"
        elif [[ "${{ steps.parse-distrib.outputs.distrib_family }}" == "ubuntu" ]]; then
          ROOT_REPO_PATH_PREFIX="ubuntu-"
        else
          echo "::error::Unsupported distribution family: ${{ steps.parse-distrib.outputs.distrib_family }}"
          exit 1
        fi

        SOURCE_ROOT_REPO_PATH_SUFFIX=""
        TARGET_ROOT_REPO_PATH_SUFFIX=""
        if [[ "${{ inputs.is_cloud }}" == "true" ]]; then
          SOURCE_ROOT_REPO_PATH_SUFFIX="-internal"
          TARGET_ROOT_REPO_PATH_SUFFIX="-internal"
        elif [[ "${{ inputs.major_version }}" < "25.05" ]]; then
          TARGET_ROOT_REPO_PATH_SUFFIX="-${{ inputs.major_version }}-stable"
        fi

        SOURCE_ROOT_REPO_PATH="${ROOT_REPO_PATH_PREFIX}standard${SOURCE_ROOT_REPO_PATH_SUFFIX}"
        TARGET_ROOT_REPO_PATH="${ROOT_REPO_PATH_PREFIX}standard${TARGET_ROOT_REPO_PATH_SUFFIX}"

        echo "[DEBUG] - Get path of testing DEB packages to promote to stable."

        case "${{ inputs.major_version }}" in
          "22.10"|"23.10")
            SEARCH_PATTERN="*.deb"
            ;;
          *)
            SEARCH_PATTERN="*${{ steps.parse-distrib.outputs.package_distrib_name }}*.deb"
            ;;
        esac

        SRC_PATHS=$(jf rt search --include-dirs "$SOURCE_ROOT_REPO_PATH/pool/${{ inputs.major_version }}/testing/${{ inputs.release_type }}/${{ inputs.module_name }}/${SEARCH_PATTERN}" | jq -r '.[].path')

        if [[ ${SRC_PATHS[@]} ]]; then
          for SRC_PATH in ${SRC_PATHS[@]}; do
            echo "[DEBUG] - Source path found: $SRC_PATH"
          done
        else
          echo "::error::Nothing to promote."
          exit 1
        fi

        echo "[DEBUG] - Promoting DEB testing artifacts to stable."
        for ARTIFACT in ${SRC_PATHS[@]}; do
          echo "[DEBUG] - Downloading $ARTIFACT from TESTING."
          jf rt download $ARTIFACT --flat
        done

        DRY_RUN_FLAG="--dry-run"
        if [ "${{ inputs.stability }}" == "stable" ]; then
          DRY_RUN_FLAG=""
        fi

        for ARTIFACT_DL in $(find . -maxdepth 1 -type f -name '*.deb' -printf '%f\n'); do
          ARCH=$(echo $ARTIFACT_DL | cut -d '_' -f3 | cut -d '.' -f1)
          if [[ "${{ inputs.major_version }}" < "25.05" ]]; then
            POOL_SUBPATH="${{ inputs.module_name }}"
            DEB_PROP="${{ inputs.distrib }}/main/$ARCH"
          else
            POOL_SUBPATH="${{ inputs.major_version }}/${{ inputs.stability }}/${{ inputs.module_name }}"
            DEB_PROP="${{ inputs.distrib }}-${{ inputs.major_version }}-${{ inputs.stability }}/main/$ARCH"
          fi
          TARGET_PATH="$TARGET_ROOT_REPO_PATH/pool/${POOL_SUBPATH}/"

          echo "[DEBUG] - Promoting (upload) $ARTIFACT_DL to stable $TARGET_PATH."
          jf rt upload "$ARTIFACT_DL" "$TARGET_PATH" --deb "${DEB_PROP}" --detailed-summary --flat $DRY_RUN_FLAG
        done

        if [ "${{ inputs.stability }}" == "stable" ]; then
          curl -H "Authorization: Bearer ${{ inputs.artifactory_token }}" -X POST "https://centreon.jfrog.io/artifactory/api/deb/reindex/$TARGET_ROOT_REPO_PATH?async=1"
        fi
      shell: bash

    - name: Move deb packages to /tmp/promoted-packages
      if: ${{ steps.parse-distrib.outputs.package_extension == 'deb' }}
      run: |
        mkdir -p /tmp/promoted-packages
        mv *.deb /tmp/promoted-packages/ >/dev/null 2>&1 || true
      shell: bash
