diff --git a/.github/workflows/update-integration-data.yml b/.github/workflows/update-integration-data.yml index 0af7ede166..ef3bdde318 100644 --- a/.github/workflows/update-integration-data.yml +++ b/.github/workflows/update-integration-data.yml @@ -5,10 +5,11 @@ name: Integration Data Updater # opens a PR through the Aspire bot GitHub App when the generated data changes. # # The bot owns the rolling `chore/update-integration-data` branch and force-pushes -# it on every run, so nobody should commit to it. The PR is labeled -# `:octocat: auto-merge`, which .github/policies/auto-merge.yml turns into an -# approval and a squash auto-merge once the required `ci-gate` check passes. -# Remove the label to hold a PR for manual review. +# it on every run, so nobody should commit to it. When the updater's unit tests +# pass against the new data, the PR is labeled `:octocat: auto-merge`, which +# .github/policies/auto-merge.yml turns into an approval and a squash auto-merge +# once the required `ci-gate` check passes. Remove the label to hold a PR for +# manual review. # # All logic lives in src/frontend/scripts/update-integration-data.ps1 so the # exact same flow can be reproduced locally: @@ -92,6 +93,7 @@ jobs: shell: pwsh env: GITHUB_TOKEN: ${{ github.token }} + ASTRO_TELEMETRY_DISABLED: 1 run: ./src/frontend/scripts/update-integration-data.ps1 - name: Commit and refresh automation branch @@ -155,13 +157,19 @@ jobs: BRANCH: ${{ steps.push.outputs.branch }} PR_TITLE: ${{ steps.update.outputs.pr_title }} PR_BODY_FILE: ${{ steps.update.outputs.pr_body_file }} + AUTO_MERGE: ${{ steps.update.outputs.auto_merge }} shell: bash run: | set -euo pipefail + label=":octocat: auto-merge" + # Written by update-integration-data.ps1 when unit tests fail. + failure_marker="### :x: Unit tests failed" + # Reuse the existing open PR for the rolling branch if there is one, so # its review threads and history carry over; otherwise open a fresh PR. - # Adding the label at creation is what triggers the auto-merge policy. + # Adding the label is what triggers the auto-merge policy, so it is only + # applied when the updater's unit tests passed against the new data. existing=$(gh pr list \ --state open \ --head "$BRANCH" \ @@ -170,16 +178,31 @@ jobs: if [ -n "$existing" ]; then echo "Refreshing existing PR #$existing in place." + previous_body=$(gh pr view "$existing" --json body --jq .body) + has_label=$(gh pr view "$existing" --json labels \ + --jq "any(.labels[]; .name == \"$label\")") gh pr edit "$existing" \ --title "$PR_TITLE" \ --body-file "$PR_BODY_FILE" + if [ "$AUTO_MERGE" != "true" ] && [ "$has_label" = "true" ]; then + gh pr edit "$existing" --remove-label "$label" + elif [ "$AUTO_MERGE" = "true" ] && [ "$has_label" != "true" ] && + grep -qF -- "$failure_marker" <<< "$previous_body"; then + # Restore only a label this workflow removed; a maintainer's manual + # hold (label removed without the failure marker) is preserved. + gh pr edit "$existing" --add-label "$label" + fi else + label_args=() + if [ "$AUTO_MERGE" = "true" ]; then + label_args=(--label "$label") + fi new_pr=$(gh pr create \ --base main \ --head "$BRANCH" \ --title "$PR_TITLE" \ --body-file "$PR_BODY_FILE" \ - --label ":octocat: auto-merge") + "${label_args[@]}") echo "Created $new_pr" fi @@ -230,6 +253,7 @@ jobs: echo "- Versions changed: \`${{ steps.update.outputs.versions_changed || 'n/a' }}\`" echo "- API regen ran: \`${{ steps.update.outputs.regen_ran || 'n/a' }}\`" echo "- Semantic validation: \`${{ steps.update.outputs.semantic_validation || 'not run' }}\`" + echo "- Unit tests: \`${{ steps.update.outputs.unit_tests || 'not run' }}\`" if [ -n "${{ steps.update.outputs.icon_warnings }}" ]; then echo "- Icon warnings: \`${{ steps.update.outputs.icon_warnings }}\`" fi diff --git a/src/frontend/scripts/update-integration-data.ps1 b/src/frontend/scripts/update-integration-data.ps1 index 8be23b3906..c4a4bba82d 100644 --- a/src/frontend/scripts/update-integration-data.ps1 +++ b/src/frontend/scripts/update-integration-data.ps1 @@ -757,18 +757,16 @@ Write-Host "All changes are within the allowed data paths." -ForegroundColor Gre # ── File-count summary (for the PR body) ──────────────────────────────────── function Get-AreaCounts { param([Parameter(Mandatory)][string]$Prefix) - $names = @(Invoke-Git @('diff', '--name-status', 'HEAD', '--', $Prefix) | + # Generated files are untracked until the workflow stages them, so `git diff` + # alone misses every added file. Version bumps count as an add and a removal. + $names = @(Invoke-Git @('status', '--porcelain', '--untracked-files=all', '--', $Prefix) | Where-Object { $_ -and $_.Trim().Length -gt 0 }) $added = 0; $modified = 0; $removed = 0 foreach ($n in $names) { - $code = ($n.Trim())[0] - switch ($code) { - 'A' { $added++ } - 'M' { $modified++ } - 'D' { $removed++ } - 'R' { $modified++ } - default { } - } + $code = $n.Substring(0, 2) + if ($code -eq '??' -or $code.Contains('A')) { $added++ } + elseif ($code.Contains('D')) { $removed++ } + elseif ($code -match '[MR]') { $modified++ } } return [PSCustomObject]@{ Added = $added; Modified = $modified; Removed = $removed } } @@ -779,6 +777,57 @@ $goModulesCounts = Get-AreaCounts -Prefix 'src/frontend/src/data/go-modules/' $twoslashChanged = @(Invoke-Git @('diff', '--name-only', 'HEAD', '--', 'src/frontend/src/data/twoslash/aspire.d.ts') | Where-Object { $_ -and $_.Trim().Length -gt 0 }).Count -gt 0 +# ── Phase 6: CI unit tests ────────────────────────────────────────────────── +# The semantic gate above only checks generated API data. CI also runs data- +# dependent unit tests (API reference resolution, synced sample content), so run +# them here: a failure still refreshes the PR, but without auto-merge. +Write-Section 'Phase 6 — bun run test:unit' +# Tests run after the scope check, and the workflow stages the allowed paths +# afterwards, so any test that rewrites generated data would leak into the PR. +function Get-DataSnapshot { + $paths = @(Invoke-Git (@('status', '--porcelain', '--untracked-files=all', '--') + $AllowedPaths) | + Where-Object { $_ -and $_.Trim().Length -gt 0 } | + ForEach-Object { (($_.Substring(3)) -split ' -> ')[-1].Trim('"') }) + @($paths | Sort-Object | ForEach-Object { + $full = Join-Path $RepoRoot $_ + $hash = if (Test-Path -LiteralPath $full -PathType Leaf) { (Get-FileHash -LiteralPath $full).Hash } else { 'missing' } + "$_ $hash" + }) -join "`n" +} +$dataSnapshot = Get-DataSnapshot +Push-Location $FrontendDir +try { + $unitTestLog = & bun run test:unit 2>&1 | Tee-Object -Variable unitTestTeed | Out-String + $unitTestExit = $LASTEXITCODE +} +finally { + Pop-Location +} +if ((Get-DataSnapshot) -ne $dataSnapshot) { + Write-Error "bun run test:unit modified generated data under the allowed paths. Aborting; no PR will be opened." + exit 1 +} +$unitTestFailures = @($unitTestLog -split "`r?`n" | + ForEach-Object { $_ -replace '\x1b\[[0-9;]*m', '' } | + Where-Object { $_ -match '^\s*FAIL\s+(?\S.*?)\s*$' } | + ForEach-Object { $Matches.Test } | + Select-Object -Unique) +$unitTestsPassed = $unitTestExit -eq 0 +if (-not $unitTestsPassed) { + if ($unitTestFailures.Count -eq 0) { + $unitTestFailures = @("bun run test:unit exited with code $unitTestExit") + } + $unitTestWarning = "Unit tests failed against the updated data; the PR will not auto-merge." + Write-Warning $unitTestWarning + foreach ($failure in $unitTestFailures) { Write-Warning " $failure" } + if ($IsCI) { + Write-Host "::warning title=Unit tests failed::$unitTestWarning" + } +} +else { + Write-Host "Unit tests passed." -ForegroundColor Green +} + # ── PR title + body ───────────────────────────────────────────────────────── $dateShort = [DateTime]::UtcNow.ToString('M/d/yy', [System.Globalization.CultureInfo]::InvariantCulture) $prTitle = "chore: Update integration data and GitHub stats ($dateShort)" @@ -788,7 +837,16 @@ $sb = [System.Text.StringBuilder]::new() [void]$sb.AppendLine("") [void]$sb.AppendLine("Generated deterministically by ``src/frontend/scripts/update-integration-data.ps1`` (no agent inference).") [void]$sb.AppendLine("") -[void]$sb.AppendLine("This PR is approved and squash-merged automatically once ``ci-gate`` passes, as configured in ``.github/policies/auto-merge.yml``. Remove the ``:octocat: auto-merge`` label to hold it for manual review.") +if ($unitTestsPassed) { + [void]$sb.AppendLine("This PR is approved and squash-merged automatically once ``ci-gate`` passes, as configured in ``.github/policies/auto-merge.yml``. Remove the ``:octocat: auto-merge`` label to hold it for manual review.") +} +else { + [void]$sb.AppendLine("### :x: Unit tests failed") + [void]$sb.AppendLine("") + [void]$sb.AppendLine("``bun run test:unit`` failed against this data, so this PR is **not** labeled for auto-merge. Fix the failures on ``main``; the next updater run refreshes this PR and restores the ``:octocat: auto-merge`` label once the tests pass.") + [void]$sb.AppendLine("") + foreach ($failure in $unitTestFailures) { [void]$sb.AppendLine("- ``$failure``") } +} [void]$sb.AppendLine("") [void]$sb.AppendLine("### What's updated") [void]$sb.AppendLine("- ``src/frontend/src/data/aspire-integrations.json`` — latest package information") @@ -914,6 +972,8 @@ Set-Output 'pr_title' $prTitle Set-Output 'pr_body_file' $prBodyFile Set-Output 'icon_warnings' $iconWarnings Set-Output 'api_failures' ($apiFailureDetails -join "`n") +Set-Output 'unit_tests' $(if ($unitTestsPassed) { 'passed' } else { 'failed' }) +Set-Output 'auto_merge' ($unitTestsPassed.ToString().ToLowerInvariant()) Write-Host "PR title : $prTitle" Write-Host "PR body : $prBodyFile" diff --git a/src/frontend/src/data/api-generation-failures.json b/src/frontend/src/data/api-generation-failures.json index dc9c92c250..18a8f67f01 100644 --- a/src/frontend/src/data/api-generation-failures.json +++ b/src/frontend/src/data/api-generation-failures.json @@ -1,16 +1,4 @@ [ - { - "name": "Aspire.Hosting.Azure.ApiManagement", - "version": "17.0.0-preview.1.26567.5", - "stage": "TypeScript", - "reason": "Declared export Aspire.Hosting.Azure.AzureApiManagementResource.Identity has no matching generated TypeScript API." - }, - { - "name": "Aspire.Hosting.Kubernetes", - "version": "17.0.0", - "stage": "TypeScript", - "reason": "Declared export Aspire.Hosting.Kubernetes.Resources.BaseKubernetesResource.Metadata has no matching generated TypeScript API." - }, { "name": "CommunityToolkit.Aspire.Hosting.Chroma", "version": "17.0.0-beta.1052", diff --git a/src/frontend/src/utils/api-reference-core.ts b/src/frontend/src/utils/api-reference-core.ts index a4b5e65ffd..0866ebc891 100644 --- a/src/frontend/src/utils/api-reference-core.ts +++ b/src/frontend/src/utils/api-reference-core.ts @@ -67,6 +67,7 @@ export interface ApiReferenceTsCallable { export interface ApiReferenceTsHandle { name: string; fullName?: string; + baseTypeHierarchy?: string[]; capabilities?: ApiReferenceTsCallable[]; } diff --git a/src/frontend/src/utils/api-reference/typescript-provider.ts b/src/frontend/src/utils/api-reference/typescript-provider.ts index b16f5c9a20..dd3eb54238 100644 --- a/src/frontend/src/utils/api-reference/typescript-provider.ts +++ b/src/frontend/src/utils/api-reference/typescript-provider.ts @@ -28,6 +28,7 @@ interface TsRouteCandidate { targetTypeId?: string; expandedTargetTypes: string[]; parentTypeFullName?: string; + parentBaseTypeFullNames: string[]; path: string; priority: number; parameters?: ApiReferenceTsCallable['parameters']; @@ -103,6 +104,7 @@ function buildTsRouteIndex(modules: readonly ApiReferenceTsDocument[]): TsRouteI qualifiedName: fn.qualifiedName, targetTypeId: fn.targetTypeId, expandedTargetTypes: fn.expandedTargetTypes ?? [], + parentBaseTypeFullNames: [], path: `${modulePath}/${getTsItemSlug(fn, topLevelItems)}/`, priority: 0, parameters: fn.parameters, @@ -136,6 +138,7 @@ function buildTsRouteIndex(modules: readonly ApiReferenceTsDocument[]): TsRouteI targetTypeId: capability.targetTypeId, expandedTargetTypes: capability.expandedTargetTypes ?? [], parentTypeFullName: handle.fullName, + parentBaseTypeFullNames: handle.baseTypeHierarchy ?? [], path, priority, parameters: capability.parameters, @@ -172,6 +175,15 @@ function candidateMatchesMapping(route: TsRouteCandidate, mapping: ExportMapping ); } +// ATS projects members of non-exported base types onto each exported derived +// handle, so a base-type member has no capability of its own. +function candidateInheritsMapping(route: TsRouteCandidate, mapping: ExportMapping): boolean { + const normalizedExpectedTypes = mapping.declaringTypeNames.map(normalizeTypeName); + return route.parentBaseTypeFullNames.some((baseType) => + normalizedExpectedTypes.includes(normalizeTypeName(baseType)) + ); +} + function deduplicateTsCandidates(candidates: readonly TsRouteCandidate[]): TsRouteCandidate[] { const unique = new Map(); for (const candidate of candidates) { @@ -228,6 +240,15 @@ function findTsCandidates( return { matches: preferredTsCandidates(named), suggestions: named }; } + // Only when the declaring type has no capability of its own, so existing + // base-type references don't become ambiguous with derived projections. + const inherited = (tsIndex.byName.get(mapping.methodName.toLowerCase()) ?? []).filter((route) => + candidateInheritsMapping(route, mapping) + ); + if (inherited.length > 0) { + return { matches: preferredTsCandidates(inherited), suggestions: inherited }; + } + return { matches: [], suggestions: named }; } diff --git a/src/frontend/tests/unit/api-reference.vitest.test.ts b/src/frontend/tests/unit/api-reference.vitest.test.ts index 76d2628aa4..a96f4bcc92 100644 --- a/src/frontend/tests/unit/api-reference.vitest.test.ts +++ b/src/frontend/tests/unit/api-reference.vitest.test.ts @@ -1360,6 +1360,12 @@ describe('API reference authoring validator', () => { '/reference/api/typescript/aspire.hosting/distributedapplicationexecutioncontext/#ispublishmode' ); + const knownTypeScriptExportGaps = new Set([ + // Experimental export. + 'Aspire.Hosting.RadiusExtensions.WithContainerImage', + // Declared [AspireExport] upstream, but absent from the generated ATS capabilities. + 'Aspire.Hosting.Azure.AzureApiManagementResource.Identity', + ]); const declaredExportFailures: string[] = []; const checkedExports = new Set(); for (const pkg of packageDocuments) { @@ -1378,8 +1384,9 @@ describe('API reference authoring validator', () => { if (!hasMemberExport) continue; const fqn = `${typeName}.${member.name}`; - // Experimental export that the TypeScript generator currently omits. - if (fqn === 'Aspire.Hosting.RadiusExtensions.WithContainerImage') continue; + // Exports the TypeScript generator currently omits. Keep these here, not in + // api-generation-failures.json, which the integration data updater rewrites. + if (knownTypeScriptExportGaps.has(fqn)) continue; const key = `${pkg.package.name}\0${fqn}`; if (checkedExports.has(key)) continue; checkedExports.add(key); diff --git a/src/frontend/tests/unit/dashboard-image-references.vitest.test.ts b/src/frontend/tests/unit/dashboard-image-references.vitest.test.ts index 833e1a0c81..5df31bf723 100644 --- a/src/frontend/tests/unit/dashboard-image-references.vitest.test.ts +++ b/src/frontend/tests/unit/dashboard-image-references.vitest.test.ts @@ -139,12 +139,17 @@ describe('standalone dashboard sample image', () => { test('preserves the upstream nightly version pin in both README fields', () => { const sample = samples.find((entry) => entry.name === 'standalone-dashboard'); expect(sample).toBeDefined(); + // samples.json is synced from upstream, which bumps the pinned major.minor each release. + const pinnedImage = sample!.readme.match( + /mcr\.microsoft\.com\/aspire\/nightly\/dashboard:\d+\.\d+(?=[\s`])/ + )?.[0]; + expect(pinnedImage).toBeDefined(); for (const field of ['readme', 'readmeRaw'] as const) { const source = sample![field]; const images = source.match(/mcr\.microsoft\.com\/[^\s`]+/g); - expect(images).toEqual([versionedNightlyImage, versionedNightlyImage]); - expect(source).toContain(`--name aspire-dashboard ${versionedNightlyImage}`); - expect(source).toContain(`\`${versionedNightlyImage}\` prerelease image.`); + expect(images).toEqual([pinnedImage, pinnedImage]); + expect(source).toContain(`--name aspire-dashboard ${pinnedImage}`); + expect(source).toContain(`\`${pinnedImage}\` prerelease image.`); } }); }); diff --git a/src/frontend/tests/unit/integration-data-updater.vitest.test.ts b/src/frontend/tests/unit/integration-data-updater.vitest.test.ts index dbda0095cb..fe3affc91b 100644 --- a/src/frontend/tests/unit/integration-data-updater.vitest.test.ts +++ b/src/frontend/tests/unit/integration-data-updater.vitest.test.ts @@ -228,6 +228,15 @@ function global:bun { & node $env:FIXTURE_TSX (Join-Path $PSScriptRoot 'src/frontend/scripts/validate-generated-api-data.ts') $global:LASTEXITCODE = $LASTEXITCODE } + 'test:unit' { + if ($env:FIXTURE_MODE -eq 'unit-test-failure') { + Write-Output "$([char]27)[31m FAIL $([char]27)[39m tests/unit/data.vitest.test.ts > data > matches upstream" + $global:LASTEXITCODE = 1 + } + if ($env:FIXTURE_MODE -eq 'unit-test-mutation') { + Set-Content (Join-Path $data 'twoslash/aspire.d.ts') 'rewritten by a test' + } + } } } & "$PSScriptRoot/src/frontend/scripts/update-integration-data.ps1" @@ -247,6 +256,7 @@ exit $LASTEXITCODE 'Mocks' ); const output = path.join(root, 'outputs.txt'); + const bodyFile = path.join(root, 'integration-pr-body.md'); const result = spawnSync('pwsh', ['-NoProfile', '-File', wrapper], { cwd: root, encoding: 'utf8', @@ -263,6 +273,7 @@ exit $LASTEXITCODE ...result, data, outputs: fs.existsSync(output) ? fs.readFileSync(output, 'utf8') : '', + body: fs.existsSync(bodyFile) ? fs.readFileSync(bodyFile, 'utf8') : '', failures: JSON.parse( fs.readFileSync(path.join(data, 'api-generation-failures.json'), 'utf8') ) as ApiGenerationFailure[], @@ -280,6 +291,26 @@ describe('integration data orchestration', () => { expect(run.failures).toHaveLength(2); expect(run.failures[0].reason).toContain('NU1102'); expect(run.outputs).toContain('api_failures_checked<<'); + // Good moves 1.0.0 -> 2.0.0; the new file is untracked until the workflow stages it. + expect(run.body).toContain('| `src/frontend/src/data/pkgs/**` | 1 | 0 | 1 |'); + expect(run.outputs).toMatch(/auto_merge<<(\S+)\ntrue\n/); + }); + + test('withholds auto-merge and lists failing unit tests', () => { + const run = fixture('unit-test-failure'); + expect(run.status, run.stdout + run.stderr).toBe(0); + expect(run.outputs).toMatch(/changed<<(\S+)\ntrue\n/); + expect(run.outputs).toMatch(/auto_merge<<(\S+)\nfalse\n/); + expect(run.body).toContain('### :x: Unit tests failed'); + expect(run.body).toContain('- `tests/unit/data.vitest.test.ts > data > matches upstream`'); + expect(run.body).not.toContain('approved and squash-merged automatically'); + }); + + test('refuses to publish when unit tests rewrite generated data', () => { + const run = fixture('unit-test-mutation'); + expect(run.status, run.stdout + run.stderr).toBe(1); + expect(run.stdout + run.stderr).toContain('modified generated data'); + expect(run.outputs).not.toContain('changed<<'); }); test('restores a matching C#/TypeScript pair when only the TS generation fails', () => { diff --git a/src/frontend/tests/unit/twoslash-types-generator.vitest.test.ts b/src/frontend/tests/unit/twoslash-types-generator.vitest.test.ts index 5cd6725d89..6729ac1d50 100644 --- a/src/frontend/tests/unit/twoslash-types-generator.vitest.test.ts +++ b/src/frontend/tests/unit/twoslash-types-generator.vitest.test.ts @@ -11,26 +11,35 @@ import { import { tmpdir } from 'node:os'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; -import { beforeAll, describe, expect, test } from 'vitest'; +import { afterAll, beforeAll, describe, expect, test } from 'vitest'; import { hasNestedSingleDtoOptionsWrapper } from '../../scripts/validate-generated-api-data'; const testsDir = path.dirname(fileURLToPath(import.meta.url)); const frontendRoot = path.resolve(testsDir, '..', '..'); const generatorScript = path.join(frontendRoot, 'scripts', 'generate-twoslash-types.ts'); -const outputFile = path.join(frontendRoot, 'src', 'data', 'twoslash', 'aspire.d.ts'); +const outputDir = mkdtempSync(path.join(tmpdir(), 'aspire-twoslash-output-')); +const outputFile = path.join(outputDir, 'aspire.d.ts'); let output = ''; beforeAll(() => { - // The output is source-controlled, so don't unlink it. Re-run the generator - // in place; subsequent assertions read the refreshed content. + // Generate from the committed API data into a temp file: the real output is + // source-controlled, and the integration data updater stages it after tests run. // Invoke tsx directly via node to avoid cross-platform package manager shim // resolution issues (and the DEP0190 warning from `shell: true`). const tsxBin = path.join(frontendRoot, 'node_modules', 'tsx', 'dist', 'cli.mjs'); - execFileSync(process.execPath, [tsxBin, generatorScript], { cwd: frontendRoot, stdio: 'pipe' }); + execFileSync(process.execPath, [tsxBin, generatorScript], { + cwd: frontendRoot, + env: { ...process.env, ASPIRE_API_TWOSLASH_FILE: outputFile }, + stdio: 'pipe', + }); output = readFileSync(outputFile, 'utf8'); }, 60_000); +afterAll(() => { + rmSync(outputDir, { recursive: true, force: true }); +}); + describe('generate-twoslash-types', () => { test('keeps enum literals from independent packages sharing an exported short name', () => { const fixtureRoot = mkdtempSync(path.join(tmpdir(), 'aspire-enum-collision-'));