Skip to content

Commit

Permalink
Uniform logging for sdk automation (#45141)
Browse files Browse the repository at this point in the history
* Uniform logging for sdk automation
-added logging prefix and next steps
-filter logging in sdk automation

* Update eng/scripts/Invoke-GenerateAndBuildV2.ps1

Co-authored-by: Crystal YU <chunyu@microsoft.com>

* Update eng/scripts/automation/GenerateAndBuildLib.ps1

Co-authored-by: Crystal YU <chunyu@microsoft.com>

* Added guidance to FAQ and exitcode

* Update logging format

* Use write-host for better format

* Added packagename to the output

---------

Co-authored-by: Crystal YU <chunyu@microsoft.com>
  • Loading branch information
raych1 and chunyu3 authored Jul 25, 2024
1 parent f3612e7 commit 78f27f2
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 29 deletions.
10 changes: 8 additions & 2 deletions eng/scripts/Invoke-GenerateAndBuildV2.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ for ($i = 0; $i -le $readmeFiles.Count - 1; $i++) {
$readme = "https://github.com/$org/azure-rest-api-specs/blob/$commitid/$readmeFile"
}
} else {
throw "No readme File path provided."
throw "[ERROR] Neither 'specFolder' nor 'headSha' is provided for `$readmeFile`. Please report this issue through https://aka.ms/azsdk/support/specreview-channel and include this pull request."
}

