|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | + Resolve a build's .binlog and print its path, for live analysis via the |
| 4 | + `binlog-mcp` MCP server (Microsoft.AITools.BinlogMcp). Works on a local |
| 5 | + build's binlog or on a failed dotnet/fsharp Azure DevOps PR build's |
| 6 | + published binlog. |
| 7 | +
|
| 8 | +.DESCRIPTION |
| 9 | + This skill's job is ACQUISITION only — it does not analyze. It locates (and, |
| 10 | + for Azure DevOps, downloads) the binary log and prints the path(s). Hand each |
| 11 | + path to the binlog-mcp MCP tools (binlog_overview, binlog_diagnose, |
| 12 | + binlog_errors, ...) with `binlog_file: <path>`. |
| 13 | +
|
| 14 | + Source (pick one): |
| 15 | + * Local — pass -BinlogPath, or run with no arguments to auto-discover the |
| 16 | + newest *.binlog under <repo>/artifacts/log. |
| 17 | + * Azure DevOps — pass -PrNumber (latest failed `fsharp-ci` build) or an |
| 18 | + explicit -BuildId; the build-leg binlog artifact is downloaded |
| 19 | + to a temp folder and KEPT, so the MCP can read it afterwards. |
| 20 | +
|
| 21 | +.PARAMETER BinlogPath |
| 22 | + Local binlog source: a .binlog file, a directory (newest *.binlog inside, or |
| 23 | + all with -AllLegs), or a glob. No download is performed. |
| 24 | +
|
| 25 | +.PARAMETER PrNumber |
| 26 | + GitHub PR number in dotnet/fsharp. The latest failed build is used. |
| 27 | +
|
| 28 | +.PARAMETER BuildId |
| 29 | + Explicit Azure DevOps build id. |
| 30 | +
|
| 31 | +.PARAMETER AllLegs |
| 32 | + Include every binlog rather than just the build leg / newest: all binlog |
| 33 | + artifacts for an AzDo build, or every *.binlog in a local directory. |
| 34 | +
|
| 35 | +.PARAMETER Json |
| 36 | + Emit the resolved path(s) as JSON (`{ "binlogs": [ ... ] }`). |
| 37 | +
|
| 38 | +.EXAMPLE |
| 39 | + # Newest local build binlog (build first, e.g. ./build.sh --binaryLog): |
| 40 | + pwsh Get-Binlog.ps1 |
| 41 | +
|
| 42 | +.EXAMPLE |
| 43 | + pwsh Get-Binlog.ps1 -BinlogPath artifacts/log/Debug/Build.binlog |
| 44 | +
|
| 45 | +.EXAMPLE |
| 46 | + pwsh Get-Binlog.ps1 -PrNumber 19941 |
| 47 | +
|
| 48 | +.EXAMPLE |
| 49 | + pwsh Get-Binlog.ps1 -BuildId 1462217 -Json |
| 50 | +#> |
| 51 | +[CmdletBinding(DefaultParameterSetName = 'Local')] |
| 52 | +param( |
| 53 | + [Parameter(ParameterSetName = 'ByPath', Mandatory, Position = 0)] |
| 54 | + [string[]]$BinlogPath, |
| 55 | + |
| 56 | + [Parameter(ParameterSetName = 'ByPr', Mandatory, Position = 0)] |
| 57 | + [int]$PrNumber, |
| 58 | + |
| 59 | + [Parameter(ParameterSetName = 'ByBuild', Mandatory, Position = 0)] |
| 60 | + [long]$BuildId, |
| 61 | + |
| 62 | + [string]$Org = 'dnceng-public', |
| 63 | + [string]$Project = 'public', |
| 64 | + [int]$Definition = 90, |
| 65 | + [switch]$AllLegs, |
| 66 | + [switch]$Json |
| 67 | +) |
| 68 | + |
| 69 | +$ErrorActionPreference = 'Stop' |
| 70 | +$api = "https://dev.azure.com/$Org/$Project/_apis/build" |
| 71 | + |
| 72 | +function Resolve-BuildId([int]$pr) { |
| 73 | + $url = "$api/builds?definitions=$Definition&reasonFilter=pullRequest&statusFilter=completed&`$top=100&api-version=7.1" |
| 74 | + $builds = (Invoke-RestMethod -Uri $url).value | |
| 75 | + Where-Object { $_.triggerInfo.'pr.number' -eq "$pr" } |
| 76 | + if (-not $builds) { throw "No completed PR builds found for PR #$pr (definition $Definition)." } |
| 77 | + $failed = $builds | Where-Object { $_.result -eq 'failed' } | Sort-Object finishTime -Descending |
| 78 | + $chosen = if ($failed) { $failed[0] } else { ($builds | Sort-Object finishTime -Descending)[0] } |
| 79 | + Write-Host "PR #$pr -> build $($chosen.id) ($($chosen.result), finished $($chosen.finishTime))" |
| 80 | + return $chosen.id |
| 81 | +} |
| 82 | + |
| 83 | +function Get-RepoRoot { (Resolve-Path (Join-Path $PSScriptRoot '..\..\..\..')).Path } |
| 84 | + |
| 85 | +function Resolve-LocalBinlogs([string[]]$paths, [bool]$all) { |
| 86 | + $out = [System.Collections.Generic.List[string]]::new() |
| 87 | + foreach ($p in $paths) { |
| 88 | + if (Test-Path -LiteralPath $p -PathType Leaf) { |
| 89 | + if ([IO.Path]::GetExtension($p) -eq '.binlog') { $out.Add((Resolve-Path -LiteralPath $p).Path) } |
| 90 | + else { Write-Warning "Skipping non-binlog file: $p" } |
| 91 | + } |
| 92 | + elseif (Test-Path -LiteralPath $p -PathType Container) { |
| 93 | + $found = Get-ChildItem -LiteralPath $p -Recurse -Filter *.binlog -ErrorAction SilentlyContinue | |
| 94 | + Sort-Object LastWriteTime -Descending |
| 95 | + if (-not $found) { throw "No *.binlog under directory: $p" } |
| 96 | + if ($all) { $found | ForEach-Object { $out.Add($_.FullName) } } else { $out.Add($found[0].FullName) } |
| 97 | + } |
| 98 | + else { |
| 99 | + $glob = Get-ChildItem -Path $p -ErrorAction SilentlyContinue | Where-Object { $_.Extension -eq '.binlog' } |
| 100 | + if (-not $glob) { throw "Path not found or no .binlog match: $p" } |
| 101 | + $glob | ForEach-Object { $out.Add($_.FullName) } |
| 102 | + } |
| 103 | + } |
| 104 | + return $out |
| 105 | +} |
| 106 | + |
| 107 | +$binlogs = [System.Collections.Generic.List[string]]::new() |
| 108 | + |
| 109 | +switch ($PSCmdlet.ParameterSetName) { |
| 110 | + 'Local' { |
| 111 | + $logDir = Join-Path (Get-RepoRoot) 'artifacts/log' |
| 112 | + if (-not (Test-Path $logDir)) { |
| 113 | + throw "No local build logs at '$logDir'. Build with a binary log first " + |
| 114 | + "(e.g. ./build.sh --binaryLog or eng/Build.ps1 -binaryLog), or pass -BinlogPath." |
| 115 | + } |
| 116 | + Write-Host "Auto-discovering newest binlog under $logDir ..." |
| 117 | + $binlogs = Resolve-LocalBinlogs @($logDir) $AllLegs.IsPresent |
| 118 | + } |
| 119 | + 'ByPath' { |
| 120 | + $binlogs = Resolve-LocalBinlogs $BinlogPath $AllLegs.IsPresent |
| 121 | + } |
| 122 | + default { |
| 123 | + # Azure DevOps modes (ByPr / ByBuild): download the build-leg binlog |
| 124 | + # artifact and KEEP it so the binlog-mcp MCP server can read it. |
| 125 | + if ($PSCmdlet.ParameterSetName -eq 'ByPr') { $BuildId = Resolve-BuildId $PrNumber } |
| 126 | + $artifacts = (Invoke-RestMethod -Uri "$api/builds/$BuildId/artifacts?api-version=7.1").value |
| 127 | + $selected = if ($AllLegs) { |
| 128 | + $artifacts | Where-Object { $_.name -match 'binlog' } |
| 129 | + } else { |
| 130 | + $build = $artifacts | Where-Object { $_.name -match 'build binlog' } |
| 131 | + if ($build) { $build } else { $artifacts | Where-Object { $_.name -match 'binlog' } } |
| 132 | + } |
| 133 | + if (-not $selected) { throw "Build $BuildId has no binlog artifacts." } |
| 134 | + |
| 135 | + $downloadDir = Join-Path ([IO.Path]::GetTempPath()) "binlog-analysis-$BuildId" |
| 136 | + if (Test-Path $downloadDir) { Remove-Item -Recurse -Force $downloadDir } |
| 137 | + New-Item -ItemType Directory -Force -Path $downloadDir | Out-Null |
| 138 | + foreach ($a in $selected) { |
| 139 | + $zip = Join-Path $downloadDir "$($a.name -replace '[^\w.-]', '_').zip" |
| 140 | + Write-Host "Downloading artifact: $($a.name)" |
| 141 | + Invoke-WebRequest -Uri $a.resource.downloadUrl -OutFile $zip |
| 142 | + $dest = Join-Path $downloadDir ($a.name -replace '[^\w.-]', '_') |
| 143 | + Expand-Archive -Path $zip -DestinationPath $dest -Force |
| 144 | + Get-ChildItem $dest -Recurse -Filter *.binlog | ForEach-Object { $binlogs.Add($_.FullName) } |
| 145 | + } |
| 146 | + Write-Host "Kept under: $downloadDir" |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +if ($binlogs.Count -eq 0) { throw "No .binlog files resolved." } |
| 151 | + |
| 152 | +if ($Json) { |
| 153 | + [pscustomobject]@{ binlogs = @($binlogs) } | ConvertTo-Json |
| 154 | + return |
| 155 | +} |
| 156 | + |
| 157 | +Write-Host '' |
| 158 | +Write-Host "Resolved $($binlogs.Count) binlog(s):" |
| 159 | +foreach ($b in $binlogs) { Write-Host " $b" } |
| 160 | +Write-Host '' |
| 161 | +Write-Host 'Next: analyze with the binlog-mcp MCP server (arg name is binlog_file):' |
| 162 | +Write-Host ' binlog_overview { binlog_file: "<path>" } # build status + error/warning counts' |
| 163 | +Write-Host ' binlog_diagnose { binlog_file: "<path>" } # categorized root causes + next steps' |
| 164 | +Write-Host ' binlog_errors { binlog_file: "<path>" } # structured errors (code/file/line/project)' |
| 165 | +Write-Host 'If the build succeeded / 0 errors (e.g. a test-only or formatting failure), there is nothing to analyze.' |
0 commit comments