#!/usr/bin/env bash

# Check if environment variables are set
if [ -z "$SONARQUBE_HOST" ] || [ -z "$SONARQUBE_TOKEN" ]; then
  echo "Please set the SONARQUBE_HOST and SONARQUBE_TOKEN environment variables."
  exit 1
fi

# Initialize variables
total_task_count=0
page_size=100 # Adjust as needed
page=1

# Fetch tasks from the Compute Engine API
while true; do
  tasks_response=$(curl -s -u "$SONARQUBE_TOKEN:" "$SONARQUBE_HOST/api/ce/activity" -d "status=PENDING,IN_PROGRESS" -d "ps=$page_size" -d "p=$page")
  tasks=$(echo "$tasks_response" | jq -r '.tasks // []')

  total_task_count=$((total_task_count + $(echo "$tasks" | jq length)))

  if [ "$(echo "$tasks" | jq length)" -lt "$page_size" ]; then
    break
  fi

  page=$((page + 1))
done

echo "$total_task_count"
