forked from microsoft/msquic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdownload-failures.ps1
100 lines (76 loc) · 3.48 KB
/
download-failures.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<#
.SYNOPSIS
This script will download all faulure logs, along with their associated builds, from AZP.
.PARAMETER AccessToken
Specifies the AccessToken used to access the artifacts. This token only needs Build (Read)
permissions. This can also be read from an AZP_ACCESS_TOKEN environment variable if not passed in.
PATs can be grabbed by using the instructions at the following link.
https://docs.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops&tabs=preview-page
It only needs to support Build (Read)
.PARAMETER BuildNumber
Specifies the build number to grab artifacts from
.EXAMPLE
download-failures.ps1 -AccessToken GetAccessTokenFromAzureHere -BuildNumber BuildNumberFromAzure
#>
param (
[Parameter(Mandatory = $false)]
[string]$AccessToken = $null,
[Parameter(Mandatory = $true)]
[string]$BuildNumber
)
Set-StrictMode -Version 'Latest'
$PSDefaultParameterValues['*:ErrorAction'] = 'Stop'
if ([string]::IsNullOrWhiteSpace($AccessToken)) {
$AccessToken = $env:AZP_ACCESS_TOKEN
if ([string]::IsNullOrWhiteSpace($AccessToken)) {
Write-Error "No access token found in either parameters or AZP_ACCESS_TOKEN env variable"
}
}
$RootDir = Split-Path $PSScriptRoot -Parent
$LogsFolder = Join-Path $RootDir "artifacts" "failurelogs"
$BuildLogFolder = Join-Path $LogsFolder $BuildNumber
New-Item -Path $LogsFolder -ItemType Directory -Force | Out-Null
$URL = "https://dev.azure.com/ms/msquic/_apis/build/builds/$BuildNumber"
$AzureDevOpsAuthenicationHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($AccessToken)")) }
$Artifacts = Invoke-RestMethod -Uri "$URL/artifacts" -Method "GET" -ContentType "application/json" -Headers $AzureDevOpsAuthenicationHeader
if ($Artifacts.count -eq 0) {
Write-Error "No Artifacts found"
}
$ContainerId = $null
#Find Artifacts container id
foreach ($Artifact in $Artifacts.value) {
if ($Artifact.name -eq "artifacts") {
$ContainerId = $Artifact.resource.data
$ContainerId = $ContainerId.Substring(2)
break
}
}
if ($null -eq $ContainerId) {
Write-Error "Artifacts for build not found"
}
# Check to see if we have any "logs" artifacts
foreach ($Artifact in $Artifacts.value) {
if ($Artifact.name -ne "logs") {
continue
}
# Download logs artifact
$ArtifactUrl = $Artifact.resource.downloadUrl
$ArtifactsZip = Join-Path $LogsFolder "Logs_$BuildNumber.zip"
Invoke-WebRequest -Uri $ArtifactUrl -Method "GET" -OutFile $ArtifactsZip
Expand-Archive -Path $ArtifactsZip -DestinationPath $BuildLogFolder -Force
$ContainerRootUri = "https://dev.azure.com/ms/_apis/resources/Containers/$ContainerId" + "?itemPath=artifacts/bin/"
# Find all failing builds, download all artifacts for said build
$FailingConfigs = Get-ChildItem "$BuildLogFolder/logs/*/*"
foreach ($Config in $FailingConfigs) {
$Arch = $Config.Name
$Os = $config.Parent.Name
$Config = "$Os/$Arch"
$DownloadUri = $ContainerRootUri + $Config + "&%24format=zip&saveAbsolutePath=false"
$DownloadFile = Join-Path $BuildLogFolder "$Os$Arch.zip"
Invoke-WebRequest -Uri $DownloadUri -Method "GET" -OutFile $DownloadFile -Headers $AzureDevOpsAuthenicationHeader
$BinFolder = Join-Path $BuildLogFolder "bin" $os
Write-Host $BinFolder
Expand-Archive -Path $DownloadFile -DestinationPath $BinFolder -Force
}
break
}