---
name: Check for New Releases

on:
  schedule:
    - cron: "0 0 * * *" # Runs daily at midnight (UTC)

jobs:
  check_release:
    runs-on: ubuntu-latest
    env:
      GH_TOKEN: ${{ github.token }}
    permissions:
      contents: write
      pull-requests: write
    steps:
      - name: Checkout repository
        uses: actions/checkout@v2
      - name: Check for new release
        id: check
        run: |
          set -e  # Exit script on first error
          # Get the list of releases
          releases=$(gh release list --repo https://github.com/crc-org/crc/ \
                                     --exclude-pre-releases \
                                     --exclude-drafts)
          crc_ocp_map=$(echo "$releases" | sed -E 's/^[0-9]+\.[0-9]+\.[0-9]+-([0-9]+\.[0-9]+\.[0-9]+).*v([0-9]+\.[0-9]+\.[0-9]+).*/\1\tv\2/')
          
          # Create an associative array to keep track of the latest crc version for each OpenShift version
          declare -A latest_crc_versions
          while IFS= read -r line; do
            ocp_version=$(awk '{print $1}' <<< "$line" | awk '{$1=$1};1')
            crc_version=$(awk '{print $NF}' <<< "$line" | awk '{$1=$1};1')
            if [[ -z "${latest_crc_versions[$ocp_version]}" || "$crc_version" > "${latest_crc_versions[$ocp_version]}" ]]; then
              latest_crc_versions[$ocp_version]=$crc_version
            fi
          done <<< "$crc_ocp_map"
          
          openshift_versions=$(printf '%s\n' "${!latest_crc_versions[@]}" | sort)

          # Update files if there is a diff, otherwise exit
          cd build || exit
          current_versions=$(grep -oP "(?<=OPENSHIFT_VERSION: \[).*?(?=\])" .gitlab-ci.yml | tr -d ' ' | tr ',' '\n' | sort)
          if [[ "$current_versions" == "$openshift_versions" ]]; then
            exit
          else
            # Replace the array in GitLab CI YAML
            openshift_versions=$(echo "$openshift_versions" | awk -v d=" " '{s=(NR==1?s:s d)$0}END{print s}' | tr ' ' ', ')
            sed -i "s/\(i[[:space:]]*\)- OPENSHIFT_VERSION: \[.*\]/\1- OPENSHIFT_VERSION: [$openshift_versions]/" .gitlab-ci.yml 

            # Create properly formatted version map from $latest_crc_versions
            version_map="declare -A VERSION_MAP=("
            for key in "${!latest_crc_versions[@]}"; do
              version_map+="[\"${key}\"]=\"${latest_crc_versions[$key]}\" "
            done
            version_map+=")"
                                                                                                                   
            # Replace version map in setup.sh
            sed -i "s/^declare -A VERSION_MAP.*/$version_map/" setup.sh
          fi
      - name: Commit and push changes
        id: commit
        run: |
          git config --local user.email "action@github.com"
          git config --local user.name "GitHub Action"

          branch_name="$(date +%F)-bump-crc-versions"

          git checkout -b "$branch_name"

          if [[ -z "$(git status --porcelain)" ]]; then
            echo "No changes to commit. Exiting..."
            exit 0
          fi

          git commit -am "Update crc releases"
          git push origin "$branch_name"

          gh pr create --draft  --base 'main' \
                                --body 'Added new OpenShift versions from [OpenShift Local](https://github.com/crc-org/crc/releases)' \
                                --label 'automated' \
                                --label 'enhancement' \
                                --reviewer 'pfeifferj' \
                                --title '[AUTOMATED ACTION] Add new OpenShift versions' \
                                --repo https://github.com/pfeifferj/openpipe
