Skip to content
Closed
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
38 changes: 25 additions & 13 deletions eng/pipelines/templates/jobs/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -344,22 +344,34 @@ jobs:
CheckLinkGuidance: $true

- ${{ if and(eq(variables['Build.Reason'], 'PullRequest'), eq(parameters.ServiceDirectory, 'auto')) }}:
# NOTE: The PRServiceDirectories string is used in the two Code Generation steps below and
# the verify samples
# For Pull Request pipelines scan all /sdk/<service>/<library> directories that were modified.
# This will scope code generation validation to the libraries that were modified. For example, if
# /sdk/storage/azure-storage-blob was modified but none of the other Storage libraries were only validating
# azure-storage-blob's code generation will properly validate the PR.
- pwsh: |
$packageInfoFiles = Get-ChildItem -Path '$(Build.ArtifactStagingDirectory)/PackageInfo' -Filter "*.json" -File
$serviceDirectories = @()
foreach ($packageInfoFile in $packageInfoFiles) {
$packageInfo = Get-Content -Path $packageInfoFile | ConvertFrom-Json
$serviceDirectories += $packageInfo.ServiceDirectory
$diffJson = Get-Content '$(Build.ArtifactStagingDirectory)/diff/diff.json' -Raw
$diff = ConvertFrom-Json $diffJson

$changedServicesString = ''
if ($diff.ChangedFiles) {
$servicesToScan = @()
foreach ($changedFile in $diff.ChangedFiles) {
if ($changedFile -match 'sdk/(.*?)/(.*?)/.*?') {
$servicesToScan += $Matches[1] + '/' + $Matches[2]
}
}

$servicesToScan = $servicesToScan | Sort-Object -Unique
$changedServicesString = $servicesToScan -join ","
}
# dedupe the list
$serviceDirectories = $serviceDirectories | Select-Object -Unique
$changedServicesString = $serviceDirectories -join ","

Write-Host "changedServicesString='$changedServicesString'"
Write-Host "##vso[task.setvariable variable=PRServiceDirectories;]$changedServicesString"
displayName: Set PRServiceDirectories from PackageInfo files
- ${{ else }}:
# For non-Pull Request runs always use the service directory indicated in the ci*.yml that triggered the build.
# This will result in all libraries managed by the YAML file to be validated. For example, if
# /sdk/storage/ci.yml was ran all Storage libraries will be validated, which is the proper validation.
- pwsh: |
$changedServicesString='${{ parameters.ServiceDirectory }}'
Write-Host "changedServicesString='$changedServicesString'"
Expand Down Expand Up @@ -402,20 +414,20 @@ jobs:
displayName: Use Node.js 22.x

- task: PowerShell@2
displayName: Verify Code Generation for Pull Request
displayName: Verify Swagger Code Generation
inputs:
pwsh: true
filePath: $(Build.SourcesDirectory)/eng/scripts/Compare-CurrentToCodegeneration.ps1
arguments: >
-ServiceDirectories '$(PRServiceDirectories)'
-ScanDirectories '$(PRServiceDirectories)'

- task: PowerShell@2
displayName: Verify TypeSpec Code Generation
inputs:
pwsh: true
filePath: $(Build.SourcesDirectory)/eng/scripts/TypeSpec-Compare-CurrentToCodegeneration.ps1
arguments: >
-ServiceDirectories '$(PRServiceDirectories)'
-ScanDirectories '$(PRServiceDirectories)'

- template: /eng/pipelines/templates/steps/run-and-validate-linting.yml
parameters:
Expand Down
32 changes: 17 additions & 15 deletions eng/scripts/Compare-CurrentToCodegeneration.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ code.
If the regenerated code is different than the current code this will tell the differences, the files the differences
are in, and exit with a failure status.

.PARAMETER Directory
The directory that will be searched for 'Update-Codegeneration.ps1' scripts. The default is the root directory of the
Azure SDK for Java repository. CI jobs should use the 'ServiceDirectory', such as /sdk/storage.
.PARAMETER ScanDirectories
A comma-separated list of directories that will be scanned for'Update-Codegeneration.ps1' scripts. If nothing is passed
the default behavior is a no-op. CI jobs should determine which directories to scan based either on the 'ServiceDirectory',
such as /sdk/storage, of the ci.yml being used or for Pull Request pipelines determine all directories based on what files
were changed.
#>

param(
[Parameter(Mandatory = $false)]
[string]$ServiceDirectories
[string]$ScanDirectories
)

$SeparatorBars = "==========================================================================="
Expand All @@ -26,20 +28,20 @@ $SeparatorBars = "==============================================================
function Compare-CurrentToCodegeneration {
param(
[Parameter(Mandatory=$true)]
$ServiceDirectory
$ScanDirectory
)

$swaggers = Get-ChildItem -Path $ServiceDirectory -Filter "Update-Codegeneration.ps1" -Recurse
$swaggers = Get-ChildItem -Path $ScanDirectory -Filter "Update-Codegeneration.ps1" -Recurse
if ($swaggers.Count -eq 0) {
Write-Host "$SeparatorBars"
Write-Host "No Swagger files to regenerate for $ServiceDirectory"
Write-Host "No Swagger files to regenerate for $ScanDirectory"
Write-Host "$SeparatorBars"
return $false
}


Write-Host "$SeparatorBars"
Write-Host "Invoking Autorest code regeneration for $ServiceDirectory"
Write-Host "Invoking Autorest code regeneration for $ScanDirectory"
Write-Host "$SeparatorBars"

foreach ($script in $swaggers) {
Expand All @@ -48,15 +50,15 @@ function Compare-CurrentToCodegeneration {
}

Write-Host "$SeparatorBars"
Write-Host "Verify no diff for $ServiceDirectory"
Write-Host "Verify no diff for $ScanDirectory"
Write-Host "$SeparatorBars"

# prevent warning related to EOL differences which triggers an exception for some reason
& git -c core.safecrlf=false diff --ignore-space-at-eol --exit-code -- "*.java"

if ($LastExitCode -ne 0) {
$status = git status -s | Out-String
Write-Host "The following files in $ServiceDirectory are out of date:"
Write-Host "The following files in $ScanDirectory are out of date:"
Write-Host "$status"
return $true
}
Expand All @@ -65,11 +67,11 @@ function Compare-CurrentToCodegeneration {

$hasError = $false

# If a list of ServiceDirectories was passed in, process the entire list otherwise
# If a list of ScanDirectories was passed in, process the entire list otherwise
# pass in an empty string to verify everything
if ($ServiceDirectories) {
foreach ($ServiceDirectory in $ServiceDirectories.Split(',')) {
$path = "sdk/$ServiceDirectory"
if ($ScanDirectories) {
foreach ($ScanDirectory in $ScanDirectories.Split(',')) {
$path = "sdk/$ScanDirectory"
$result = Compare-CurrentToCodegeneration $path
if ($result) {
$hasError = $true
Expand All @@ -82,4 +84,4 @@ if ($ServiceDirectories) {
if ($hasError) {
exit 1
}
exit 0
exit 0
28 changes: 15 additions & 13 deletions eng/scripts/TypeSpec-Compare-CurrentToCodegeneration.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ Sync TypeSpec defintion and generate SDK for RPs under the input directory and h
If the regenerated code is different from current code, this will tell the files the differences
are in, and exit with a failure status.

.PARAMETER Directory
The directory that will be used to get 'tsp-location.yaml' and generate SDK. The default is the root directory of the
Azure SDK for Java repository. One can also input service directory like: /sdk/storage, sdk/anomalydetector/azure-ai-anomalydetector.
.PARAMETER ScanDirectories
A comma-separated list of directories that will be scanned for 'tsp-location.yaml'. If nothing is passed the default
behavior is a no-op. CI jobs should determine which directories to scan based either on the 'ServiceDirectory',
such as /sdk/storage, of the ci.yml being used or for Pull Request pipelines determine all directories based on what files
were changed.
#>

param(
[Parameter(Mandatory = $false)]
[string]$ServiceDirectories
[string]$ScanDirectories
)

$SeparatorBars = "==========================================================================="
Expand Down Expand Up @@ -44,19 +46,19 @@ function Install-typespec-client-generator-cli {
function TypeSpec-Compare-CurrentToCodegeneration {
param(
[Parameter(Mandatory=$true)]
$ServiceDirectory
$ScanDirectory
)

$tspYamls = Get-ChildItem -Path $ServiceDirectory -Filter "tsp-location.yaml" -Recurse
$tspYamls = Get-ChildItem -Path $ScanDirectory -Filter "tsp-location.yaml" -Recurse
if ($tspYamls.Count -eq 0) {
Write-Host "$SeparatorBars"
Write-Host "No TypeSpec files to regenerate for $ServiceDirectory"
Write-Host "No TypeSpec files to regenerate for $ScanDirectory"
Write-Host "$SeparatorBars"
return $false
}

Write-Host "$SeparatorBars"
Write-Host "Invoking tsp-client update for tsp-location.yaml files in $ServiceDirectory"
Write-Host "Invoking tsp-client update for tsp-location.yaml files in $ScanDirectory"
Write-Host "$SeparatorBars"

$failedSdk = $null
Expand All @@ -80,7 +82,7 @@ function TypeSpec-Compare-CurrentToCodegeneration {
}

Write-Host "$SeparatorBars"
Write-Host "Verify no diff for TypeSpec generated files in $ServiceDirectory"
Write-Host "Verify no diff for TypeSpec generated files in $ScanDirectory"
Write-Host "$SeparatorBars"

# prevent warning related to EOL differences which triggers an exception for some reason
Expand All @@ -95,17 +97,17 @@ function TypeSpec-Compare-CurrentToCodegeneration {
}

# Delete out TypeSpec temporary folders if they still exist.
Get-ChildItem -Path $ServiceDirectory -Filter TempTypeSpecFiles -Recurse -Directory | ForEach-Object {
Get-ChildItem -Path $ScanDirectory -Filter TempTypeSpecFiles -Recurse -Directory | ForEach-Object {
Remove-Item -Path $_.FullName -Recurse -Force
}
return $false
}

$hasError = $false
if ($ServiceDirectories) {
if ($ScanDirectories) {
Install-typespec-client-generator-cli
foreach ($ServiceDirectory in $ServiceDirectories.Split(',')) {
$path = "sdk/$ServiceDirectory"
foreach ($ScanDirectory in $ScanDirectories.Split(',')) {
$path = "sdk/$ScanDirectory"
$result = TypeSpec-Compare-CurrentToCodegeneration $path
if ($result) {
$hasError = $true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## 1.5.0-beta.1 (Unreleased)

Test

### Features Added

### Breaking Changes
Expand Down
Loading