This repository has been archived by the owner on Mar 23, 2022. It is now read-only.
/
job-to-helix-info.ps1
65 lines (55 loc) · 2.52 KB
/
job-to-helix-info.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
param (
[Parameter(Mandatory=$true)]
$buildUrl,
[Parameter(Mandatory=$true)]
$azdoPat,
[Parameter(Mandatory=$true)]
$appInsightsAppId,
[Parameter(Mandatory=$true)]
$appInsightsKey
)
$base64authinfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$azdoPat"))
$vstsAuthHeader = @{"Authorization"="Basic $base64authinfo"}
$allHeaders = $vstsAuthHeader + @{"Content-Type"="application/json"; "Accept"="application/json"}
# Find the plan info
$buildUri = [System.Uri]$buildUrl
$buildId = $null
$account = $null
$project = $null
if ($buildUri.Host -eq "dev.azure.com") {
$account = $buildUri.Segments[1].Replace("/", "")
$project = $buildUri.Segments[2].Replace("/", "")
} else {
throw "Please supply buildUrl in dev.azure.com/<account>/<project> form"
}
if ($buildUri.Query -match "buildId=(?<buildId>\d+)") {
$buildId = [int]$Matches.buildId
} else {
throw "Could not parse build url $buildUrl"
}
Write-Verbose "Looking up plan info for build $buildId in project $project on account $account"
$buildInfo = Invoke-WebRequest -Method Get -Headers $allHeaders -Uri "https://dev.azure.com/$account/$project/_apis/build/builds/$buildId" | ConvertFrom-Json
$planId = $buildInfo.orchestrationPlan.planId
Write-Verbose "Found plan id $planId, looking up in appinsights"
$appInsightsHeaders = @{ “X-Api-Key” = $appInsightsKey; “Content-Type” = “application/json” }
$appInsightsOperation = "query"
$appInsightsQuery = [uri]::EscapeUriString("?query=traces|where message contains `"Successfully submitted new Helix job`" | where customDimensions.orchestrationId contains `"$planId`"")
$fullUri = “https://api.applicationinsights.io/v1/apps/$appInsightsAppId/$appInsightsOperation$appInsightsQuery”
$appInsightsData = Invoke-WebRequest -Method Get -Uri $fullUri -Headers $appInsightsHeaders | ConvertFrom-Json
Write-Host ""
$firstRow = $true
foreach ($row in $appInsightsData.tables.rows) {
$customDims = ConvertFrom-Json $row[4]
if ($firstRow) {
$firstRow = $false
Write-Host "Pool: $($customDims.agentPool)"
Write-Host "Plan Id: $($customDims.orchestrationId)"
Write-Host ""
}
Write-Host "Agent Id: $($customDims.agentId)"
Write-Host "Job Name: $($customDims.jobName)"
Write-Host "Queue: $($customDims.queueId)"
Write-Host "Helix Correlation Id: $($customDims.helixJob)"
Write-Host "Helix Work Item Friendly Name: $($customDims.workItemName)"
Write-Host ""
}