Skip to content

Instantly share code, notes, and snippets.

@gasparnagy
Created September 6, 2023 14:15
Show Gist options
  • Save gasparnagy/395db346ba5a0ce11dc4e5a53ec7a600 to your computer and use it in GitHub Desktop.
Save gasparnagy/395db346ba5a0ce11dc4e5a53ec7a600 to your computer and use it in GitHub Desktop.
PowerShell script to re-download published TRX files after VSTest / VsTestForSpecSync task see https://docs.specsolutions.eu/specsync/important-concepts/how-to-publish-test-results-from-pipelines-using-the-vstest-task for details.
[CmdletBinding()]
param (
$outputFolder = "TestResults",
$testRunId = $env:VSTEST_TESTRUNID,
$pat = $env:SYSTEM_ACCESSTOKEN,
$projectUrl = $env:SYSTEM_COLLECTIONURI + $env:SYSTEM_TEAMPROJECT
)
if ($testRunId -eq $null -or $testRunId -eq '') {
Write-Error 'Test Run ID is not specified!' -ErrorAction Stop
}
if ($pat -eq $null -or $pat -eq '') {
Write-Error 'PAT is not specified!' -ErrorAction Stop
}
if ($projectUrl -eq $null -or $projectUrl -eq '') {
Write-Error 'Project URL is not specified!' -ErrorAction Stop
}
if ($outputFolder -eq $null -or $outputFolder -eq '') {
Write-Error 'Output folder is not specified!' -ErrorAction Stop
}
Write-Host "Downloading attachments for Test Run #$testRunId in project $projectUrl"
$currentFolder = Get-Location
$fullOutputFolder = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($currentFolder, $outputFolder))
$_ = [System.IO.Directory]::CreateDirectory($fullOutputFolder)
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($pat)"))
$header = @{Authorization = "Basic $token"}
$attachmentsResult = Invoke-RestMethod -Uri ("$projectUrl/_apis/test/runs/" + $testRunId + "/attachments?api-version=5.1") -Headers $header -Method Get
Write-Debug "attachments = $($attachmentsResult | ConvertTo-Json -Depth 100)"
$attachmentsResult.value | ForEach-Object {
$attachmentUrl = $_.url
$attachmentFileName = $_.fileName
$outputFile = [System.IO.Path]::Combine($fullOutputFolder, $attachmentFileName)
Write-Host "Downloading attachment $attachmentFileName"
Write-Host " from $attachmentUrl"
Write-Host " to $outputFile"
Invoke-RestMethod -Uri ($attachmentUrl + "?api-version=5.1") -Headers $header -Method Get -OutFile $outputFile
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment