name: Run Scripts on PVE Node for testing
permissions:
  pull-requests: write
on:
  pull_request_target:
    branches:
      - main
    paths:
      - "install/**.sh"
      - "ct/**.sh"

jobs:
  run-install-script:
    runs-on: pvenode
    steps:
      - name: Checkout PR branch
        uses: actions/checkout@v4
        with:
          ref: ${{ github.event.pull_request.head.ref }}
          repository: ${{ github.event.pull_request.head.repo.full_name }}
          fetch-depth: 0

      - name: Add Git safe directory
        run: |
          git config --global --add safe.directory /__w/ProxmoxVE/ProxmoxVE

      - name: Set up GH_TOKEN
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          echo "GH_TOKEN=${GH_TOKEN}" >> $GITHUB_ENV

      - name: Get Changed Files
        run: |
          CHANGED_FILES=$(gh pr diff ${{ github.event.pull_request.number }} --repo ${{ github.repository }} --name-only)
          CHANGED_FILES=$(echo "$CHANGED_FILES" | tr '\n' ' ')
          echo "Changed files: $CHANGED_FILES"
          echo "SCRIPT=$CHANGED_FILES" >> $GITHUB_ENV
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Get scripts
        id: check-install-script
        run: |
          ALL_FILES=()
          ADDED_FILES=()  
          for FILE in ${{ env.SCRIPT }}; do           
            if [[ $FILE =~ ^install/.*-install\.sh$ ]] || [[ $FILE =~ ^ct/.*\.sh$ ]]; then             
              STRIPPED_NAME=$(basename "$FILE" | sed 's/-install//' | sed 's/\.sh$//')
              if [[ ! " ${ADDED_FILES[@]} " =~ " $STRIPPED_NAME " ]]; then
                ALL_FILES+=("$FILE")
                ADDED_FILES+=("$STRIPPED_NAME")  # Mark this base file as added (without the path)
              fi
            fi
          done
          ALL_FILES=$(echo "${ALL_FILES[@]}" | xargs)
          echo "$ALL_FILES"
          echo "ALL_FILES=$ALL_FILES" >> $GITHUB_ENV

      - name: Run scripts
        id: run-install
        continue-on-error: true
        run: |
          set +e  
          #run for each files in /ct
          for FILE in ${{ env.ALL_FILES }}; do            
            STRIPPED_NAME=$(basename "$FILE" | sed 's/-install//' | sed 's/\.sh$//')
            echo "Running Test for: $STRIPPED_NAME"
            if grep -E -q 'read\s+-r\s+-p\s+".*"\s+\w+' "$FILE"; then
              echo "The script contains an interactive prompt. Skipping execution."
              continue
            fi
            if [[ $FILE =~ ^install/.*-install\.sh$ ]]; then
              CT_SCRIPT="ct/$STRIPPED_NAME.sh"
              if [[ ! -f $CT_SCRIPT ]]; then
                echo "No CT script found for $STRIPPED_NAME"
                ERROR_MSG="No CT script found for $FILE"
                echo "$ERROR_MSG" > result_$STRIPPED_NAME.log
                continue
              fi
                if grep -E -q 'read\s+-r\s+-p\s+".*"\s+\w+' "install/$STRIPPED_NAME-install.sh"; then
                  echo "The script contains an interactive prompt. Skipping execution."
                  continue
                fi
                echo "Found CT script for $STRIPPED_NAME"
                chmod +x "$CT_SCRIPT"
                RUNNING_FILE=$CT_SCRIPT
            elif [[ $FILE =~ ^ct/.*\.sh$ ]]; then
              INSTALL_SCRIPT="install/$STRIPPED_NAME-install.sh"
              if [[ ! -f $INSTALL_SCRIPT ]]; then
                echo "No install script found for $STRIPPED_NAME"
                ERROR_MSG="No install script found for $FILE"
                echo "$ERROR_MSG" > result_$STRIPPED_NAME.log
                continue
              fi                
                echo "Found install script for $STRIPPED_NAME"
                chmod +x "$INSTALL_SCRIPT"
                RUNNING_FILE=$FILE
                if grep -E -q 'read\s+-r\s+-p\s+".*"\s+\w+' "ct/$STRIPPED_NAME.sh"; then
                  echo "The script contains an interactive prompt. Skipping execution."
                  continue
                fi
            fi
            git remote add community-scripts https://github.com/community-scripts/ProxmoxVE.git
            git fetch community-scripts
            rm -f .github/workflows/scripts/app-test/pr-build.func || true
            rm -f .github/workflows/scripts/app-test/pr-install.func || true
            rm -f .github/workflows/scripts/app-test/pr-alpine-install.func || true
            rm -f .github/workflows/scripts/app-test/pr-create-lxc.sh || true
            git checkout community-scripts/main -- .github/workflows/scripts/app-test/pr-build.func
            git checkout community-scripts/main -- .github/workflows/scripts/app-test/pr-install.func
            git checkout community-scripts/main -- .github/workflows/scripts/app-test/pr-alpine-install.func
            git checkout community-scripts/main -- .github/workflows/scripts/app-test/pr-create-lxc.sh
            chmod +x $RUNNING_FILE         
            chmod +x .github/workflows/scripts/app-test/pr-create-lxc.sh
            chmod +x .github/workflows/scripts/app-test/pr-install.func
            chmod +x .github/workflows/scripts/app-test/pr-alpine-install.func
            chmod +x .github/workflows/scripts/app-test/pr-build.func
            sed -i 's|source <(curl -fsSL https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/misc/build.func)|source .github/workflows/scripts/app-test/pr-build.func|g' "$RUNNING_FILE"
            echo "Executing $RUNNING_FILE"
            ERROR_MSG=$(./$RUNNING_FILE 2>&1 > /dev/null)
            echo "Finished running $FILE"
            if [ -n "$ERROR_MSG" ]; then
              echo "ERROR in $STRIPPED_NAME: $ERROR_MSG"
              echo "$ERROR_MSG" > result_$STRIPPED_NAME.log
            fi
          done
          set -e  # Restore exit-on-error

      - name: Cleanup PVE Node
        run: |
          containers=$(pct list | tail -n +2 | awk '{print $0 " " $4}' | awk '{print $1}')

          for container_id in $containers; do
            status=$(pct status $container_id | awk '{print $2}')
            if [[ $status == "running" ]]; then
                pct stop $container_id
                pct destroy $container_id
            fi            
          done

      - name: Post error comments
        run: |
          ERROR="false"
          SEARCH_LINE=".github/workflows/scripts/app-test/pr-build.func: line 255:"

          # Get all existing comments on the PR
          EXISTING_COMMENTS=$(gh pr view ${{ github.event.pull_request.number }} --repo ${{ github.repository }} --json comments --jq '.comments[].body')

          for FILE in ${{ env.ALL_FILES }}; do
            STRIPPED_NAME=$(basename "$FILE" | sed 's/-install//' | sed 's/\.sh$//')
            if [[ ! -f result_$STRIPPED_NAME.log ]]; then
              continue
            fi
            ERROR_MSG=$(cat result_$STRIPPED_NAME.log)

            if [ -n "$ERROR_MSG" ]; then
              CLEANED_ERROR_MSG=$(echo "$ERROR_MSG" | sed "s|$SEARCH_LINE.*||")
              COMMENT_BODY=":warning: The script _**$FILE**_ failed with the following message: <br> <div><strong>${CLEANED_ERROR_MSG}</strong></div>"

              # Check if the comment already exists
              if echo "$EXISTING_COMMENTS" | grep -qF "$COMMENT_BODY"; then
                echo "Skipping duplicate comment for $FILE"
              else
                echo "Posting error message for $FILE"
                gh pr comment ${{ github.event.pull_request.number }} \
                  --repo ${{ github.repository }} \
                  --body "$COMMENT_BODY"
                ERROR="true"
              fi
            fi
          done

          echo "ERROR=$ERROR" >> $GITHUB_ENV
