Skip to content

Commit

Permalink
Merge branch 'main' into docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mario-guerra authored Dec 11, 2024
2 parents 069d54a + faebe07 commit 5ff1bda
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 9 deletions.
54 changes: 54 additions & 0 deletions eng/pipelines/typespec-apiview.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
parameters:
- name: APIViewArtifactsDirectoryName
type: string
default: 'TypespecAPIViewArtifacts'
- name: APIViewArtifactsName
type: string
default: 'typeSpecAPIViewArtifacts'
- name: APIViewAPIUri
type: string
default: 'https://apiview.dev/PullRequest/DetectAPIChanges'
# Please use 'https://apiviewstagingtest.com/PullRequest/DetectAPIChanges' for testing purposes

jobs:
- job:
pool:
name: azsdk-pool-mms-ubuntu-2204-general
vmImage: ubuntu-22.04

steps:
- checkout: self
fetchDepth: 0

- template: /eng/pipelines/templates/steps/npm-install.yml

- pwsh: |
. $(Build.SourcesDirectory)/eng/scripts/Create-APIView.ps1
New-TypeSpecAPIViewTokens `
-TempDirectory "$(Agent.TempDirectory)" `
-ArtifactsStagingDirectory "$(Build.ArtifactStagingDirectory)" `
-APIViewArtifactsDirectoryName "${{ parameters.APIViewArtifactsDirectoryName }}"
displayName: Generate TypeSpec APIView Tokens
- task: PublishPipelineArtifact@1
inputs:
targetPath: '$(Build.ArtifactStagingDirectory)/${{ parameters.APIViewArtifactsDirectoryName }}'
artifactName: '${{ parameters.APIViewArtifactsName }}'
publishLocation: 'pipeline'
displayName: 'Publish TypeSpec APIView Artifacts'
condition: and(succeeded(), ne(variables['Agent.JobStatus'], 'SucceededWithIssues'))

- pwsh: |
. $(Build.SourcesDirectory)/eng/scripts/Create-APIView.ps1
New-RestSpecsAPIViewReviews `
-ArtiFactsStagingDirectory $(Build.ArtifactStagingDirectory) `
-APIViewArtifactsDirectoryName ${{ parameters.APIViewArtifactsDirectoryName }} `
-APIViewArtifactsName ${{ parameters.APIViewArtifactsName }} `
-APIViewUri ${{ parameters.APIViewAPIUri }} `
-BuildId $(Build.BuildId) `
-RepoName $(Build.Repository.Name) `
-PullRequestNumber $(System.PullRequest.PullRequestNumber)`
-Language 'TypeSpec' `
-CommitSha $(Build.SourceVersion)
displayName: Create TypeSpec APIView
condition: and(succeeded(), ne(variables['Agent.JobStatus'], 'SucceededWithIssues'))
10 changes: 10 additions & 0 deletions eng/scripts/ChangedFiles-Functions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ function Get-ChangedSwaggerFiles($changedFiles = (Get-ChangedFiles)) {
return $changedSwaggerFiles
}

function Get-ChangedTypeSpecFiles($changedFiles = (Get-ChangedFiles)) {
$changedFiles = Get-ChangedFilesUnderSpecification $changedFiles

$changedTypeSpecFiles = $changedFiles.Where({
$_.EndsWith(".tsp")
})

return $changedTypeSpecFiles
}

