Repository navigation
Test Java API hashes across module types (do not merge) #51395
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Weidong Xu (weidongxu-microsoft)
wants to merge
4
commits into
main
from
weidongxu/java-api-hash-multi-origin-validation
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5618e36
Generate Java release API hashes from staged sources
weidongxu-microsoft 52bfb6a
Scope JDK 17 to Java API hashing tasks
weidongxu-microsoft 85f2105
Honor confirmation before expanding API review sparse checkout
weidongxu-microsoft 49fc4c1
Validate API hashes across Java module types
weidongxu-microsoft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| parameters: | ||
| - name: ArtifactStagingDirectory | ||
| type: string | ||
| default: $(Build.ArtifactStagingDirectory) | ||
|
|
||
| steps: | ||
| - task: PowerShell@2 | ||
| displayName: Build Java API hash tooling | ||
| env: | ||
| JAVA_HOME: $(JAVA_HOME_17_X64) | ||
| 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 | ||
| env: | ||
| JAVA_HOME: $(JAVA_HOME_17_X64) | ||
| 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" | ||
| -JavaExecutable "$(JAVA_HOME_17_X64)/bin/java" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| # 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, | ||
| [string]$JavaExecutable = 'java' | ||
| ) | ||
|
|
||
| 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 { | ||
| & $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." | ||
| } | ||
| $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 | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
eng/scripts/tests/Save-Package-ApiHash-Property.tests.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| # 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 ( | ||
| "<project><groupId>com.azure</groupId><artifactId>$Name</artifactId><version>$Version</version></project>" | ||
| ) | ||
| 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' { | ||
|
weidongxu-microsoft marked this conversation as resolved.
Resolved
|
||
| 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 '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') | ||
| & $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) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.