name: SonarQube Analysis

on:
  push:
    branches: [ main, master, develop ]
  pull_request:
    branches: [ main, master ]

jobs:
  sonarqube:
    name: SonarQube Scan
    runs-on: ubuntu-latest
    
    steps:
    - name: Checkout code
      run: |
        git clone http://172.17.0.2:3000/${{ gitea.repository }}.git .
        
    - name: Test SonarQube connection
      run: |
        echo "Testing connection to SonarQube..."
        curl -f http://172.17.0.3:9000/api/system/ping || echo "Connection failed"
        
    - name: Run SonarQube scan
      env:
        SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
      run: |
        docker run \
          --rm \
          --network bridge \
          -e SONAR_HOST_URL="http://172.17.0.3:9000" \
          -e SONAR_SCANNER_OPTS="-Dsonar.projectKey=my-project" \
          -e SONAR_TOKEN="${SONAR_TOKEN}" \
          -v "$(pwd):/usr/src" \
          sonarsource/sonar-scanner-cli