Skip to content

Commit

Permalink
Publish api json file and enable api change detection (Azure#17837)
Browse files Browse the repository at this point in the history
* Publish api file and enable api change detection
  • Loading branch information
praveenkuttappan authored Sep 28, 2021
1 parent 740a343 commit 8eba05d
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 18 deletions.
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)
{
# 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"
}
}

0 comments on commit 8eba05d

Please sign in to comment.