function Get-ChangedFilesUnderSpecification($changedFiles = (Get-ChangedFiles)) {
$changedFilesUnderSpecification = $changedFiles.Where({
$_.StartsWith("specification")
Expand Down
152 changes: 145 additions & 7 deletions eng/scripts/Create-APIView.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,68 @@ function Invoke-SwaggerAPIViewParser {
}
}

<#
.DESCRIPTION
Invoke the TypeSpec parser to generate APIView tokens.
.PARAMETER Type
New or Baseline TypeSpec APIView tokens.
.PARAMETER ProjectPath
The TypeSpec Project path.
.PARAMETER ResourceProvider
The ResourceProvider Name.
.PARAMETER Tag
The Tag to use for generating the APIView Tokens.
.PARAMETER TokenDirectory
The directory to store the generated APIView Tokens.
.OUTPUTS
The resource provider name.
#>
function Invoke-TypeSpecAPIViewParser {
param (
[ValidateSet("New", "Baseline")]
[Parameter(Mandatory = $true)]
[string]$Type,
[Parameter(Mandatory = $true)]
[string]$ProjectPath,
[Parameter(Mandatory = $true)]
[string]$ResourceProvider,
[Parameter(Mandatory = $true)]
[string]$TokenDirectory
)
$tempWorkingDirectoryName = [guid]::NewGuid().ToString()
$tempWorkingDirectoryPath = [System.IO.Path]::Combine($TempDirectory, $tempWorkingDirectoryName)
New-Item -ItemType Directory -Path $tempWorkingDirectoryPath > $null

try {
Write-Host "Compiling files and generating '$Type' APIView for '$resourceProvider'..."
Push-Location $ProjectPath
Write-Host "npm exec --no -- tsp compile . --emit=@azure-tools/typespec-apiview --option @azure-tools/typespec-apiview.emitter-output-dir=$tempWorkingDirectoryPath/output/apiview.json"
npm exec --no -- tsp compile . --emit=@azure-tools/typespec-apiview --option @azure-tools/typespec-apiview.emitter-output-dir=$tempWorkingDirectoryPath/output/apiview.json
if ($LASTEXITCODE) {
throw
}
Pop-Location

$generatedAPIViewTokenFile = Get-ChildItem -File $tempWorkingDirectoryPath/output/apiview.json | Select-Object -First 1
$apiViewTokensFilePath = [System.IO.Path]::Combine($TokenDirectory, "$resourceProvider.$Type.json")
Write-Host "Moving generated APIView Token file to '$apiViewTokensFilePath'"
Move-Item -Path $generatedAPIViewTokenFile.FullName -Destination $apiViewTokensFilePath -Force > $null
} catch {
LogError " Failed to generate '$Type' APIView Tokens on '$ProjectPath' for '$resourceProvider', please check the detail log and make sure TypeSpec compiler version is the latest."
throw
} finally {
if (Test-Path -Path $tempWorkingDirectoryPath) {
Remove-Item -Path $tempWorkingDirectoryPath -Recurse -Force > $null
}
}
}

<#
.DESCRIPTION
Generate New and Baseline APIView tokens for the changed swagger files in the PR.
Expand All @@ -177,7 +239,7 @@ function Invoke-SwaggerAPIViewParser {
.PARAMETER TempDirectory
Temporary directory for files being processed. Use $(Agent.TempDirectory) on DevOps
.PARAMETER ArtiFactsStagingDirectory
.PARAMETER ArtifactsStagingDirectory
The directory where the APIView tokens will be stored. Use $(Build.ArtifactStagingDirectory) on DevOps
.PARAMETER APIViewArtifactsDirectoryName
Expand All @@ -188,7 +250,7 @@ function New-SwaggerAPIViewTokens {
[Parameter(Mandatory = $true)]
[string]$TempDirectory,
[Parameter(Mandatory = $true)]
[string]$ArtiFactsStagingDirectory,
[string]$ArtifactsStagingDirectory,
[Parameter(Mandatory = $true)]
[string]$APIViewArtifactsDirectoryName
)
Expand Down Expand Up @@ -230,7 +292,7 @@ function New-SwaggerAPIViewTokens {

$currentBranch = git rev-parse --abbrev-ref HEAD

$swaggerAPIViewArtifactsDirectory = [System.IO.Path]::Combine($ArtiFactsStagingDirectory, $APIViewArtifactsDirectoryName)
$swaggerAPIViewArtifactsDirectory = [System.IO.Path]::Combine($ArtifactsStagingDirectory, $APIViewArtifactsDirectoryName)

# Generate Swagger APIView Tokens
foreach ($entry in $autoRestConfigInfo.GetEnumerator()) {
Expand Down Expand Up @@ -273,11 +335,87 @@ function New-SwaggerAPIViewTokens {
LogGroupEnd
}

<#
.DESCRIPTION
Generate New and Baseline APIView tokens for the changed TypeSpec files in the PR.
Detects the TypeSpec files changed in the PR and generates APIView tokens for the TypeSpec files.
New APIView tokens are generated using the default tag on the base branch.
Baseline APIView tokens are generated using the same tag on the target branch.
Script asumes that the merge commit is checked out. Such that Source commit = HEAD^ and Target commit = HEAD.
.PARAMETER TempDirectory
Temporary directory for files being processed. Use $(Agent.TempDirectory) on DevOps
.PARAMETER ArtifactsStagingDirectory
The directory where the APIView tokens will be stored. Use $(Build.ArtifactStagingDirectory) on DevOps
.PARAMETER APIViewArtifactsDirectoryName
Name for the subdirectory where the APIView tokens will be stored.
#>
function New-TypeSpecAPIViewTokens {
param (
[Parameter(Mandatory = $true)]
[string]$TempDirectory,
[Parameter(Mandatory = $true)]
[string]$ArtifactsStagingDirectory,
[Parameter(Mandatory = $true)]
[string]$APIViewArtifactsDirectoryName
)

$SourceCommitId = $(git rev-parse HEAD^)
$TargetCommitId = $(git rev-parse HEAD)

$typeSpecProjects, $null = &"$PSScriptRoot/Get-TypeSpec-Folders.ps1" `
-IgnoreCoreFiles:$true `
-BaseCommitish:$SourceCommitId `
-TargetCommitish:$TargetCommitId

LogGroupStart " TypeSpec APIView Tokens will be generated for the following configuration files..."
$typeSpecProjects | ForEach-Object {
LogInfo " - $_"
}
LogGroupEnd

$currentBranch = git rev-parse --abbrev-ref HEAD

$typeSpecAPIViewArtifactsDirectory = [System.IO.Path]::Combine($ArtifactsStagingDirectory, $APIViewArtifactsDirectoryName)
New-Item -ItemType Directory -Path $typeSpecAPIViewArtifactsDirectory -Force | Out-Null

try {
# Generate TypeSpec APIView Tokens
foreach ($typeSpecProject in $typeSpecProjects) {
$tokenDirectory = [System.IO.Path]::Combine($typeSpecAPIViewArtifactsDirectory, $typeSpecProject.split([IO.Path]::DirectorySeparatorChar)[-1])
New-Item -ItemType Directory -Path $tokenDirectory -Force | Out-Null

# Generate New APIView Token using default tag on base branch
git checkout $SourceCommitId
Invoke-TypeSpecAPIViewParser -Type "New" -ProjectPath $typeSpecProject -ResourceProvider $($typeSpecProject.split([IO.Path]::DirectorySeparatorChar)[-1]) -TokenDirectory $tokenDirectory

# Generate BaseLine APIView Token using same tag on target branch
git checkout $TargetCommitId

# Skip Baseline APIView Token for new projects
if (!(Test-Path -Path $typeSpecProject)) {
Write-Host "TypeSpec project $typeSpecProjectDir is not found in pull request target branch. API review will not have a baseline revision."
}
else {
Invoke-TypeSpecAPIViewParser -Type "Baseline" -ProjectPath $typeSpecProject -ResourceProvider $($typeSpecProject.split([IO.Path]::DirectorySeparatorChar)[-1]) -TokenDirectory $tokenDirectory | Out-Null
}
}
}
finally {
git checkout $currentBranch
LogGroupStart " See all generated TypeSpec APIView Artifacts..."
Get-ChildItem -Path $typeSpecAPIViewArtifactsDirectory -Recurse
LogGroupEnd
}
}

<#
.DESCRIPTION
Create APIView for the published packages. Send DevOps artifacts information to APIView to create APIView for the published packages.
.PARAMETER ArtiFactsStagingDirectory
.PARAMETER ArtifactsStagingDirectory
The DevOps artifacts staging directory. Use $(Build.ArtifactStagingDirectory) on DevOps
.PARAMETER APIViewArtifactsDirectoryName
Temporary Directory for processing the APIView artifacts
Expand All @@ -292,14 +430,14 @@ TGhe BuildId of the Run
.PARAMETER PullRequestNumber
The PR number
.PARAMETER Language
The language of the resource provider `Swagger`
The language of the resource provider
.PARAMETER CommitSha
The commit sha of the current branch. Uusally the merge commit of the PR.
#>
function New-RestSpecsAPIViewReviews {
param (
[Parameter(Mandatory = $true)]
[string]$ArtiFactsStagingDirectory,
[string]$ArtifactsStagingDirectory,
[Parameter(Mandatory = $true)]
[string]$APIViewArtifactsDirectoryName,
[Parameter(Mandatory = $true)]
Expand All @@ -318,7 +456,7 @@ function New-RestSpecsAPIViewReviews {
[string]$CommitSha
)

$apiViewArtifactsDirectory = [System.IO.Path]::Combine($ArtiFactsStagingDirectory, $APIViewArtifactsDirectoryName)
$apiViewArtifactsDirectory = [System.IO.Path]::Combine($ArtifactsStagingDirectory, $APIViewArtifactsDirectoryName)
$publishedPackages = Get-ChildItem -Path $apiViewArtifactsDirectory -Directory -ErrorAction SilentlyContinue

Write-Host "Published packages: $publishedPackages"
Expand Down
7 changes: 5 additions & 2 deletions eng/scripts/Get-TypeSpec-Folders.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[CmdletBinding()]
param (
[switch]$IgnoreCoreFiles = $false,
[switch]$CheckAll = $false,
[string]$BaseCommitish = "HEAD^",
[string]$TargetCommitish = "HEAD"
Expand All @@ -21,7 +22,7 @@ else {
$changedFiles = @(Get-ChangedFiles -baseCommitish $BaseCommitish -targetCommitish $TargetCommitish -diffFilter "")
$coreChangedFiles = Get-ChangedCoreFiles $changedFiles

if ($coreChangedFiles) {
if ($coreChangedFiles -and !$IgnoreCoreFiles) {
Write-Verbose "Found changes to core eng or root files so checking all specs."
$changedFiles = $checkAllPath
$checkedAll = $true
Expand Down Expand Up @@ -51,6 +52,8 @@ foreach ($skippedTypespecFolder in $skippedTypespecFolders | Select-Object -Uniq
Write-Host "Cannot find directory $skippedTypespecFolder"
}

$typespecFolders = $typespecFolders | ForEach-Object { [IO.Path]::GetRelativePath($repoPath, $_) -replace '\\', '/' } | Sort-Object -Unique
if ($typespecFolders.Length) {
$typespecFolders = $typespecFolders | ForEach-Object { [IO.Path]::GetRelativePath($repoPath, $_) -replace '\\', '/' } | Sort-Object -Unique
}

return @($typespecFolders, $checkedAll)

0 comments on commit 5ff1bda

Please sign in to comment.