From 5618e36f3195ec3e6cf123f1216a683cd7649e1e Mon Sep 17 00:00:00 2001 From: Weidong Xu Date: Tue, 24 Nov 2026 11:08:38 +0800 Subject: [PATCH 1/4] Generate Java release API hashes from staged sources Reuse the ARH executable for release PackageInfo ApiHash metadata, including source-built patch packages. Preserve metadata through existing signed-artifact transfers without adding approval or publication gates. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d47c40e0-dce3-4856-b258-f62b42922e1b --- eng/pipelines/templates/jobs/ci.yml | 2 + .../stages/archetype-sdk-client-patch.yml | 2 + .../templates/steps/save-package-api-hash.yml | 32 +++++ eng/scripts/Save-Package-ApiHash-Property.ps1 | 88 ++++++++++++ eng/scripts/Stage-ApiReviewTools.ps1 | 15 +- eng/scripts/tests/ApiReviewHub.tests.ps1 | 17 +++ .../Save-Package-ApiHash-Property.tests.ps1 | 136 ++++++++++++++++++ sdk/tools/api-review-maven-plugin/README.md | 2 + 8 files changed, 293 insertions(+), 1 deletion(-) create mode 100644 eng/pipelines/templates/steps/save-package-api-hash.yml create mode 100644 eng/scripts/Save-Package-ApiHash-Property.ps1 create mode 100644 eng/scripts/tests/Save-Package-ApiHash-Property.tests.ps1 diff --git a/eng/pipelines/templates/jobs/ci.yml b/eng/pipelines/templates/jobs/ci.yml index a84a46a8d1c89..2b502e56b5f9a 100644 --- a/eng/pipelines/templates/jobs/ci.yml +++ b/eng/pipelines/templates/jobs/ci.yml @@ -264,6 +264,8 @@ jobs: -PackageInfoDir $(Build.ArtifactStagingDirectory)/PackageInfo -InformationAction Continue + - template: /eng/pipelines/templates/steps/save-package-api-hash.yml + - task: Powershell@2 inputs: filePath: $(Build.SourcesDirectory)/eng/scripts/Save-Package-Namespaces-Property.ps1 diff --git a/eng/pipelines/templates/stages/archetype-sdk-client-patch.yml b/eng/pipelines/templates/stages/archetype-sdk-client-patch.yml index 1b2e3c56e6b2b..2b200f47593d5 100644 --- a/eng/pipelines/templates/stages/archetype-sdk-client-patch.yml +++ b/eng/pipelines/templates/stages/archetype-sdk-client-patch.yml @@ -181,6 +181,8 @@ extends: -Artifacts ('$(ArtifactsJsonEscaped)' | ConvertFrom-Json | Where-Object -Not skipPublishPackage) -InformationAction Continue + - template: /eng/pipelines/templates/steps/save-package-api-hash.yml + - task: Powershell@2 inputs: filePath: $(Build.SourcesDirectory)/eng/scripts/Save-Package-Namespaces-Property.ps1 diff --git a/eng/pipelines/templates/steps/save-package-api-hash.yml b/eng/pipelines/templates/steps/save-package-api-hash.yml new file mode 100644 index 0000000000000..8b94ac512954e --- /dev/null +++ b/eng/pipelines/templates/steps/save-package-api-hash.yml @@ -0,0 +1,32 @@ +parameters: + - name: ArtifactStagingDirectory + type: string + default: $(Build.ArtifactStagingDirectory) + +steps: + - task: JavaToolInstaller@0 + displayName: Use Java 17 for API hashing + inputs: + versionSpec: '17' + jdkArchitectureOption: x64 + jdkSourceOption: PreInstalled + + - task: PowerShell@2 + displayName: Build Java API hash tooling + inputs: + pwsh: true + targetType: filePath + filePath: $(Build.SourcesDirectory)/eng/scripts/Stage-ApiReviewTools.ps1 + arguments: >- + -BinOutputDirectory "$(Build.BinariesDirectory)/api-review-tools" + -ExpandSparseCheckout + + - task: PowerShell@2 + displayName: Save API hashes from staged release sources + inputs: + pwsh: true + targetType: filePath + filePath: $(Build.SourcesDirectory)/eng/scripts/Save-Package-ApiHash-Property.ps1 + arguments: >- + -ArtifactStagingDirectory "${{ parameters.ArtifactStagingDirectory }}" + -ToolJar "$(Build.BinariesDirectory)/api-review-tools/api-review-maven-plugin.jar" diff --git a/eng/scripts/Save-Package-ApiHash-Property.ps1 b/eng/scripts/Save-Package-ApiHash-Property.ps1 new file mode 100644 index 0000000000000..67b89182b6809 --- /dev/null +++ b/eng/scripts/Save-Package-ApiHash-Property.ps1 @@ -0,0 +1,88 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. +#Requires -Version 7.0 + +[CmdletBinding(SupportsShouldProcess)] +param( + [Parameter(Mandatory)][string]$ArtifactStagingDirectory, + [Parameter(Mandatory)][string]$ToolJar +) + +Set-StrictMode -Version 4 +$ErrorActionPreference = 'Stop' +$PSNativeCommandUseErrorActionPreference = $false + +. (Join-Path $PSScriptRoot 'MavenPackaging.ps1') + +$root = (Resolve-Path -LiteralPath $ArtifactStagingDirectory).Path +$tool = (Resolve-Path -LiteralPath $ToolJar).Path +$packages = @(Get-MavenPackageDetails -ArtifactDirectory $root) +$updates = @{} + +foreach ($file in Get-ChildItem -LiteralPath (Join-Path $root 'PackageInfo') -Filter '*.json' -File) { + $info = Get-Content -LiteralPath $file.FullName -Raw | ConvertFrom-Json + $version = $info.Version + if ($info.PSObject.Properties.Name -contains 'DevVersion' -and $info.DevVersion) { + $version = $info.DevVersion + } + $candidates = @($packages | Where-Object { + $_.GroupID -eq $info.Group -and $_.ArtifactID -eq $info.ArtifactName + }) + if ($candidates.Count -eq 0) { + continue + } + if ($candidates.Count -ne 1) { + throw "Expected one staged package for $($info.Group):$($info.ArtifactName):$version." + } + $package = $candidates[0] + if ($package.Version -ne $version) { + throw "Staged package version does not match PackageInfo for $($package.FullyQualifiedName)." + } + $binaries = @($package.AssociatedArtifacts | Where-Object { + -not $_.Classifier -and $_.Type -in @('jar', 'aar') + }) + if ($binaries.Count -eq 0) { + continue + } + $sources = @($package.AssociatedArtifacts | Where-Object { + $_.Classifier -eq 'sources' -and $_.Type -eq 'jar' + }) + if ($sources.Count -ne 1) { + throw "Expected one staged sources JAR for $($package.FullyQualifiedName)." + } + $path = $info.DirectoryPath.Replace('\', '/') + if ($path -notmatch '^sdk/[^/]+/[^/]+$') { + throw "Expected a repository-relative SDK package path for $($info.Name): $path" + } + if (-not $PSCmdlet.ShouldProcess($file.FullName, 'Save API hash from the staged sources JAR')) { + continue + } + $temporary = Join-Path ([IO.Path]::GetTempPath()) "java-release-api-$([guid]::NewGuid().ToString('N'))" + try { + & java -jar $tool --sources-jar $sources[0].File.FullName --package-relative-path $path ` + --output-directory $temporary + if ($LASTEXITCODE -ne 0) { + throw "API hash generation failed for $($package.FullyQualifiedName) with exit code $LASTEXITCODE." + } + $markdown = Join-Path $temporary 'api.md' + $metadata = @(Get-Content -LiteralPath (Join-Path $temporary 'api.metadata.yml')) + $hash = (Get-FileHash -LiteralPath $markdown -Algorithm SHA256).Hash.ToLowerInvariant() + $expectedVersion = "packageVersion: '$($version.Replace("'", "''"))'" + $expectedPath = "packageRelativePath: '$($path.Replace("'", "''"))'" + if ("apiHash: $hash" -notin $metadata -or $expectedVersion -notin $metadata -or + $expectedPath -notin $metadata -or (Get-Item -LiteralPath $markdown).Length -eq 0) { + throw "API review metadata does not match the staged package $($package.FullyQualifiedName)." + } + $info | Add-Member -MemberType NoteProperty -Name ApiHash -Value $hash -Force + $updates[$file.FullName] = $info | ConvertTo-Json -Depth 100 + } + finally { + if (Test-Path -LiteralPath $temporary) { + Remove-Item -LiteralPath $temporary -Recurse -Force + } + } +} + +foreach ($path in $updates.Keys) { + Set-Content -LiteralPath $path -Value $updates[$path] -Encoding utf8 +} diff --git a/eng/scripts/Stage-ApiReviewTools.ps1 b/eng/scripts/Stage-ApiReviewTools.ps1 index c9520627d0e5f..1ce8ac90d0c60 100644 --- a/eng/scripts/Stage-ApiReviewTools.ps1 +++ b/eng/scripts/Stage-ApiReviewTools.ps1 @@ -8,13 +8,26 @@ Builds and stages the Java API review JAR and generator script into an ARH tools #> [CmdletBinding(SupportsShouldProcess)] param( - [Parameter(Mandatory)][string]$BinOutputDirectory + [Parameter(Mandatory)][string]$BinOutputDirectory, + [switch]$ExpandSparseCheckout ) Set-StrictMode -Version 4 $ErrorActionPreference = 'Stop' $PSNativeCommandUseErrorActionPreference = $false $root = (Resolve-Path -LiteralPath (Join-Path $PSScriptRoot '..' '..')).Path +if ($ExpandSparseCheckout -and -not $WhatIfPreference) { + $sparseCheckout = & git -C $root config --bool core.sparseCheckout + if ($LASTEXITCODE -gt 1) { + throw 'Unable to determine whether the tooling checkout is sparse.' + } + if ($sparseCheckout -eq 'true') { + & git -C $root sparse-checkout add sdk/tools/api-review-maven-plugin sdk/tools/apiview-java-processor .vscode + if ($LASTEXITCODE -ne 0) { + throw 'Unable to include Java API review tooling in the sparse checkout.' + } + } +} $plugin = Join-Path $root 'sdk' 'tools' 'api-review-maven-plugin' $pomPath = Join-Path $plugin 'pom.xml' $generator = Join-Path $root 'eng' 'scripts' 'Generate-ApiReviewArtifact.ps1' diff --git a/eng/scripts/tests/ApiReviewHub.tests.ps1 b/eng/scripts/tests/ApiReviewHub.tests.ps1 index 6fac859e39d6f..6a71145012e92 100644 --- a/eng/scripts/tests/ApiReviewHub.tests.ps1 +++ b/eng/scripts/tests/ApiReviewHub.tests.ps1 @@ -66,6 +66,23 @@ Describe 'Java API review hub tooling' { Should -Invoke mvn -Times 0 } + It 'includes tooling sources when staging from a sparse build checkout' { + Mock git { $global:LASTEXITCODE = 0; if ($args -contains 'config') { return 'true' } } + Mock mvn { $global:LASTEXITCODE = 0 } + & $stage -BinOutputDirectory (Join-Path $TestDrive 'sparse-bin') -ExpandSparseCheckout + Should -Invoke git -Times 1 -ParameterFilter { + $args -contains 'sparse-checkout' -and $args -contains 'sdk/tools/api-review-maven-plugin' -and + $args -contains 'sdk/tools/apiview-java-processor' -and $args -contains '.vscode' + } + } + + It 'does not expand sparse checkout with WhatIf' { + Mock git { throw 'Unexpected sparse-checkout mutation.' } + & $stage -BinOutputDirectory (Join-Path $TestDrive 'sparse-whatif') -ExpandSparseCheckout -WhatIf + Should -Invoke git -Times 0 + Should -Invoke mvn -Times 0 + } + It 'runs the staged generator against a source checkout without engineering files' { Mock mvn { $global:LASTEXITCODE = 0 } $bin = Join-Path $TestDrive 'staged' diff --git a/eng/scripts/tests/Save-Package-ApiHash-Property.tests.ps1 b/eng/scripts/tests/Save-Package-ApiHash-Property.tests.ps1 new file mode 100644 index 0000000000000..cf3488e64ba10 --- /dev/null +++ b/eng/scripts/tests/Save-Package-ApiHash-Property.tests.ps1 @@ -0,0 +1,136 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +BeforeAll { + $script:HashScript = Join-Path $PSScriptRoot '..' 'Save-Package-ApiHash-Property.ps1' + $script:AutoReleaseScript = Join-Path $PSScriptRoot '..' '..' 'pipelines' 'scripts' 'Stage-AutoReleaseSignedArtifacts.ps1' + + function New-PackageFixture([string]$Name, [string]$Version = '1.0.0', [switch]$PomOnly) { + $directory = Join-Path $script:Staging 'com.azure' $Name + New-Item -ItemType Directory -Path $directory -Force | Out-Null + Set-Content -LiteralPath (Join-Path $directory "$Name-$Version.pom") -Value ( + "com.azure$Name$Version" + ) + if (-not $PomOnly) { + Set-Content -LiteralPath (Join-Path $directory "$Name-$Version.jar") -Value 'binary' + Set-Content -LiteralPath (Join-Path $directory "$Name-$Version-sources.jar") -Value 'sources' + } + $info = [ordered]@{ + Name = $Name + ArtifactName = $Name + Group = 'com.azure' + Version = $Version + DirectoryPath = "sdk/test/$Name" + ArtifactDetails = @{ releaseInBatch = $true } + Namespaces = @('com.azure.test') + } + $path = Join-Path $script:Staging 'PackageInfo' "$Name.json" + $info | ConvertTo-Json -Depth 10 | Set-Content -LiteralPath $path + return $path + } +} + +Describe 'Release artifact API hashes' { + BeforeEach { + $script:Staging = Join-Path $TestDrive ([guid]::NewGuid().ToString('N')) + New-Item -ItemType Directory -Path (Join-Path $script:Staging 'PackageInfo') -Force | Out-Null + $tool = Join-Path $TestDrive 'tool.jar' + Set-Content -LiteralPath $tool -Value 'tool' + $script:Version = '1.0.0' + Mock mvn { throw 'Hash generation must not rebuild the SDK package.' } + Mock java { + $destination = $args[-1] + $packagePath = $args[-3] + New-Item -ItemType Directory -Path $destination -Force | Out-Null + $markdown = Join-Path $destination 'api.md' + [IO.File]::WriteAllText($markdown, "API fixture`n") + $hash = (Get-FileHash -LiteralPath $markdown -Algorithm SHA256).Hash.ToLowerInvariant() + [IO.File]::WriteAllText((Join-Path $destination 'api.metadata.yml'), + "apiHash: $hash`npackageVersion: '$script:Version'`npackageRelativePath: '$packagePath'`n") + $global:LASTEXITCODE = 0 + } + } + + It 'hashes the staged sources without rebuilding and preserves other PackageInfo fields' { + $file = New-PackageFixture 'azure-example' + & $script:HashScript -ArtifactStagingDirectory $script:Staging -ToolJar $tool + $info = Get-Content -LiteralPath $file -Raw | ConvertFrom-Json + $info.ApiHash | Should -Match '^[0-9a-f]{64}$' + $info.Namespaces | Should -Contain 'com.azure.test' + $info.ArtifactDetails.releaseInBatch | Should -BeTrue + Should -Invoke java -Times 1 -ParameterFilter { + $args -contains (Join-Path $script:Staging 'com.azure' 'azure-example' 'azure-example-1.0.0-sources.jar') + } + Should -Invoke mvn -Times 0 + } + + It 'uses DevVersion for the actual built package without changing the release version' { + $file = New-PackageFixture 'azure-example' '1.1.0-beta.1' + $info = Get-Content -LiteralPath $file -Raw | ConvertFrom-Json + $info | Add-Member -MemberType NoteProperty -Name DevVersion -Value '1.1.0-beta.1' + $info.Version = '1.0.0' + $info | ConvertTo-Json -Depth 10 | Set-Content -LiteralPath $file + $script:Version = '1.1.0-beta.1' + & $script:HashScript -ArtifactStagingDirectory $script:Staging -ToolJar $tool + $saved = Get-Content -LiteralPath $file -Raw | ConvertFrom-Json + $saved.Version | Should -Be '1.0.0' + $saved.DevVersion | Should -Be '1.1.0-beta.1' + $saved.ApiHash | Should -Match '^[0-9a-f]{64}$' + } + + It 'skips POM-only packages and metadata for packages not built' { + $file = New-PackageFixture 'azure-parent' -PomOnly + Copy-Item -LiteralPath $file -Destination (Join-Path $script:Staging 'PackageInfo' 'unused.json') + & $script:HashScript -ArtifactStagingDirectory $script:Staging -ToolJar $tool + Should -Invoke java -Times 0 + (Get-Content -LiteralPath $file -Raw | ConvertFrom-Json).PSObject.Properties.Name | + Should -Not -Contain 'ApiHash' + } + + It 'rejects a stale staged package version rather than omitting its hash' { + $file = New-PackageFixture 'azure-example' + $info = Get-Content -LiteralPath $file -Raw | ConvertFrom-Json + $info.Version = '2.0.0' + $info | ConvertTo-Json -Depth 10 | Set-Content -LiteralPath $file + { & $script:HashScript -ArtifactStagingDirectory $script:Staging -ToolJar $tool } | + Should -Throw '*version does not match*' + Should -Invoke java -Times 0 + } + + It 'rejects missing release sources and leaves PackageInfo unchanged' { + $file = New-PackageFixture 'azure-example' + $before = Get-Content -LiteralPath $file -Raw + Remove-Item -LiteralPath (Join-Path $script:Staging 'com.azure' 'azure-example' 'azure-example-1.0.0-sources.jar') + { & $script:HashScript -ArtifactStagingDirectory $script:Staging -ToolJar $tool } | + Should -Throw '*one staged sources JAR*' + Get-Content -LiteralPath $file -Raw | Should -Be $before + } + + It 'does not publish a hash when generated metadata describes a different version' { + $file = New-PackageFixture 'azure-example' + $before = Get-Content -LiteralPath $file -Raw + Mock java { + $destination = $args[-1] + New-Item -ItemType Directory -Path $destination -Force | Out-Null + $markdown = Join-Path $destination 'api.md' + [IO.File]::WriteAllText($markdown, "API fixture`n") + $hash = (Get-FileHash -LiteralPath $markdown -Algorithm SHA256).Hash.ToLowerInvariant() + [IO.File]::WriteAllText((Join-Path $destination 'api.metadata.yml'), + "apiHash: $hash`npackageVersion: '2.0.0'`npackageRelativePath: 'sdk/test/azure-example'`n") + $global:LASTEXITCODE = 0 + } + { & $script:HashScript -ArtifactStagingDirectory $script:Staging -ToolJar $tool } | + Should -Throw '*metadata does not match*' + Get-Content -LiteralPath $file -Raw | Should -Be $before + } + + It 'preserves the hash when signed artifacts are selected for auto-release' { + $file = New-PackageFixture 'azure-example' + & $script:HashScript -ArtifactStagingDirectory $script:Staging -ToolJar $tool + $destination = Join-Path $TestDrive 'signed-auto-release' + & $script:AutoReleaseScript -SourceDirectory $script:Staging -TargetDirectory $destination ` + -ArtifactsJson '[{"name":"azure-example","groupId":"com.azure"}]' + Get-Content -LiteralPath (Join-Path $destination 'PackageInfo' 'azure-example.json') -Raw | + Should -Be (Get-Content -LiteralPath $file -Raw) + } +} diff --git a/sdk/tools/api-review-maven-plugin/README.md b/sdk/tools/api-review-maven-plugin/README.md index f8e4d66c6eb6a..b4d21850ad731 100644 --- a/sdk/tools/api-review-maven-plugin/README.md +++ b/sdk/tools/api-review-maven-plugin/README.md @@ -69,3 +69,5 @@ The plugin includes the shared processor sources and resources from `sdk/tools/a Verification also runs SDK Checkstyle, Spotless formatting, and Codesnippet checks. Checkstyle covers plugin sources and tests; the processor module checks its own sources. Narrow exceptions allow Maven-injected fields and tool-specific exception handling. Both modules are validated by `sdk/tools/ci.yml`, with release disabled. This repository's ARH integration builds the tool from source and does not publish it. + +SDK build pipelines also use this executable on staged sources JARs to save `ApiHash` in PackageInfo JSON. The hash uses the same Markdown bytes as ARH generation; package-info files travel with the signed artifacts. POM-only packages have no API hash. From 52bfb6af371a5438f1c01c1ff9dff6b3826cc4b3 Mon Sep 17 00:00:00 2001 From: Weidong Xu Date: Tue, 24 Nov 2026 11:49:51 +0800 Subject: [PATCH 2/4] Scope JDK 17 to Java API hashing tasks Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d47c40e0-dce3-4856-b258-f62b42922e1b --- .../templates/steps/save-package-api-hash.yml | 12 ++++----- eng/scripts/Save-Package-ApiHash-Property.ps1 | 5 ++-- .../Save-Package-ApiHash-Property.tests.ps1 | 26 +++++++++++++++++++ 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/eng/pipelines/templates/steps/save-package-api-hash.yml b/eng/pipelines/templates/steps/save-package-api-hash.yml index 8b94ac512954e..5c0af32713276 100644 --- a/eng/pipelines/templates/steps/save-package-api-hash.yml +++ b/eng/pipelines/templates/steps/save-package-api-hash.yml @@ -4,15 +4,10 @@ parameters: default: $(Build.ArtifactStagingDirectory) steps: - - task: JavaToolInstaller@0 - displayName: Use Java 17 for API hashing - inputs: - versionSpec: '17' - jdkArchitectureOption: x64 - jdkSourceOption: PreInstalled - - task: PowerShell@2 displayName: Build Java API hash tooling + env: + JAVA_HOME: $(JAVA_HOME_17_X64) inputs: pwsh: true targetType: filePath @@ -23,6 +18,8 @@ steps: - task: PowerShell@2 displayName: Save API hashes from staged release sources + env: + JAVA_HOME: $(JAVA_HOME_17_X64) inputs: pwsh: true targetType: filePath @@ -30,3 +27,4 @@ steps: arguments: >- -ArtifactStagingDirectory "${{ parameters.ArtifactStagingDirectory }}" -ToolJar "$(Build.BinariesDirectory)/api-review-tools/api-review-maven-plugin.jar" + -JavaExecutable "$(JAVA_HOME_17_X64)/bin/java" diff --git a/eng/scripts/Save-Package-ApiHash-Property.ps1 b/eng/scripts/Save-Package-ApiHash-Property.ps1 index 67b89182b6809..5d0686900b0c4 100644 --- a/eng/scripts/Save-Package-ApiHash-Property.ps1 +++ b/eng/scripts/Save-Package-ApiHash-Property.ps1 @@ -5,7 +5,8 @@ [CmdletBinding(SupportsShouldProcess)] param( [Parameter(Mandatory)][string]$ArtifactStagingDirectory, - [Parameter(Mandatory)][string]$ToolJar + [Parameter(Mandatory)][string]$ToolJar, + [string]$JavaExecutable = 'java' ) Set-StrictMode -Version 4 @@ -59,7 +60,7 @@ foreach ($file in Get-ChildItem -LiteralPath (Join-Path $root 'PackageInfo') -Fi } $temporary = Join-Path ([IO.Path]::GetTempPath()) "java-release-api-$([guid]::NewGuid().ToString('N'))" try { - & java -jar $tool --sources-jar $sources[0].File.FullName --package-relative-path $path ` + & $JavaExecutable -jar $tool --sources-jar $sources[0].File.FullName --package-relative-path $path ` --output-directory $temporary if ($LASTEXITCODE -ne 0) { throw "API hash generation failed for $($package.FullyQualifiedName) with exit code $LASTEXITCODE." diff --git a/eng/scripts/tests/Save-Package-ApiHash-Property.tests.ps1 b/eng/scripts/tests/Save-Package-ApiHash-Property.tests.ps1 index cf3488e64ba10..1aafc38b8dd6a 100644 --- a/eng/scripts/tests/Save-Package-ApiHash-Property.tests.ps1 +++ b/eng/scripts/tests/Save-Package-ApiHash-Property.tests.ps1 @@ -78,6 +78,32 @@ Describe 'Release artifact API hashes' { $saved.ApiHash | Should -Match '^[0-9a-f]{64}$' } + It 'uses the selected Java executable without changing the calling environment' { + $file = New-PackageFixture 'azure-example' + $selectedJava = Join-Path $TestDrive 'selected java.ps1' + Set-Content -LiteralPath $selectedJava -Value @' +$destination = $args[-1] +$packagePath = $args[-3] +New-Item -ItemType Directory -Path $destination -Force | Out-Null +$markdown = Join-Path $destination 'api.md' +[IO.File]::WriteAllText($markdown, "Selected Java fixture`n") +$hash = (Get-FileHash -LiteralPath $markdown -Algorithm SHA256).Hash.ToLowerInvariant() +[IO.File]::WriteAllText((Join-Path $destination 'api.metadata.yml'), + "apiHash: $hash`npackageVersion: '1.0.0'`npackageRelativePath: '$packagePath'`n") +$global:LASTEXITCODE = 0 +'@ + $javaHome = $env:JAVA_HOME + $path = $env:PATH + + & $script:HashScript -ArtifactStagingDirectory $script:Staging -ToolJar $tool ` + -JavaExecutable $selectedJava + + (Get-Content -LiteralPath $file -Raw | ConvertFrom-Json).ApiHash | Should -Match '^[0-9a-f]{64}$' + Should -Invoke java -Times 0 + $env:JAVA_HOME | Should -Be $javaHome + $env:PATH | Should -Be $path + } + It 'skips POM-only packages and metadata for packages not built' { $file = New-PackageFixture 'azure-parent' -PomOnly Copy-Item -LiteralPath $file -Destination (Join-Path $script:Staging 'PackageInfo' 'unused.json') From 85f2105493c7efcc5422a3c28dfc55fb928d4292 Mon Sep 17 00:00:00 2001 From: Weidong Xu Date: Tue, 24 Nov 2026 12:48:56 +0800 Subject: [PATCH 3/4] Honor confirmation before expanding API review sparse checkout Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d47c40e0-dce3-4856-b258-f62b42922e1b --- eng/scripts/Stage-ApiReviewTools.ps1 | 3 ++- eng/scripts/tests/ApiReviewHub.tests.ps1 | 25 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/eng/scripts/Stage-ApiReviewTools.ps1 b/eng/scripts/Stage-ApiReviewTools.ps1 index 1ce8ac90d0c60..a959ec5e4e9d4 100644 --- a/eng/scripts/Stage-ApiReviewTools.ps1 +++ b/eng/scripts/Stage-ApiReviewTools.ps1 @@ -21,7 +21,8 @@ if ($ExpandSparseCheckout -and -not $WhatIfPreference) { if ($LASTEXITCODE -gt 1) { throw 'Unable to determine whether the tooling checkout is sparse.' } - if ($sparseCheckout -eq 'true') { + if ($sparseCheckout -eq 'true' -and + $PSCmdlet.ShouldProcess($root, 'Include Java API review tools in sparse checkout')) { & git -C $root sparse-checkout add sdk/tools/api-review-maven-plugin sdk/tools/apiview-java-processor .vscode if ($LASTEXITCODE -ne 0) { throw 'Unable to include Java API review tooling in the sparse checkout.' diff --git a/eng/scripts/tests/ApiReviewHub.tests.ps1 b/eng/scripts/tests/ApiReviewHub.tests.ps1 index 6a71145012e92..d0ac2a003c8fc 100644 --- a/eng/scripts/tests/ApiReviewHub.tests.ps1 +++ b/eng/scripts/tests/ApiReviewHub.tests.ps1 @@ -83,6 +83,31 @@ Describe 'Java API review hub tooling' { Should -Invoke mvn -Times 0 } + It 'requires confirmation before changing sparse checkout' { + $marker = Join-Path $TestDrive 'sparse-checkout-mutated' + $command = @" +function git { + `$global:LASTEXITCODE = 0 + if (`$args -contains 'config') { return 'true' } + Set-Content -LiteralPath '$($marker.Replace("'", "''"))' -Value 'expanded' +} +function mvn { throw 'Unexpected Maven invocation.' } +try { + & '$($stage.Replace("'", "''"))' -BinOutputDirectory '$($script:Output.Replace("'", "''"))' -ExpandSparseCheckout -Confirm +} +catch { + `$_.Exception.Message + exit 0 +} +exit 1 +"@ + $output = & (Get-Process -Id $PID).Path -NoLogo -NoProfile -NonInteractive -Command $command + + $LASTEXITCODE | Should -Be 0 + $output | Should -Match 'NonInteractive' + Test-Path -LiteralPath $marker | Should -BeFalse + } + It 'runs the staged generator against a source checkout without engineering files' { Mock mvn { $global:LASTEXITCODE = 0 } $bin = Join-Path $TestDrive 'staged' From e0c4038d38adb564fc0aeddc5fc16131a39fd906 Mon Sep 17 00:00:00 2001 From: Weidong Xu Date: Tue, 24 Nov 2026 15:38:46 +0800 Subject: [PATCH 4/4] Warn and skip unavailable Java API hashes Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d47c40e0-dce3-4856-b258-f62b42922e1b --- eng/scripts/Save-Package-ApiHash-Property.ps1 | 9 ++++- .../Save-Package-ApiHash-Property.tests.ps1 | 39 +++++++++++++++++-- sdk/tools/api-review-maven-plugin/README.md | 2 +- 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/eng/scripts/Save-Package-ApiHash-Property.ps1 b/eng/scripts/Save-Package-ApiHash-Property.ps1 index 5d0686900b0c4..64e5dd164c242 100644 --- a/eng/scripts/Save-Package-ApiHash-Property.ps1 +++ b/eng/scripts/Save-Package-ApiHash-Property.ps1 @@ -48,6 +48,10 @@ foreach ($file in Get-ChildItem -LiteralPath (Join-Path $root 'PackageInfo') -Fi $sources = @($package.AssociatedArtifacts | Where-Object { $_.Classifier -eq 'sources' -and $_.Type -eq 'jar' }) + if ($sources.Count -eq 0) { + Write-Warning "No staged sources JAR for $($package.FullyQualifiedName); skipping API hash." + continue + } if ($sources.Count -ne 1) { throw "Expected one staged sources JAR for $($package.FullyQualifiedName)." } @@ -63,7 +67,10 @@ foreach ($file in Get-ChildItem -LiteralPath (Join-Path $root 'PackageInfo') -Fi & $JavaExecutable -jar $tool --sources-jar $sources[0].File.FullName --package-relative-path $path ` --output-directory $temporary if ($LASTEXITCODE -ne 0) { - throw "API hash generation failed for $($package.FullyQualifiedName) with exit code $LASTEXITCODE." + Write-Warning "API hash generation failed for $($package.FullyQualifiedName) with exit code $LASTEXITCODE; skipping API hash." + # PowerShell@2 also checks the last native exit code. + $global:LASTEXITCODE = 0 + continue } $markdown = Join-Path $temporary 'api.md' $metadata = @(Get-Content -LiteralPath (Join-Path $temporary 'api.metadata.yml')) diff --git a/eng/scripts/tests/Save-Package-ApiHash-Property.tests.ps1 b/eng/scripts/tests/Save-Package-ApiHash-Property.tests.ps1 index 1aafc38b8dd6a..b49e918d1ddd4 100644 --- a/eng/scripts/tests/Save-Package-ApiHash-Property.tests.ps1 +++ b/eng/scripts/tests/Save-Package-ApiHash-Property.tests.ps1 @@ -30,7 +30,7 @@ BeforeAll { } } -Describe 'Release artifact API hashes' { +Describe 'Release artifact API hashes' -Tag 'UnitTest' { BeforeEach { $script:Staging = Join-Path $TestDrive ([guid]::NewGuid().ToString('N')) New-Item -ItemType Directory -Path (Join-Path $script:Staging 'PackageInfo') -Force | Out-Null @@ -123,12 +123,43 @@ $global:LASTEXITCODE = 0 Should -Invoke java -Times 0 } - It 'rejects missing release sources and leaves PackageInfo unchanged' { + It 'warns and skips missing release sources without changing PackageInfo' { $file = New-PackageFixture 'azure-example' $before = Get-Content -LiteralPath $file -Raw Remove-Item -LiteralPath (Join-Path $script:Staging 'com.azure' 'azure-example' 'azure-example-1.0.0-sources.jar') - { & $script:HashScript -ArtifactStagingDirectory $script:Staging -ToolJar $tool } | - Should -Throw '*one staged sources JAR*' + & $script:HashScript -ArtifactStagingDirectory $script:Staging -ToolJar $tool ` + -WarningVariable warnings -WarningAction SilentlyContinue + $warnings | Should -HaveCount 1 + $warnings[0].Message | Should -Match 'No staged sources JAR.*skipping API hash' + Should -Invoke java -Times 0 + Get-Content -LiteralPath $file -Raw | Should -Be $before + } + + It 'continues hashing other packages when a sources JAR is missing' { + $missing = New-PackageFixture 'azure-missing' + $before = Get-Content -LiteralPath $missing -Raw + Remove-Item -LiteralPath (Join-Path $script:Staging 'com.azure' 'azure-missing' 'azure-missing-1.0.0-sources.jar') + $valid = New-PackageFixture 'azure-valid' + + & $script:HashScript -ArtifactStagingDirectory $script:Staging -ToolJar $tool ` + -WarningAction SilentlyContinue + + Get-Content -LiteralPath $missing -Raw | Should -Be $before + (Get-Content -LiteralPath $valid -Raw | ConvertFrom-Json).ApiHash | Should -Match '^[0-9a-f]{64}$' + Should -Invoke java -Times 1 + } + + It 'warns and skips failed API generation without failing the calling task' { + $file = New-PackageFixture 'azure-example' + $before = Get-Content -LiteralPath $file -Raw + Mock java { $global:LASTEXITCODE = 1 } + + & $script:HashScript -ArtifactStagingDirectory $script:Staging -ToolJar $tool ` + -WarningVariable warnings -WarningAction SilentlyContinue + + $warnings | Should -HaveCount 1 + $warnings[0].Message | Should -Match 'generation failed.*exit code 1.*skipping API hash' + $global:LASTEXITCODE | Should -Be 0 Get-Content -LiteralPath $file -Raw | Should -Be $before } diff --git a/sdk/tools/api-review-maven-plugin/README.md b/sdk/tools/api-review-maven-plugin/README.md index b4d21850ad731..5be756389d2b6 100644 --- a/sdk/tools/api-review-maven-plugin/README.md +++ b/sdk/tools/api-review-maven-plugin/README.md @@ -70,4 +70,4 @@ Verification also runs SDK Checkstyle, Spotless formatting, and Codesnippet chec Both modules are validated by `sdk/tools/ci.yml`, with release disabled. This repository's ARH integration builds the tool from source and does not publish it. -SDK build pipelines also use this executable on staged sources JARs to save `ApiHash` in PackageInfo JSON. The hash uses the same Markdown bytes as ARH generation; package-info files travel with the signed artifacts. POM-only packages have no API hash. +SDK build pipelines also use this executable on staged sources JARs to save `ApiHash` in PackageInfo JSON. The hash uses the same Markdown bytes as ARH generation; package-info files travel with the signed artifacts. POM-only packages have no API hash. Missing sources JARs or failed API generation produce a warning and skip hashing; existing Maven artifact validation remains unchanged.