if ($autorestConfigYaml) {
Expand Down Expand Up @@ -109,6 +109,7 @@ if ($inputFileToGen) {
UpdateExistingSDKByInputFiles -inputFilePaths $inputFileToGen -sdkRootPath $sdkPath -headSha $commitid -repoHttpsUrl $repoHttpsUrl -downloadUrlPrefix "$downloadUrlPrefix" -generatedSDKPackages $generatedSDKPackages
}

$exitCode = 0
# generate sdk from typespec file
if ($relatedTypeSpecProjectFolder) {
foreach ($typespecRelativeFolder in $relatedTypeSpecProjectFolder) {
Expand All @@ -126,6 +127,7 @@ if ($relatedTypeSpecProjectFolder) {
$serviceType = "resource-manager"
}
$repo = $repoHttpsUrl -replace "https://github.com/", ""
Write-host "Start to call tsp-client to generate package:$packageName"
$tspclientCommand = "npx --package=@azure-tools/typespec-client-generator-cli --yes tsp-client init --tsp-config $tspConfigFile --repo $repo --commit $commitid"
if ($swaggerDir) {
$tspclientCommand += " --local-spec-repo $typespecFolder"
Expand All @@ -134,11 +136,14 @@ if ($relatedTypeSpecProjectFolder) {
Invoke-Expression $tspclientCommand
if ($LASTEXITCODE) {
# If Process script call fails, then return with failure to CI and don't need to call GeneratePackage
Write-Error "Failed to generate typespec project. Exit code: $LASTEXITCODE"
Write-Error "[ERROR] Failed to generate typespec project:$typespecFolder. Exit code: $LASTEXITCODE."
Write-Error "[ERROR] Please review the detail errors for potential fixes. For guidance, visit https://aka.ms/azsdk/sdk-automation-faq ."
Write-Error "[ERROR] If the issue persists after re-running, contact the DotNet language support channel at $DotNetSupportChannelLink and include this spec pull request."
$generatedSDKPackages.Add(@{
result = "failed";
path=@("");
})
$exitCode = $LASTEXITCODE
} else {
$relativeSdkPath = Resolve-Path $sdkProjectFolder -Relative
GeneratePackage `
Expand All @@ -157,3 +162,4 @@ $outputJson = [PSCustomObject]@{
packages = $generatedSDKPackages
}
$outputJson | ConvertTo-Json -depth 100 | Out-File $outputJsonFile
exit $exitCode
83 changes: 57 additions & 26 deletions eng/scripts/automation/GenerateAndBuildLib.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ function CreateOrUpdateAutorestConfigFile() {
$autorestConfigYaml = "# $namespace`n" + '``` yaml' + "`n$autorestConfigYaml" + '```' + "`n";
$autorestConfigYaml | Out-File $autorestFilePath
} else {
Throw "autorest.md does not exist, and no autorest configuration to create one."
Throw "[ERROR] autorest.md does not exist, and no autorest configuration to create one. Please provide the necessary autorest configuration."
}
}
}
Expand Down Expand Up @@ -265,7 +265,7 @@ function New-DataPlanePackageFolder() {
} else {
Write-Host "Path doesn't exist. create template."
if ($inputfile -eq "" -And $readme -eq "") {
Throw "Error: input file should not be empty."
Throw "[ERROR] Input file should not be empty. Please provide a valid input file or readme file."
}
dotnet new -i $sdkPath/sdk/template
Write-Host "Create project folder $projectFolder"
Expand All @@ -276,7 +276,7 @@ function New-DataPlanePackageFolder() {
Push-Location $serviceFolder
$namespaceArray = $namespace.Split(".")
if ( $namespaceArray.Count -lt 3) {
Throw "Error: invalid namespace name."
Throw "[ERROR] Invalid namespace name provided: $namespace. Please provide valid namespace."
}

$endIndex = $namespaceArray.Count - 2
Expand Down Expand Up @@ -497,7 +497,7 @@ function New-TypeSpecPackageFolder() {
Push-Location $serviceFolder
$namespaceArray = $namespace.Split(".")
if ( $namespaceArray.Count -lt 3) {
Throw "Error: invalid namespace name."
Throw "[ERROR] Invalid namespace name provided: $namespace. Please provide valid namespace."
}

$endIndex = $namespaceArray.Count - 2
Expand Down Expand Up @@ -561,7 +561,7 @@ function Get-ResourceProviderFromReadme($readmeFile) {
$pathArray = $readmeFile.Split("/");

if ( $pathArray.Count -lt 3) {
Throw "Error: invalid readme file path. A valid readme file path should contain specName and serviceType and be of the form <specName>/<serviceType>/readme.md, e.g. specification/deviceupdate/data-plane/readme.md"
Throw "[ERROR] Invalid readme file path: $readmeFile. A valid readme file path should contain specName and serviceType and be of the form <specName>/<serviceType>/readme.md, e.g. specification/deviceupdate/data-plane/readme.md"
}

$index = [array]::indexof($pathArray, "data-plane")
Expand All @@ -576,7 +576,7 @@ function Get-ResourceProviderFromReadme($readmeFile) {
return $specName, $serviceType
}

Throw "Fail to retrive the service name and type."
Throw "[ERROR] Fail to retrive the service name and type from $readmeFile. Please provide a valid readme file path, e.g. specification/deviceupdate/data-plane/readme.md"
}

<#
Expand Down Expand Up @@ -607,6 +607,8 @@ Run script with default parameters.
Invoke-GenerateAndBuildSDK -readmeAbsolutePath <path-to-readme> -sdkRootPath <path-to-sdk-root-directory> -generatedSDKPackages <package-object-list>
#>

$DotNetSupportChannelLink = "https://teams.microsoft.com/l/channel/19%3A7b87fb348f224b37b6206fa9d89a105b%40thread.skype/Language%20-%20DotNet?groupId=3e17dcb0-4257-4a30-b843-77f47f1d4121&tenantId="
function Invoke-GenerateAndBuildSDK () {
param(
[string]$readmeAbsolutePath,
Expand All @@ -621,7 +623,7 @@ function Invoke-GenerateAndBuildSDK () {
Write-Host "service:$service, serviceType:$serviceType"

if (!$readmeFile.StartsWith("http") -And !(Test-Path -Path $readmeFile)) {
Write-Error "readme file '$readmeFile' does not exist."
Write-Error "[ERROR] readme file '$readmeFile' does not exist. Please provide a valid readme file path."
exit 1
}

Expand All @@ -635,7 +637,10 @@ function Invoke-GenerateAndBuildSDK () {
}
New-MgmtPackageFolder -service $service -packageName $package -sdkPath $sdkRootPath -commitid $commitid -readme $readmeFile -outputJsonFile $newpackageoutput
if ( !$?) {
Write-Error "Failed to create sdk project folder. exit code: $?"
Write-Host "[ERROR] Failed to create sdk project folder.service:$service,package:$package,"
Write-Host "[ERROR] sdkPath:$sdkRootPath,readme:$readmeFile.exit code: $?."
Write-Host "[ERROR] Please review the detail errors for potential fixes. For guidance, visit https://aka.ms/azsdk/sdk-automation-faq ."
Write-Host "[ERROR] If the issue persists, contact the DotNet language support channel at $DotNetSupportChannelLink and include this spec pull request."
exit 1
}
$newPackageOutputJson = Get-Content $newPackageOutput | Out-String | ConvertFrom-Json
Expand Down Expand Up @@ -665,7 +670,10 @@ function Invoke-GenerateAndBuildSDK () {

New-DataPlanePackageFolder -service $service -namespace $namespace -sdkPath $sdkRootPath -readme $readmeFile -autorestConfigYaml "$autorestConfigYaml" -outputJsonFile $newpackageoutput
if ( !$? ) {
Write-Error "Failed to create sdk project folder. exit code: $?"
Write-Host "[ERROR] Failed to create sdk project folder.service:$service,namespace:$namespace,"
Write-Host "[ERROR] sdkPath:$sdkRootPath,readme:$readmeFile,autorestConfigYaml:$autorestConfigYaml.exit code: $?."
Write-Host "[ERROR] Please review the detail errors for potential fixes. For guidance, visit https://aka.ms/azsdk/sdk-automation-faq ."
Write-Host "[ERROR] If the issue persists, contact the DotNet language support channel at $DotNetSupportChannelLink and include this spec pull request."
exit 1
}
$newPackageOutputJson = Get-Content $newPackageOutput | Out-String | ConvertFrom-Json
Expand All @@ -691,7 +699,10 @@ function Invoke-GenerateAndBuildSDK () {
if ($fileContent -match $regexForMatch) {
New-DataPlanePackageFolder -service $service -namespace $folder -sdkPath $sdkRootPath -readme $readmeFile -outputJsonFile $newpackageoutput
if ( !$? ) {
Write-Error "Failed to create sdk project folder. exit code: $?"
Write-Host "[ERROR] Failed to create sdk project folder.service:$service,namespace:$folder,"
Write-Host "[ERROR] sdkPath:$sdkRootPath,readme:$readmeFile. exit code: $?."
Write-Host "[ERROR] Please review the detail errors for potential fixes. For guidance, visit https://aka.ms/azsdk/sdk-automation-faq ."
Write-Host "[ERROR] If the issue persists, contact the DotNet language support channel at $DotNetSupportChannelLink and include this spec pull request."
exit 1
}
$newPackageOutputJson = Get-Content $newPackageOutput | Out-String | ConvertFrom-Json
Expand Down Expand Up @@ -735,9 +746,10 @@ function GeneratePackage()
Write-Host "Generating code for " $packageName
$artifacts = @()
$apiViewArtifact = ""
$hasBreakingChange = $null
$content = $null
$hasBreakingChange = $false
$content = ""
$result = "succeeded"
$isGenerateSuccess = $true

# Generate Code
$srcPath = Join-Path $projectFolder 'src'
Expand All @@ -750,18 +762,22 @@ function GeneratePackage()
dotnet build /t:GenerateCode $srcPath /p:SpecRepoRoot=$specRepoRoot
}
if ( !$?) {
Write-Error "Failed to generate sdk. exit code: $?"
Write-Host "[ERROR] Failed to generate sdk for package:$packageName. Exit code: $?."
Write-Host "[ERROR] Please review the detail errors for potential fixes. For guidance, visit https://aka.ms/azsdk/sdk-automation-faq ."
Write-Host "[ERROR] If the issue persists, contact the DotNet language support channel at $DotNetSupportChannelLink and include this spec pull request."
$result = "failed"
$isGenerateSuccess = $false
}
}

if ( $?) {
Write-Host "Start to generate sdk package $projectFolder"
if ($isGenerateSuccess) {
# Build project when successfully generated the code
Write-Host "Start to build sdk project: $srcPath"
dotnet build $srcPath /p:RunApiCompat=$false
if ( !$?) {
Write-Error "Failed to build project. exit code: $?"
Write-Host "[ERROR] Failed to build the sdk project: $packageName for service: $service. Exit code: $?."
Write-Host "[ERROR] Please review the detail errors for potential fixes. For guidance, visit https://aka.ms/azsdk/sdk-automation-faq ."
Write-Host "[ERROR] If the issue persists, contact the DotNet language support channel at $DotNetSupportChannelLink and include this spec pull request."
$result = "failed"
} else {
# Build the whole solution and generate artifacts if the project build successfully
Expand All @@ -770,29 +786,43 @@ function GeneratePackage()
$serviceProjFilePath = Join-Path $sdkRootPath 'eng' 'service.proj'
dotnet build /p:Scope=$service /p:Project=$packageName /p:RunApiCompat=$false $serviceProjFilePath
if ( !$? ) {
Write-Error "Failed to build sdk solution. exit code: $?"
Write-Host "[ERROR] Failed to build sdk solution:$packageName. Exit code: $?."
Write-Host "[ERROR] Please review the detail errors for potential fixes. For guidance, visit https://aka.ms/azsdk/sdk-automation-faq ."
Write-Host "[ERROR] If the issue persists, contact the DotNet language support channel at $DotNetSupportChannelLink and include this spec pull request."
$result = "failed"
}
# pack
Write-Host "Start to pack sdk"
dotnet pack $srcPath /p:RunApiCompat=$false
if ( !$? ) {
Write-Error "Failed to pack sdk. exit code: $?"
Write-Host "[ERROR] Failed to pack the sdk package: $packageName for service: $service. Exit code: $?."
Write-Host "[ERROR] Please review the detail errors for potential fixes. For guidance, visit https://aka.ms/azsdk/sdk-automation-faq ."
Write-Host "[ERROR] If the issue persists, contact the DotNet language support channel at $DotNetSupportChannelLink and include this spec pull request."
$result = "failed"
} else {
# artifacts
Push-Location $sdkRootPath
# check the artifact in Debug folder
$artifactsPath = (Join-Path "artifacts" "packages" "Debug" $packageName)
$artifacts += Get-ChildItem $artifactsPath -Filter *.nupkg -exclude *.symbols.nupkg -Recurse | Select-Object -ExpandProperty FullName | Resolve-Path -Relative
if ($artifacts.count -le 0) {
if (Test-Path $artifactsPath) {
$artifacts += Get-ChildItem $artifactsPath -Filter *.nupkg -exclude *.symbols.nupkg -Recurse | Select-Object -ExpandProperty FullName | Resolve-Path -Relative
}
if ($artifacts.count -eq 0) {
# check the artifact in Release folder
$artifactsPath = (Join-Path "artifacts" "packages" "Release" $packageName)
$artifacts += Get-ChildItem $artifactsPath -Filter *.nupkg -exclude *.symbols.nupkg -Recurse | Select-Object -ExpandProperty FullName | Resolve-Path -Relative
if (-not (Test-Path $artifactsPath)) {
Write-Host "[ERROR] Artifact folder not found for $artifactsPath."
Write-Host "[ERROR] Please review the detail errors for potential fixes. For guidance, visit https://aka.ms/azsdk/sdk-automation-faq ."
Write-Host "[ERROR] If the issue persists, contact the DotNet language support channel at $DotNetSupportChannelLink and include this spec pull request."
}
else {
$artifacts += Get-ChildItem $artifactsPath -Filter *.nupkg -exclude *.symbols.nupkg -Recurse | Select-Object -ExpandProperty FullName | Resolve-Path -Relative
}
}
$apiViewArtifact = ""
if ( $artifacts.count -le 0) {
Write-Error "Failed to generate sdk artifact"
if ( $artifacts.count -eq 0) {
Write-Host "[ERROR] Failed to generate sdk artifact. Please review the detail errors for potential fixes. For guidance, visit https://aka.ms/azsdk/sdk-automation-faq ."
Write-Host "[ERROR] If the issue persists after re-running, contact the DotNet language support channel at $DotNetSupportChannelLink and include this spec pull request."
} else {
$apiViewArtifact = $artifacts[0]
}
Expand All @@ -811,7 +841,8 @@ function GeneratePackage()
Write-Host "Start to export api for $service"
& $sdkRootPath/eng/scripts/Export-API.ps1 $service
if ( !$? ) {
Write-Error "Failed to export api for sdk. exit code: $?"
Write-Host "[ERROR] Failed to export api for sdk. exit code: $?. Please review the detail errors for potential fixes. For guidance, visit https://aka.ms/azsdk/sdk-automation-faq ."
Write-Host "[ERROR] If the issue persists after re-running, contact the DotNet language support channel at $DotNetSupportChannelLink and include this spec pull request."
$result = "failed"
}
# breaking change validation
Expand Down Expand Up @@ -858,7 +889,7 @@ function GeneratePackage()
changelog=$changelog
}

if ($installInstructions -ne $null) {
if ($null -ne $installInstructions) {
$packageDetails['installInstructions'] = $installInstructions
}
$generatedSDKPackages.Add($packageDetails)
Expand Down Expand Up @@ -933,7 +964,7 @@ function GetSDKProjectFolder()
}
}
if (!$service || !$packageDir) {
throw "Not provide service name or packageDir."
throw "[ERROR] 'serviceDir' or 'packageDir' not provided. Please configure these settings in the 'tspconfig.yaml' file."
}
$projectFolder = (Join-Path $sdkRepoRoot $service $packageDir)
return $projectFolder
Expand Down
10 changes: 9 additions & 1 deletion eng/swagger_to_sdk_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@
"$schema": "https://raw.githubusercontent.com/Azure/azure-rest-api-specs/master/documentation/sdkautomation/SwaggerToSdkConfigSchema.json",
"generateOptions": {
"generateScript": {
"path": "pwsh ./eng/scripts/Invoke-GenerateAndBuildV2.ps1"
"path": "pwsh ./eng/scripts/Invoke-GenerateAndBuildV2.ps1",
"logPrefix": "DotNet-Sdk-Automation",
"stderr": {
"showInComment": ".*(\\[ERROR\\]|error NU|(?!0\\b)\\d+ Error\\(s\\)|ExternalError).*",
"scriptError": "\\[ERROR\\]"
},
"stdout": {
"showInComment": "(Start to|\\[ERROR\\]).*"
}
}
},
"initOptions": {
Expand Down

0 comments on commit 78f27f2

Please sign in to comment.