Skip to content

Commit

Permalink
CADL manual API review (#4832)
Browse files Browse the repository at this point in the history
* Add support for manual CADL review gen
  • Loading branch information
praveenkuttappan authored Dec 8, 2022
1 parent 189316a commit a0765e0
Show file tree
Hide file tree
Showing 14 changed files with 513 additions and 179 deletions.
75 changes: 75 additions & 0 deletions eng/pipelines/apiview-review-gen-cadl.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
pr: none

trigger: none

parameters:
- name: Reviews
type: string
default: '[{"ReviewID":"<reviewid>","RevisionID":"<revisionId>","SourceRepoName":"<RepoName>","FileName":"<fileName>","SourceBranchName":"<SourceBranchName>"}]'
- name: APIViewURL
type: string
default: 'https://apiview.dev'

pool:
name: azsdk-pool-mms-ubuntu-2004-general
vmImage: MMSUbuntu20.04

variables:
NodeVersion: '16.x'

jobs:
- job: CreateSwaggerReviewCodeFile
displayName: 'Create Swagger API review token file'

variables:
- template: /eng/pipelines/templates/variables/globals.yml

steps:
- task: NodeTool@0
inputs:
versionSpec: $(NodeVersion)
displayName: "Use Node $(NodeVersion)"

- pwsh: |
$reviews = "${{ parameters.Reviews }}"
Write-Host "Reviews: $($reviews)"
echo "##vso[task.setvariable variable=Reviews]$reviews"
displayName: "Setup Reviews Variable"
condition: eq(variables['Reviews'], '')
- pwsh: |
$url = "${{parameters.APIViewURL}}"
echo "##vso[task.setvariable variable=APIViewURL]$url"
displayName: "Setup APIViewURL Variable"
condition: eq(variables['APIViewURL'], '')
- pwsh: |
npm install -g cadl
npm install -g @cadl-lang/compiler
npm install -g @azure-tools/cadl-apiview
displayName: "Install npm packages"
- task: Powershell@2
displayName: 'Generate APIView Token files'
inputs:
pwsh: true
filePath: $(Build.SourcesDirectory)/eng/scripts/Create-Apiview-Token-Cadl.ps1
arguments: >
-Reviews "$(Reviews)"
-OutputDir "$(Build.ArtifactStagingDirectory)"
-WorkingDir "$(Pipeline.Workspace)"
- task: PublishBuildArtifacts@1
inputs:
pathtoPublish: '$(Build.ArtifactStagingDirectory)'
artifactName: 'apiview'

- task: Powershell@2
displayName: 'Send Request to APIView to Update Token files'
condition: succeededOrFailed()
inputs:
pwsh: true
filePath: $(Build.SourcesDirectory)/eng/scripts/Apiview-Update-Generated-Review.ps1
arguments: >
-BuildId $(Build.BuildId)
-ApiviewUpdateUrl "$(APIViewURL)/review/UpdateApiReview"
98 changes: 98 additions & 0 deletions eng/scripts/Create-Apiview-Token-Cadl.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
[CmdletBinding()]
param (
[ValidateNotNullOrEmpty()]
[string] $Reviews,
[ValidateNotNullOrEmpty()]
[string] $WorkingDir,
[ValidateNotNullOrEmpty()]
[string] $OutputDir
)

Set-StrictMode -Version 3
$sparseCheckoutFile = ".git/info/sparse-checkout"


function Sparse-Checkout($branchName, $packagePath)
{
Remove-Item $sparseCheckoutFile -Force
git sparse-checkout init --cone
git sparse-checkout set $packagePath
git checkout $BranchName
}

function Generate-Apiview-File($packagePath)
{
Write-Host "Generating API review token file from path '$($packagePath)'"
Push-Location $packagePath
try
{
npm install
cadl compile . --emit=@azure-tools/cadl-apiview
}
finally
{
Pop-Location
}
}

function Stage-Apiview-File($packagePath, $reviewId, $revisionId)
{
$tokenFilePath = Join-Path $packagePath "cadl-output"
$stagingReviewPath = Join-Path $OutputDir $reviewId
$stagingPath = Join-Path $stagingReviewPath $revisionId
Write-Host "Copying APIView file from '$($tokenFilePath)' to '$($stagingPath)'"
New-Item $stagingPath -ItemType Directory -Force
Copy-Item -Destination $stagingPath -Path "$tokenFilePath/*"
}


Write-Host "Review Details Json: $($Reviews)"
$revs = ConvertFrom-Json $Reviews
if ($revs)
{
$prevRepo = ""
foreach ($r in $revs)
{
$reviewId = $r.ReviewID
$revisionId = $r.RevisionID
$packagePath = $r.FileName
$GitRepoName = $r.SourceRepoName
$branchName = $r.SourceBranchName

Write-Host "Generating API review for Review ID: '$($reviewId), Revision ID: '$($revisionId)"
Write-Host "URL to Repo: '$($GitRepoName), Branch name: '$($branchName), Package Path: '$($packagePath)"

$repoDirectory = Split-Path $GitRepoName -leaf
if (Test-Path $repoDirectory)
{
Write-Host "Destination path '$($repoDirectory)' already exists in working directory and is not an empty directory."
exit 1
}
# initialize git clone if current review is generated from different repo than previous one
if ($GitRepoName -ne $prevRepo)
{
git clone --no-checkout --filter=tree:0 "https://github.com/$GitRepoName"
if ($LASTEXITCODE) { exit $LASTEXITCODE }
$prevRepo = $GitRepoName
}

$repoDirectory = Split-Path $GitRepoName -leaf
Push-Location $repoDirectory
try
{
Write-Host "GitHub Repo Name: '$($repoDirectory)"
# Sparse checkout package root path
Sparse-Checkout -branchName $branchName -packagePath $packagePath
# Generate API code file
Generate-Apiview-File -packagePath $packagePath
#Copy generated code file to stagin location
Stage-Apiview-File -packagePath $packagePath -reviewId $reviewId -revisionId $revisionId
}
finally
{
Pop-Location
}
}

Write-Host "Generated and copied Api review token file to output directory"
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public async Task AddRevisionAsync_Computes_Headings_Of_Sections_With_Diff_A()
{
var reviewManager = testsBaseFixture.ReviewManager;
var user = testsBaseFixture.User;
var review = await testsBaseFixture.ReviewManager.CreateReviewAsync(user, fileNameA, "Revision1", fileStreamA, false, true);
var review = await testsBaseFixture.ReviewManager.CreateReviewAsync(user, fileNameA, "Revision1", fileStreamA, false, "Swagger", true);
await reviewManager.AddRevisionAsync(user, review.ReviewId, fileNameB, "Revision2", fileStreamB, true);
review = await reviewManager.GetReviewAsync(user, review.ReviewId);
var headingWithDiffInSections = review.Revisions[0].HeadingsOfSectionsWithDiff[review.Revisions[1].RevisionId];
Expand All @@ -64,7 +64,7 @@ public async Task AddRevisionAsync_Computes_Headings_Of_Sections_With_Diff_B()
{
var reviewManager = testsBaseFixture.ReviewManager;
var user = testsBaseFixture.User;
var review = await reviewManager.CreateReviewAsync(user, fileNameC, "Azure.Analytics.Purview.Account", fileStreamC, false, true);
var review = await reviewManager.CreateReviewAsync(user, fileNameC, "Azure.Analytics.Purview.Account", fileStreamC, false, "Swagger", true);
await reviewManager.AddRevisionAsync(user, review.ReviewId, fileNameD, "Azure.Analytics.Purview.Account", fileStreamD, true);
review = await reviewManager.GetReviewAsync(user, review.ReviewId);
var headingWithDiffInSections = review.Revisions[0].HeadingsOfSectionsWithDiff[review.Revisions[1].RevisionId];
Expand Down
Loading

0 comments on commit a0765e0

Please sign in to comment.