diff --git a/Jenkinsfile b/Jenkinsfile index 657e473..4b29899 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -48,68 +48,75 @@ pipeline { def result = currentBuild.currentResult ?: 'UNKNOWN' withCredentials([string(credentialsId: 'TAIGA_TOKEN', variable: 'TAIGA_TOKEN')]) { - powershell(returnStatus: true, script: """ - \$commitMsg = git log -1 --pretty=%B - \$match = [regex]::Match(\$commitMsg, 'TG-(\\d+)') + writeFile file: 'taiga-post.ps1', text: ''' +$ErrorActionPreference = "Continue" - if (-not \$match.Success) { - Write-Host "No TG-* reference found" - exit 0 - } +$commitMsg = git log -1 --pretty=%B +$match = [regex]::Match($commitMsg, 'TG-(\\d+)') - \$ref = \$match.Groups[1].Value - \$headers = @{ - Authorization = "Bearer \$env:TAIGA_TOKEN" - "Content-Type" = "application/json" - } +if (-not $match.Success) { + Write-Host "No TG-* reference found" + exit 0 +} - \$targets = @( - @{ endpoint = "userstories"; label = "User Story" }, - @{ endpoint = "issues"; label = "Issue" }, - @{ endpoint = "tasks"; label = "Task" } - ) +$ref = $match.Groups[1].Value - \$found = \$null +$headers = @{ + Authorization = "Bearer $env:TAIGA_TOKEN" + "Content-Type" = "application/json" +} - foreach (\$target in \$targets) { - try { - \$url = "\$env:TAIGA_URL/api/v1/\$(\$target.endpoint)/by_ref?project=\$env:TAIGA_PROJECT_ID&ref=\$ref" - \$data = Invoke-RestMethod -Uri \$url -Headers \$headers -Method Get - if (\$data.id) { - \$found = @{ - endpoint = \$target.endpoint - label = \$target.label - data = \$data - } - break - } - } catch { - Write-Host "Taiga lookup warning: \$($_.Exception.Message)" - } - } +$targets = @( + @{ endpoint = "userstories"; label = "User Story" }, + @{ endpoint = "issues"; label = "Issue" }, + @{ endpoint = "tasks"; label = "Task" } +) - if (-not \$found) { - Write-Host "Taiga TG-\$ref not found" - exit 0 - } +$found = $null - \$comment = "Jenkins Windows Qt build #$env:BUILD_NUMBER: ${result}`n$env:BUILD_URL" +foreach ($target in $targets) { + try { + $url = "$env:TAIGA_URL/api/v1/$($target.endpoint)/by_ref?project=$env:TAIGA_PROJECT_ID&ref=$ref" + $data = Invoke-RestMethod -Uri $url -Headers $headers -Method Get + if ($data.id) { + $found = @{ + endpoint = $target.endpoint + label = $target.label + data = $data + } + break + } + } catch { + Write-Host "Taiga lookup warning: $($_.Exception.Message)" + } +} - \$body = @{ - comment = \$comment - version = \$found.data.version - } | ConvertTo-Json +if (-not $found) { + Write-Host "Taiga TG-$ref not found" + exit 0 +} - try { - \$url = "\$env:TAIGA_URL/api/v1/\$(\$found.endpoint)/\$(\$found.data.id)" - Invoke-RestMethod -Uri \$url -Headers \$headers -Method Patch -Body \$body - Write-Host "Commented Taiga TG-\$ref (\$(\$found.label))" - } catch { - Write-Host "Taiga comment warning: \$($_.Exception.Message)" - } +$comment = "Jenkins Windows Qt build #$env:BUILD_NUMBER: $env:BUILD_RESULT`n$env:BUILD_URL" - exit 0 - """) +$body = @{ + comment = $comment + version = $found.data.version +} | ConvertTo-Json + +try { + $url = "$env:TAIGA_URL/api/v1/$($found.endpoint)/$($found.data.id)" + Invoke-RestMethod -Uri $url -Headers $headers -Method Patch -Body $body + Write-Host "Commented Taiga TG-$ref ($($found.label))" +} catch { + Write-Host "Taiga comment warning: $($_.Exception.Message)" +} + +exit 0 +''' + + withEnv(["BUILD_RESULT=${result}"]) { + powershell(returnStatus: true, file: 'taiga-post.ps1') + } } } }