#!/bin/bash

set -e

PROJECT_KEY=$1
ENFORCE_QUALITY_GATE=$2

echo "Checking Quality Gate for project: $PROJECT_KEY"

STATUS=$(curl -s -u "$SONAR_TOKEN:" \
  "http://localhost:9000/api/qualitygates/project_status?projectKey=$PROJECT_KEY" \
  | jq -r '.projectStatus.status')

echo "Quality Gate status: $STATUS"

if [ "$ENFORCE_QUALITY_GATE" = "true" ] && [ "$STATUS" != "OK" ]; then
  echo "Quality Gate failed"
  exit 1
fi

echo "Quality Gate passed"