name: Crawl Versions from newreleases.io

on:
  workflow_dispatch:
  schedule:
    # Runs at 12:00 AM and 12:00 PM UTC
    - cron: "0 0,12 * * *"

permissions:
  contents: write
  pull-requests: write

jobs:
  crawl-versions:
    if: github.repository == 'community-scripts/ProxmoxVE'
    runs-on: runner-cluster-htl-set

    steps:
      - name: Checkout Repository
        uses: actions/checkout@v2
        with:
          repository: community-scripts/ProxmoxVE
          ref: main

      - name: Generate a token
        id: generate-token
        uses: actions/create-github-app-token@v1
        with:
          app-id: ${{ vars.APP_ID }}
          private-key: ${{ secrets.APP_PRIVATE_KEY }}

      - name: Crawl from newreleases.io
        env:
          token: ${{ secrets.NEWRELEASES_TOKEN }}
        run: |
          page=1
          projects_file="project_json"
          output_file="frontend/public/json/versions.json"

          echo "[]" > $output_file

          while true; do

            echo "Start loop on page: $page"

            projects=$(curl -s -H "X-Key: $token" "https://api.newreleases.io/v1/projects?page=$page")
            total_pages=$(echo "$projects" | jq -r '.total_pages')

            if [ -z "$total_pages" ] || [ "$total_pages" -eq 0 ]; then
             echo "No pages available. Exiting."
             exit 1
            fi
            if [ $page == $total_pages ]; then
              break
            fi

            if [ -z "$projects" ] || ! echo "$projects" | jq -e '.projects' > /dev/null; then
              echo "No more projects or invalid response. Exiting."
              break
            fi

            echo "$projects" > "$projects_file"

            jq -r '.projects[] | "\(.id) \(.name)"' "$projects_file" | while read -r id name; do
              version=$(curl -s -H "X-Key: $token" "https://api.newreleases.io/v1/projects/$id/latest-release")
              version_data=$(echo "$version" | jq -r '.version // empty')
              date=$(echo "$version" | jq -r '.date // empty')
              if [ -n "$version_data" ]; then
                jq --arg name "$name" --arg version "$version_data" --arg date "$date" \
                  '. += [{"name": $name, "version": $version, "date": $date}]' "$output_file" > "$output_file.tmp" && mv "$output_file.tmp" "$output_file"
              fi
            done
            ((page++))
          done

      - name: Commit JSON
        env:
          GH_TOKEN: ${{ steps.generate-token.outputs.token }}
        run: |
          git config --global user.email "github-actions[bot]@users.noreply.github.com"
          git config --global user.name "GitHub Actions[bot]"
          git checkout -b update_versions || git checkout update_versions
          git add frontend/public/json/versions.json
          if git diff --cached --quiet; then
            echo "No changes detected."
            echo "changed=false" >> "$GITHUB_ENV"
            exit 0
          else
            echo "Changes detected:"
            git diff --stat --cached
            echo "changed=true" >> "$GITHUB_ENV"
          fi
          git commit -m "Update versions.json"
          git push origin update_versions --force
          gh pr create --title "[Github Action] Update versions.json" --body "Update versions.json, crawled from newreleases.io" --base main --head update_versions --label "automated pr"

      - name: Approve pull request
        if: env.changed == 'true'
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          PR_NUMBER=$(gh pr list --head "update_versions" --json number --jq '.[].number')
          if [ -n "$PR_NUMBER" ]; then
            gh pr review $PR_NUMBER --approve
          fi

      - name: Approve pull request and merge
        if: env.changed == 'true'
        env:
          GH_TOKEN: ${{ secrets.PAT_AUTOMERGE }}
        run: |
          PR_NUMBER=$(gh pr list --head "update_versions" --json number --jq '.[].number')
          if [ -n "$PR_NUMBER" ]; then
            gh pr review $PR_NUMBER --approve
            gh pr merge $PR_NUMBER --squash --admin
          fi

      - name: Re-approve pull request after update
        if: env.changed == 'true'
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          PR_NUMBER=$(gh pr list --head "update_versions" --json number --jq '.[].number')
          if [ -n "$PR_NUMBER" ]; then
            gh pr review $PR_NUMBER --approve
          fi
