From 927475ee7fc9be9851b88c1681e979e27b0a06ff Mon Sep 17 00:00:00 2001 From: Chidozie Ononiwu Date: Thu, 23 Jan 2025 08:35:51 -0800 Subject: [PATCH] APIView Creation from PR for Go Changes in Template for testing Update go Language Settings and Detect-Api-Changes test Changes manually Update Go Analyze Step --- eng/common/scripts/Detect-Api-Changes.ps1 | 2 + eng/pipelines/templates/steps/analyze.yml | 48 ++++++--- eng/scripts/Create-ApiReview.ps1 | 39 ------- eng/scripts/Language-Settings.ps1 | 8 +- eng/scripts/apiview-helpers.ps1 | 100 ++++++++++++++++++ .../aztemplate/internal/response_types.go | 2 +- .../aztemplate/internal/template_client.go | 4 +- 7 files changed, 142 insertions(+), 61 deletions(-) delete mode 100644 eng/scripts/Create-ApiReview.ps1 create mode 100644 eng/scripts/apiview-helpers.ps1 diff --git a/eng/common/scripts/Detect-Api-Changes.ps1 b/eng/common/scripts/Detect-Api-Changes.ps1 index cdfc3cfae581..dd3a0c218a53 100644 --- a/eng/common/scripts/Detect-Api-Changes.ps1 +++ b/eng/common/scripts/Detect-Api-Changes.ps1 @@ -108,6 +108,8 @@ $packageProperties = Get-ChildItem -Recurse -Force "$configFileDir" ` $_.Extension -eq '.json' -and ($_.FullName.Substring($configFileDir.Length + 1) -notmatch '^_.*?\\') } +Write-Host "packageProperties: $packageProperties" + foreach ($packagePropFile in $packageProperties) { $packageMetadata = Get-Content $packagePropFile | ConvertFrom-Json diff --git a/eng/pipelines/templates/steps/analyze.yml b/eng/pipelines/templates/steps/analyze.yml index ae1fd0a60d2f..ace9afc72976 100644 --- a/eng/pipelines/templates/steps/analyze.yml +++ b/eng/pipelines/templates/steps/analyze.yml @@ -5,7 +5,7 @@ parameters: NonShipping: false LicenseCheck: true IsSdkLibrary: true - + ArtifactName: packages steps: - task: Powershell@2 @@ -19,6 +19,25 @@ steps: displayName: Dump Package properties condition: and(succeeded(), ${{ parameters.IsSdkLibrary }}) + - pwsh: | + . $(Build.SourcesDirectory)/eng/scripts/apiview-helpers.ps1 + $directoryToPublish = Join-Path -Path $(Build.ArtifactStagingDirectory) ${{ parameters.ArtifactName }} + + New-APIViewArtifacts ` + -ServiceDirectory '${{ parameters.ServiceDirectory }}' ` + -OutputDirectory $(Build.ArtifactStagingDirectory) ` + -DirectoryToPublish $directoryToPublish + + Copy-Item "$(Build.ArtifactStagingDirectory)/PackageInfo" -Destination $directoryToPublish -Recurse + displayName: 'Create Go APIView Artifact' + + - template: /eng/common/pipelines/templates/steps/publish-1es-artifact.yml + parameters: + ArtifactName: ${{ parameters.ArtifactName }} + ArtifactPath: "$(Build.ArtifactStagingDirectory)/${{ parameters.ArtifactName }}" + CustomCondition: and(succeeded(), ${{ parameters.IsSdkLibrary }}) + + # Second publish of PackageInfo to avoid breakng release pipeline, should be cleaned up once release pipeline is updated - template: /eng/common/pipelines/templates/steps/publish-1es-artifact.yml parameters: ArtifactName: "PackageInfo" @@ -27,23 +46,24 @@ steps: - template: /eng/common/pipelines/templates/steps/set-default-branch.yml - - task: Powershell@2 - inputs: - filePath: $(Build.SourcesDirectory)/eng/scripts/Create-ApiReview.ps1 - arguments: > - -ServiceDirectory '${{parameters.ServiceDirectory}}' - -OutPath '$(Build.ArtifactStagingDirectory)' - -ApiKey '$(azuresdk-apiview-apikey)' - -SourceBranch '$(Build.SourceBranchName)' - -DefaultBranch '$(DefaultBranch)' - -ConfigFileDir '$(Build.ArtifactStagingDirectory)/PackageInfo' - -BuildId $(Build.BuildId) + - pwsh: | + . $(Build.SourcesDirectory)/eng/scripts/apiview-helpers.ps1 + New-APIViewFromCI ` + -ServiceDirectory '${{parameters.ServiceDirectory}}' ` + -ArtifactPath '$(Build.ArtifactStagingDirectory)' ` + -ApiKey '$(azuresdk-apiview-apikey)' ` + -SourceBranch '$(Build.SourceBranchName)' ` + -DefaultBranch '$(DefaultBranch)' ` + -ConfigFileDir '$(Build.ArtifactStagingDirectory)/PackageInfo' ` + -BuildId $(Build.BuildId) ` -RepoName '$(Build.Repository.Name)' - pwsh: true - workingDirectory: $(Pipeline.Workspace) displayName: Create API review for Go + workingDirectory: $(Pipeline.Workspace) condition: and(succeeded(), ne(variables['Skip.CreateApiReview'], 'true') , ne(variables['Build.Reason'],'PullRequest'), eq(variables['System.TeamProject'], 'internal')) + - ${{ if eq(variables['Build.Reason'],'PullRequest') }}: + - template: /eng/common/pipelines/templates/steps/detect-api-changes.yml + - ${{ if and(ne(variables['Skip.PackageValidation'], 'true'), eq(variables['System.TeamProject'], 'internal')) }}: - pwsh: | . ./eng/common/scripts/common.ps1 diff --git a/eng/scripts/Create-ApiReview.ps1 b/eng/scripts/Create-ApiReview.ps1 deleted file mode 100644 index d30af2491237..000000000000 --- a/eng/scripts/Create-ApiReview.ps1 +++ /dev/null @@ -1,39 +0,0 @@ -Param( - [Parameter(Mandatory=$True)] - [string] $ServiceDirectory, - [Parameter(Mandatory=$True)] - [string] $OutPath, - [Parameter(Mandatory=$True)] - [string] $ApiKey, - [Parameter(Mandatory=$True)] - [string] $SourceBranch, - [Parameter(Mandatory=$True)] - [string] $DefaultBranch, - [Parameter(Mandatory=$True)] - [string] $ConfigFileDir, - [string] $RepoName, - [string] $BuildId, - [bool] $MarkPackageAsShipped = $false -) - - -Write-Host "$PSScriptRoot" -. (Join-Path $PSScriptRoot .. common scripts common.ps1) -$createReviewScript = (Join-Path $PSScriptRoot .. common scripts Create-APIReview.ps1) - -$artifactList = @() -foreach ($sdk in (Get-AllPackageInfoFromRepo $ServiceDirectory)) -{ - Write-Host "Creating API review artifact for $($sdk.Name)" - New-Item -ItemType Directory -Path $OutPath/$($sdk.Name) -force - $fileName = Split-Path -Path $sdk.Name -Leaf - Compress-Archive -Path $sdk.DirectoryPath -DestinationPath $outPath/$($sdk.Name)/$fileName -force - Rename-Item $outPath/$($sdk.Name)/$fileName.zip -NewName "$fileName.gosource" - - $artifactList += [PSCustomObject]@{ - name = $sdk.Name - } -} - -Write-Host "Create Go APIView using generated artifacts" -&($createReviewScript) -ArtifactList $artifactList -ArtifactPath $outPath -APIKey $ApiKey -SourceBranch $SourceBranch -DefaultBranch $DefaultBranch -ConfigFileDir $ConfigFileDir -RepoName $RepoName -BuildId $BuildId -MarkPackageAsShipped $MarkPackageAsShipped diff --git a/eng/scripts/Language-Settings.ps1 b/eng/scripts/Language-Settings.ps1 index a25fbf00e2f2..86d52164a6ed 100644 --- a/eng/scripts/Language-Settings.ps1 +++ b/eng/scripts/Language-Settings.ps1 @@ -101,13 +101,11 @@ function Get-AllPackageInfoFromRepo($serviceDirectory) $searchPath = Join-Path $RepoRoot "sdk" $pkgFiles = @() if ($serviceDirectory) { - $searchPath = Join-Path $searchPath $serviceDirectory "go.mod" - [array]$pkgFiles = @(Get-ChildItem $searchPath) - } else { - # If service directory is not passed in, find all modules - [array]$pkgFiles = Get-ChildItem -Path $searchPath -Include "go.mod" -Recurse + $searchPath = Join-Path $searchPath $serviceDirectory } + [array]$pkgFiles = Get-ChildItem -Path $searchPath -Include "go.mod" -Recurse + foreach ($pkgFile in $pkgFiles) { $modPropertes = Get-GoModuleProperties $pkgFile.DirectoryName diff --git a/eng/scripts/apiview-helpers.ps1 b/eng/scripts/apiview-helpers.ps1 new file mode 100644 index 000000000000..8628a60849b5 --- /dev/null +++ b/eng/scripts/apiview-helpers.ps1 @@ -0,0 +1,100 @@ +Write-Host "$PSScriptRoot" +. (Join-Path $PSScriptRoot .. common scripts common.ps1) + +<# +.DESCRIPTION + Create .gosource APIVIew artifact for go +.PARAMETER ServiceDirectory + Thee name of the ServiceDirectory +.PARAMETER OutputDirectory + Base output Directory path for the generated gosource artifacts +.PARAMETER DirectoryToPublish + Directory containing all artifacts to be publisehd to the pipeline +#> +function New-APIViewArtifacts { + Param( + [Parameter(Mandatory=$True)] + [string] $ServiceDirectory, + [Parameter(Mandatory=$True)] + [string] $OutputDirectory, + [Parameter(Mandatory=$True)] + [string] $DirectoryToPublish + ) + + foreach ($sdk in (Get-AllPackageInfoFromRepo $ServiceDirectory)) + { + Write-Host "Creating API review artifact for $($sdk.Name)" + $sdkDirectoryPath = Join-Path -Path $OutputDirectory $sdk.Name + New-Item -ItemType Directory -Path $sdkDirectoryPath -force + $fileName = Split-Path -Path $sdk.Name -Leaf + $compressedArchivePath = Join-Path $sdkDirectoryPath "$fileName.zip" + Compress-Archive -Path $sdk.DirectoryPath -DestinationPath $compressedArchivePath -force + Rename-Item $compressedArchivePath -NewName "$fileName.gosource" + + $artifactParentDirectory = $sdk.Name -Split "/" | Select-Object -First 1 + Copy-Item "$OutputDirectory/$artifactParentDirectory" -Destination "$DirectoryToPublish/$artifactParentDirectory" -Recurse + } +} + +<# +.DESCRIPTION + Create new automatic APIView from a CI run +.PARAMETER ServiceDirectory + Thee name of the ServiceDirectory +.PARAMETER ArtifactPath + Directory containing the gosources artifact +.PARAMETER ApiKey + The APIview ApiKey +.PARAMETER SourceBranch + SourceBranch +.PARAMETER DefaultBranch + DefaultBranch +.PARAMETER ConfigFileDir + Path to the ConfigFileDir as published in the pipeline +.PARAMETER RepoName + The name of the repository +.PARAMETER BuildId + The build Id of the pipeline run +.PARAMETER MarkPackageAsShipped + Indicate weather to mark the package a s shipped +#> +function New-APIViewFromCI { + Param( + [Parameter(Mandatory=$True)] + [string] $ServiceDirectory, + [Parameter(Mandatory=$True)] + [string] $ArtifactPath, + [Parameter(Mandatory=$True)] + [string] $ApiKey, + [Parameter(Mandatory=$True)] + [string] $SourceBranch, + [Parameter(Mandatory=$True)] + [string] $DefaultBranch, + [Parameter(Mandatory=$True)] + [string] $ConfigFileDir, + [string] $RepoName, + [string] $BuildId, + [bool] $MarkPackageAsShipped = $false + ) + $artifactList = @() + + Get-AllPackageInfoFromRepo $ServiceDirectory | ForEach-Object { + $artifactList += [PSCustomObject]@{ + name = $sdk.Name + } + } + + $createReviewScript = (Join-Path $PSScriptRoot .. common scripts Create-APIReview.ps1) + + Write-Host "Create Go APIView using generated artifacts" + &($createReviewScript) ` + -ArtifactList $artifactList ` + -ArtifactPath $ArtifactPath ` + -APIKey $ApiKey ` + -SourceBranch $SourceBranch ` + -DefaultBranch $DefaultBranch ` + -ConfigFileDir $ConfigFileDir ` + -RepoName $RepoName ` + -BuildId $BuildId ` + -MarkPackageAsShipped $MarkPackageAsShipped +} \ No newline at end of file diff --git a/sdk/template/aztemplate/internal/response_types.go b/sdk/template/aztemplate/internal/response_types.go index ebebd21bc099..8130553e263b 100644 --- a/sdk/template/aztemplate/internal/response_types.go +++ b/sdk/template/aztemplate/internal/response_types.go @@ -9,7 +9,7 @@ package internal // TemplateClientSomeAPIResponse contains the response from method TemplateClient.SomeAPI. -type TemplateClientSomeAPIResponse struct { +type TemplateClientSomeAPIResponses struct { // can contain header values, unmarshaled response body payload etc Value *string } diff --git a/sdk/template/aztemplate/internal/template_client.go b/sdk/template/aztemplate/internal/template_client.go index 745cc1d8f880..c267571ab86b 100644 --- a/sdk/template/aztemplate/internal/template_client.go +++ b/sdk/template/aztemplate/internal/template_client.go @@ -27,6 +27,6 @@ func NewTemplateClient(pl runtime.Pipeline) *TemplateClient { } // SomeAPI is an example of an API. -func (t *TemplateClient) SomeAPI(ctx context.Context, options *SomeAPIOptions) (TemplateClientSomeAPIResponse, error) { - return TemplateClientSomeAPIResponse{Value: to.Ptr("value")}, nil +func (t *TemplateClient) SomeAPI(ctx context.Context, options *SomeAPIOptions) (TemplateClientSomeAPIResponses, error) { + return TemplateClientSomeAPIResponses{Value: to.Ptr("value")}, nil }