Repository: dotnet/runtime
File: .github/skills/vmr-codeflow-status/scripts/Get-CodeflowStatus.ps1 (PowerShell)

Code before:
    else {
        # No branch-derived snapshot — PR body only
        $commitCount = if ($prCommits) { $prCommits.Count } else { 0 }
        if ($commitCount -eq 1 -and $prCommits[0].messageHeadline -match "^Initial commit for subscription") {
            Write-Host "  ℹ️  PR has only an initial subscription commit — PR body snapshot ($(Get-ShortSha $vmrCommit)) not yet verifiable" -ForegroundColor DarkGray
        }
        else {
            Write-Host "  ⚠️  Could not verify PR body snapshot ($(Get-ShortSha $vmrCommit)) from branch" -ForegroundColor Yellow
        }
    }
}

# --- Step 3: Check source freshness ---
$freshnessLabel = if ($isForwardFlow) { "Source Freshness" } else { "VMR Freshness" }
Write-Section $freshnessLabel

$sourceHeadSha = $null
$aheadBy = 0
$behindBy = 0
$compareStatus = $null

if ($vmrCommit -and $vmrBranch) {
    # Get current branch HEAD (URL-encode branch name for path segments with /)
    $encodedBranch = [uri]::EscapeDataString($vmrBranch)
    $branchHead = Invoke-GitHubApi "/repos/$freshnessRepo/commits/$encodedBranch"
    if ($branchHead) {
        $sourceHeadSha = $branchHead.sha
        $sourceHeadDate = $branchHead.commit.committer.date
        $snapshotSource = if ($usedBranchSnapshot) {
            if ($versionDetailsVmrCommit -and $vmrCommit.StartsWith($versionDetailsVmrCommit)) { "from Version.Details.xml" }
            elseif ($commitMsgVmrCommit) { "from branch commit" }
            else { "from branch" }
        } else { "from PR body" }
        Write-Status "PR snapshot" "$(Get-ShortSha $vmrCommit) ($snapshotSource)"
        Write-Status "$freshnessRepoLabel HEAD" "$(Get-ShortSha $sourceHeadSha) ($sourceHeadDate)"

        if ($vmrCommit -eq $sourceHeadSha) {
            Write-Host "  ✅ PR is up to date with $freshnessRepoLabel branch" -ForegroundColor Green
        }
        else {
            # Compare to find how many commits differ
            $compare = Invoke-GitHubApi "/repos/$freshnessRepo/compare/$vmrCommit...$sourceHeadSha"
            if ($compare) {
                $aheadBy = $compare.ahead_by
                $behindBy = $compare.behind_by
                $compareStatus = $compare.status

                switch ($compareStatus) {
                    'identical' {
                        Write-Host "  ✅ PR is up to date with $freshnessRepoLabel branch" -ForegroundColor Green
                    }

Review comment (bug):
The GitHub `/contents/...` endpoint returns a JSON payload by default (with Base64-encoded `content`). If `Invoke-GitHubApi -Raw` doesn’t explicitly request the raw media type (or decode JSON content), `$vdContent` may not be the XML text and the regex will never match, causing Version.Details.xml fallback to silently fail. Consider updating `Invoke-GitHubApi` usage here to reliably fetch raw file contents (e.g., by setting the appropriate `Accept` header) or decode the JSON response’s `content` before parsing.
```suggestion
    $vdResponse = Invoke-GitHubApi "/repos/$Repository/contents/eng/Version.Details.xml?ref=$([System.Uri]::EscapeDataString($pr.headRefName))" -Raw
    $vdContent = $null
    if ($vdResponse) {
        try {
            # Default GitHub /contents response is JSON with Base64-encoded "content"
            $vdJson = $vdResponse | ConvertFrom-Json
            if ($vdJson -and $vdJson.content) {
                $decodedBytes = [Convert]::FromBase64String($vdJson.content)
                $vdContent = [System.Text.Encoding]::UTF8.GetString($decodedBytes)
            }
        }
        catch {
            # If the response is not JSON (e.g., already raw XML), fall back to using it directly
            $vdContent = $vdResponse
        }
        if (-not $vdContent) {
            # No JSON content or decoding failed; use the raw response as a best-effort fallback
            $vdContent = $vdResponse
        }
    }
```

Code after:
            if (-not $vmrBranch) { $vmrBranch = $pr.baseRefName }
        }
        else {
            $vmrBranch = $pr.baseRefName
        }
        Write-Status "Inferred Branch" "$vmrBranch (from PR metadata)"
    }
}

# For backflow: compare against VMR (dotnet/dotnet) branch HEAD
# For forward flow: compare against product repo branch HEAD
$freshnessRepo = if ($isForwardFlow) { $sourceRepo } else { "dotnet/dotnet" }
$freshnessRepoLabel = if ($isForwardFlow) { $sourceRepo } else { "VMR" }

# Pre-load PR commits for use in validation and later analysis
$prCommits = $pr.commits

# --- Step 2b: Determine actual VMR snapshot on the PR branch ---
# Priority: 1) Version.Details.xml (ground truth), 2) commit messages, 3) PR body
$branchVmrCommit = $null
$commitMsgVmrCommit = $null
$versionDetailsVmrCommit = $null

# First: check eng/Version.Details.xml on the PR branch (authoritative source)
if (-not $isForwardFlow) {
    $vdContent = Invoke-GitHubApi "/repos/$Repository/contents/eng/Version.Details.xml?ref=$([System.Uri]::EscapeDataString($pr.headRefName))" -Raw
    if ($vdContent) {
        try {
            [xml]$vdXml = $vdContent
            $sourceNode = $vdXml.Dependencies.Source
            if ($sourceNode -and $sourceNode.Sha -and $sourceNode.Sha -match '^[a-fA-F0-9]{40}$') {
                $versionDetailsVmrCommit = $sourceNode.Sha
                $branchVmrCommit = $versionDetailsVmrCommit
            }
        }
        catch {
            # Fall back to regex if XML parsing fails
            if ($vdContent -match '<Source\s+[^>]*Sha="([a-fA-F0-9]{40})"') {
                $versionDetailsVmrCommit = $Matches[1]
                $branchVmrCommit = $versionDetailsVmrCommit
            }
        }
    }
}

# Second: scan commit messages for "Backflow from" / "Forward flow from" SHAs
if ($prCommits) {
    $reversedCommits = @($prCommits)
    [Array]::Reverse($reversedCommits)
    foreach ($c in $reversedCommits) {
        $msg = $c.messageHeadline