Skip to content
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

Publish api json file and enable api change detection #17837

Merged
Merged
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
17 changes: 12 additions & 5 deletions eng/pipelines/templates/steps/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,6 @@ steps:
node eng/tools/rush-runner.js unlink
displayName: "Unlink dependencies"

- template: /eng/common/pipelines/templates/steps/create-apireview.yml
parameters:
Artifacts: ${{ parameters.Artifacts }}
ArtifactPath: $(Build.SourcesDirectory)/sdk/${{ parameters.ServiceDirectory }}

- template: ../steps/generate-doc.yml
parameters:
ServiceDirectory: ${{parameters.ServiceDirectory}}
Expand All @@ -94,7 +89,19 @@ steps:
}
displayName: 'Copy Packages'

- pwsh: |
eng/scripts/stage-api-review-file.ps1 -PackageInfoPath $(Build.ArtifactStagingDirectory)/PackageInfo -StagingDirectory $(Build.ArtifactStagingDirectory)
displayName: 'Copy API extracted files'

- template: /eng/common/pipelines/templates/steps/publish-artifact.yml
parameters:
ArtifactPath: '$(Build.ArtifactStagingDirectory)'
ArtifactName: 'packages'

- template: /eng/common/pipelines/templates/steps/create-apireview.yml
parameters:
Artifacts: ${{ parameters.Artifacts }}

- template: /eng/common/pipelines/templates/steps/detect-api-changes.yml
parameters:
Artifacts: ${{ parameters.Artifacts }}
22 changes: 9 additions & 13 deletions eng/scripts/Language-Settings.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -404,35 +404,31 @@ Removed $($package.name) because of docs package validation failure on $(Get-Dat
}

# function is used to auto generate API View
function Find-javascript-Artifacts-For-Apireview($artifactDir, $packageName = "")
function Find-javascript-Artifacts-For-Apireview($artifactDir, $packageName)
{
# Find api.json file in service temp directory
[regex]$pattern = "azure-"
$pkgName = $pattern.replace($packageName, "", 1)
$packageDir = Join-Path $artifactDir $pkgName "temp"
if (Test-Path $packageDir)
$artifactPath = Join-Path $artifactDir $packageName
if (Test-Path $artifactPath)
{
Write-Host "Searching for *.api.json in path $($packageDir)"
$files = Get-ChildItem "${packageDir}" | Where-Object -FilterScript { $_.Name.EndsWith(".api.json") }
Write-Host "Searching for *.api.json in path $($artifactPath)"
$files = Get-ChildItem "${artifactPath}" | Where-Object -FilterScript { $_.Name.EndsWith(".api.json") }
if (!$files)
{
Write-Host "$($packageDir) does not have api review json for package"
Write-Host "$($packageName) does not have api review json"
Write-Host "API Extractor must be enabled for $($packageName). Please ensure api-extractor.json is present in package directory and api extract script included in build script"
return $null
}
elseif ($files.Count -ne 1)
{
Write-Host "$($packageDir) should contain only one api review for $($packageName)"
Write-Host "No of Packages $($files.Count)"
Write-Host "$($artifactPath) should contain only one api review for $($packageName)"
Write-Host "No of files $($files.Count)"
return $null
}
}
else
{
Write-Host "$($pkgName) does not have api review json"
return $null
}

}
$packages = @{
$files[0].Name = $files[0].FullName
}
Expand Down
50 changes: 50 additions & 0 deletions eng/scripts/stage-api-review-file.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
param (
[Parameter(mandatory = $true)]
$PackageInfoPath,
[Parameter(mandatory = $true)]
$StagingDirectory
)

if (!((Test-Path $PackageInfoPath) -and (Test-Path $StagingDirectory)))
{
Write-Error "Invalid parameter values. Pleaes verify values for these parameters."
exit 1
}

foreach($pkg in (Get-ChildItem -Path $PackageInfoPath "*.json"))
{
$info = Get-Content -Path $pkg.FullName | ConvertFrom-Json
$apiFilePath = Join-Path $info.DirectoryPath "temp"
if (Test-Path $apiFilePath)
{
$apiFile = Get-ChildItem -Path $apiFilePath "*.api.json"
if ($apiFile)
{
if ($apiFile.Count -ne 1)
Copy link
Member

Choose a reason for hiding this comment

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

Does this always give back a list? I know PS sometimes gives back a single object if there is only one in the list.

Copy link
Member Author

Choose a reason for hiding this comment

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

I believe so. It was working when referring it directly without index also. I thought of adding this check to avoid any issue if we get any such scenario.

{
# Unlikely, but handling to avoid any issue in the future if more than one api file is present here
Write-Error "Detected more than one api extracted file in $apiFilePath"
exit 1
}

$pkgStagingDir = Join-Path $StagingDirectory $info.ArtifactName
if (!(Test-Path $pkgStagingDir))
{
New-Item -Type Directory -Name $info.ArtifactName -Path $StagingDirectory > $null
}
$sourceFilePath = $apiFile[0].FullName
$targetFilePath = "$($pkgStagingDir)/$($info.ArtifactName)_$($info.Version).api.json"
Write-Host "Copying $($sourceFilePath) to $($targetFilePath)"
Copy-Item $sourceFilePath $targetFilePath
}
else
{
# Not an error is api-extractor is not cofigured/ required for a package
Write-Host "API extracted file is not present for package $($info.Name)"
}
}
else
{
Write-Host "Directory $($apiFilePath) is not present in package root to search for api-extracted file"
}
}