Private/Get-CCNuGetPolicyRow.ps1
|
function Get-CCNuGetPolicyRow { <# .SYNOPSIS One readiness row for each workflow that logs in to NuGet by trusted publishing: when that login was last seen to work. .DESCRIPTION A release that publishes to NuGet by trusted publishing fails at its login when the policy on nuget.org is missing or names another repository or workflow file, and by then the tag is pushed. The policy cannot be read from outside nuget.org: there is no public sign of it and no API for its owner, and the login itself takes a token that only a workflow run is given. What can be read is GitHub: whether a run of the workflow has logged in. Such a run is the one real test, and a dry run of the release makes it without publishing anything. The row is Pass where a run logged in, with its day and its address, and a Warning where none did, with what to do. It never fails a release: a login that worked once does not prove the policy is there today, and a missing run does not prove it is not. .NOTES Read on 2026-12-04 from the NuGet documentation and the source of nuget.org (NuGet/NuGetGallery) and of the action (NuGet/login). A policy for a private repository is active for 7 days until its first login, and that login makes it permanent. #> param( [Parameter(Mandatory)][string]$Path, # Do not ask GitHub: the row then says that it was not asked. [switch]$NoAsk ) $folder = Join-Path (Join-Path $Path '.github') 'workflows' if (-not (Test-Path -LiteralPath $folder -PathType Container)) { return } $row = { param([string]$Status, [string]$Item, [string]$Message) [pscustomobject]@{ Check = 'release.nuget-policy'; Status = $Status; Item = $Item; Message = $Message } } $unread = 'The trusted publishing policy on nuget.org cannot be read from outside, and its first login would be the release itself, after the tag.' foreach ($file in @(Get-ChildItem -LiteralPath $folder -File | Where-Object { $_.Extension -in '.yml', '.yaml' } | Sort-Object Name)) { $lines = [IO.File]::ReadAllLines($file.FullName) $at = -1 for ($i = 0; $i -lt $lines.Count; $i++) { if ($lines[$i] -match '^\s*(-\s+)?uses:\s*NuGet/login@(\S+)') { $at = $i; $action = "NuGet/login@$($Matches[2])"; break } } if ($at -lt 0) { continue } # The name of the step, as a run lists it: the one it is given, or "Run" and the action. $first = $at while ($first -gt 0 -and $lines[$first] -notmatch '^\s*-\s') { $first-- } $step = "Run $action" for ($i = $first; $i -lt $lines.Count -and ($i -eq $first -or $lines[$i] -notmatch '^\s*-\s'); $i++) { if ($lines[$i] -match '^\s*(-\s+)?name:\s*(.+?)\s*$') { $step = $Matches[2].Trim('"', "'"); break } } $text = $lines -join "`n" $byHand = $text -match '(?m)^\s+workflow_dispatch:' -and $text -match '(?m)^\s+dry_run:' $todo = if ($byHand) { "A dry run logs in and publishes nothing: gh workflow run $($file.Name) -f dry_run=true" } else { "$($file.Name) has no dry run: look at the policy on nuget.org, under Trusted Publishing. It names the owner, the repository and the workflow file $($file.Name)." } if ($NoAsk) { & $row 'Skipped' $file.Name "GitHub was not asked whether $($file.Name) has logged in to NuGet. $unread $todo" continue } $runs = Invoke-CCGh -Path $Path -Arguments @('run', 'list', '--workflow', $file.Name, '--status', 'success', '--limit', '5', '--json', 'databaseId,event,createdAt,url') if (-not $runs.Ok) { & $row 'Warning' $file.Name "GitHub could not be asked for the runs of $($file.Name): $("$($runs.Error) $($runs.Output)".Trim()) $unread $todo" continue } $seen = $null foreach ($run in @(if ($runs.Output) { $runs.Output | ConvertFrom-Json })) { $jobs = Invoke-CCGh -Path $Path -Arguments @('run', 'view', "$($run.databaseId)", '--json', 'jobs') if (-not $jobs.Ok -or -not $jobs.Output) { continue } $steps = @(($jobs.Output | ConvertFrom-Json).jobs | ForEach-Object { $_.steps }) if ($steps | Where-Object { $_.name -eq $step -and $_.conclusion -eq 'success' }) { $seen = $run; break } } if ($seen) { $day = ([datetime]$seen.createdAt).ToUniversalTime().ToString('yyyy-MM-dd') $how = if ($seen.event -eq 'workflow_dispatch') { 'a run started by hand' } else { "a run from a $($seen.event)" } & $row 'Pass' $file.Name "$($file.Name) logged in to NuGet on $day, in ${how}: $($seen.url) The policy matched then. Nothing outside nuget.org can read it, so that is the last time it was seen to work." } else { & $row 'Warning' $file.Name "No run of $($file.Name) has been seen to log in to NuGet (step '$step'). $unread A policy for a private repository is active for 7 days until its first login. $todo" } } } |