Created
April 25, 2025 11:47
-
-
Save willsantos/1a59122b1d8a3117ddef16c44ceb1cfe to your computer and use it in GitHub Desktop.
Script para enviar o xml do coverlet para o sonarqube local
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Variáveis | |
$projetoKey = "CHAVE_NO_SONAR" | |
$sonarHost = "http://localhost:9000" | |
$branch = "develop" | |
$sonarToken = $Env:SONARQUBE_TOKEN | |
$globalResultsPath = "TestResults" | |
if (-not $sonarToken) { | |
Write-Error "❌ Variável de ambiente SONARQUBE_TOKEN não está definida." | |
exit 1 | |
} | |
# Criar pasta de resultados se não existir | |
if (-not (Test-Path $globalResultsPath)) { | |
New-Item -ItemType Directory -Path $globalResultsPath -Force | Out-Null | |
} | |
# Caminho absoluto para a pasta de resultados | |
$resolvedResultsPath = (Resolve-Path $globalResultsPath -ErrorAction SilentlyContinue)?.Path | |
if (-not $resolvedResultsPath) { | |
Write-Error "❌ Falha ao resolver o caminho absoluto para TestResults." | |
exit 1 | |
} | |
# Chama cli para conectar com o sonar | |
dotnet sonarscanner begin ` | |
/k:"$projetoKey" ` | |
/d:sonar.login="$sonarToken" ` | |
/d:sonar.host.url="$sonarHost" ` | |
/d:sonar.branch.name="$branch" ` | |
/d:sonar.cs.opencover.reportsPaths="$resolvedResultsPath\*.coverage.xml" | |
# Buscar e executar todos os projetos de teste | |
$testProjects = Get-ChildItem -Recurse -Filter *.csproj | | |
Where-Object { $_.FullName -match "Tests" } | |
if (-not $testProjects) { | |
Write-Host "`n⚠️ Nenhum projeto de teste encontrado com 'Tests' no nome." | |
} | |
else { | |
Write-Host "`n📦 Projetos de teste encontrados: $($testProjects.Count)" | |
foreach ($proj in $testProjects) { | |
$projectPath = $proj.FullName | |
$projectName = Split-Path $projectPath -LeafBase | |
$coveragePath = Join-Path $resolvedResultsPath "$projectName.coverage.xml" | |
# Validar dependência coverlet.msbuild | |
$projectContent = Get-Content $projectPath -Raw | |
if ($projectContent -notmatch 'coverlet.msbuild') { | |
Write-Error "`n❌ O projeto $projectName não possui o pacote 'coverlet.msbuild' instalado." | |
Write-Host "➡️ Execute o seguinte comando para corrigir:" | |
Write-Host " dotnet add $projectPath package coverlet.msbuild`n" | |
dotnet sonarscanner end /d:sonar.login="$sonarToken" | |
exit 1 | |
} | |
Write-Host "`n🧪 Executando cobertura no projeto: $projectName" | |
dotnet test $projectPath ` | |
/p:CollectCoverage=true ` | |
/p:CoverletOutputFormat=opencover ` | |
/p:CoverletOutput="$coveragePath" | |
} | |
} | |
# Verificar cobertura gerada | |
Write-Host "`n🔍 Arquivos de cobertura gerados:" | |
$xmls = Get-ChildItem -Path $resolvedResultsPath -Filter *.coverage.xml | |
if ($xmls.Count -eq 0) { | |
Write-Warning "Nenhum arquivo .coverage.xml encontrado em $resolvedResultsPath" | |
} | |
else { | |
$xmls | ForEach-Object { Write-Host " - $($_.Name)" } | |
} | |
# Encerrar análise Sonar | |
Write-Host "`n📤 Finalizando análise com Sonar..." | |
dotnet sonarscanner end /d:sonar.login="$sonarToken" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment