Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions eng/pipelines/templates/jobs/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This follows the existing APIView lifecycle: the patch pipeline calls APIView after publication to mark packages as shipped. We generate ApiHash here so the metadata is ready when that call is replaced by ARH's mark-released operation. This prepares the planned migration without adding an ARH call or a new release gate today.


- task: Powershell@2
inputs:
filePath: $(Build.SourcesDirectory)/eng/scripts/Save-Package-Namespaces-Property.ps1
Expand Down
30 changes: 30 additions & 0 deletions eng/pipelines/templates/steps/save-package-api-hash.yml
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"
96 changes: 96 additions & 0 deletions eng/scripts/Save-Package-ApiHash-Property.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# 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 -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)."
}
$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) {
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
}
Comment thread
Copilot marked this conversation as resolved.
Resolved
$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) {
Comment thread
Copilot marked this conversation as resolved.
Resolved
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
}
16 changes: 15 additions & 1 deletion eng/scripts/Stage-ApiReviewTools.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,27 @@ 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' -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.'
}
}
}
$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'
Expand Down
42 changes: 42 additions & 0 deletions eng/scripts/tests/ApiReviewHub.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,48 @@ 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 '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'
Expand Down
Loading
Loading