---
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/')
          openshift_versions=$(echo "$releases" | sed -E 's/.*-([0-9]+\.[0-9]+\.[0-9]+).*/\1/')
          openshift_versions+="\nlatest"
          openshift_versions=$(echo "$openshift_versions" | sort) 
      - name: Update files
        id: update
        run: |
          # 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
            sed -i "s/OPENSHIFT_VERSION:\[.*\]/OPENSHIFT_VERSION:[$openshift_versions]/" .gitlab-ci.yml
                                                                                                                   
            # Create properly formatted version map from $crc_ocp_map
            version_map="declare -A VERSION_MAP=("
            while IFS= read -r line; do
              key=$(awk '{print $1}' <<< "$line" | awk '{$1=$1};1')
              value=$(awk '{print $NF}' <<< "$line" | awk '{$1=$1};1')
              version_map+="[\"${key}\"]=\"${value}\" "
            done <<< "$crc_ocp_map"
            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"
          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
