...

Text file src/github.com/Azure/azure-sdk-for-go/eng/common/scripts/Create-APIReview.ps1

Documentation: github.com/Azure/azure-sdk-for-go/eng/common/scripts

     1[CmdletBinding()]
     2Param (
     3  [Parameter(Mandatory=$True)]
     4  [string] $ArtifactPath,
     5  [Parameter(Mandatory=$True)]
     6  [string] $APIViewUri,
     7  [Parameter(Mandatory=$True)]
     8  [string] $APIKey,
     9  [Parameter(Mandatory=$True)]
    10  [string] $APILabel,
    11  [string] $PackageName,
    12  [string] $SourceBranch,
    13  [string] $DefaultBranch,
    14  [string] $ConfigFileDir = ""
    15)
    16
    17# Submit API review request and return status whether current revision is approved or pending or failed to create review
    18function Submit-APIReview($packagename, $filePath, $uri, $apiKey, $apiLabel, $releaseStatus)
    19{
    20    $multipartContent = [System.Net.Http.MultipartFormDataContent]::new()
    21    $FileStream = [System.IO.FileStream]::new($filePath, [System.IO.FileMode]::Open)
    22    $fileHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
    23    $fileHeader.Name = "file"
    24    $fileHeader.FileName = $packagename
    25    $fileContent = [System.Net.Http.StreamContent]::new($FileStream)
    26    $fileContent.Headers.ContentDisposition = $fileHeader
    27    $fileContent.Headers.ContentType = [System.Net.Http.Headers.MediaTypeHeaderValue]::Parse("application/octet-stream")
    28    $multipartContent.Add($fileContent)
    29
    30
    31    $stringHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
    32    $stringHeader.Name = "label"
    33    $StringContent = [System.Net.Http.StringContent]::new($apiLabel)
    34    $StringContent.Headers.ContentDisposition = $stringHeader
    35    $multipartContent.Add($stringContent)
    36    Write-Host "Request param, label: $apiLabel"
    37
    38    if ($releaseStatus -and ($releaseStatus -ne "Unreleased"))
    39    {
    40        $compareAllParam = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
    41        $compareAllParam.Name = "compareAllRevisions"
    42        $compareAllParamContent = [System.Net.Http.StringContent]::new($true)
    43        $compareAllParamContent.Headers.ContentDisposition = $compareAllParam
    44        $multipartContent.Add($compareAllParamContent)
    45        Write-Host "Request param, compareAllRevisions: true"
    46    }
    47
    48    $headers = @{
    49        "ApiKey" = $apiKey;
    50        "content-type" = "multipart/form-data"
    51    }
    52
    53    try
    54    {
    55        $Response = Invoke-WebRequest -Method 'POST' -Uri $uri -Body $multipartContent -Headers $headers
    56        Write-Host "API Review URL: $($Response.Content)"
    57        $StatusCode = $Response.StatusCode
    58    }
    59    catch
    60    {
    61        Write-Host "Exception details: $($_.Exception.Response)"
    62        $StatusCode = $_.Exception.Response.StatusCode
    63    }
    64
    65    return $StatusCode
    66}
    67
    68
    69. (Join-Path $PSScriptRoot common.ps1)
    70
    71Write-Host "Artifact path: $($ArtifactPath)"
    72Write-Host "Package Name: $($PackageName)"
    73Write-Host "Source branch: $($SourceBranch)"
    74Write-Host "Config File directory: $($ConfigFileDir)"
    75
    76$packages = @{}
    77if ($FindArtifactForApiReviewFn -and (Test-Path "Function:$FindArtifactForApiReviewFn"))
    78{
    79    $packages = &$FindArtifactForApiReviewFn $ArtifactPath $PackageName
    80}
    81else
    82{
    83    Write-Host "The function for 'FindArtifactForApiReviewFn' was not found.`
    84    Make sure it is present in eng/scripts/Language-Settings.ps1 and referenced in eng/common/scripts/common.ps1.`
    85    See https://github.com/Azure/azure-sdk-tools/blob/main/doc/common/common_engsys.md#code-structure"
    86    exit(1)
    87}
    88
    89# Check if package config file is present. This file has package version, SDK type etc info.
    90if (-not $ConfigFileDir)
    91{
    92    $ConfigFileDir = Join-Path -Path $ArtifactPath "PackageInfo"
    93}
    94
    95if ($packages)
    96{
    97    foreach($pkgPath in $packages.Values)
    98    {
    99        $pkg = Split-Path -Leaf $pkgPath
   100        $pkgPropPath = Join-Path -Path $ConfigFileDir "$PackageName.json"
   101        if (-Not (Test-Path $pkgPropPath))
   102        {
   103            Write-Host " Package property file path $($pkgPropPath) is invalid."
   104            continue
   105        }
   106        # Get package info from json file created before updating version to daily dev
   107        $pkgInfo = Get-Content $pkgPropPath | ConvertFrom-Json
   108        $version = [AzureEngSemanticVersion]::ParseVersionString($pkgInfo.Version)
   109        if ($version -eq $null)
   110        {
   111            Write-Host "Version info is not available for package $PackageName, because version '$(pkgInfo.Version)' is invalid. Please check if the version follows Azure SDK package versioning guidelines."
   112            exit 1
   113        }
   114
   115        Write-Host "Version: $($version)"
   116        Write-Host "SDK Type: $($pkgInfo.SdkType)"
   117        Write-Host "Release Status: $($pkgInfo.ReleaseStatus)"
   118
   119        # Run create review step only if build is triggered from main branch or if version is GA.
   120        # This is to avoid invalidating review status by a build triggered from feature branch
   121        if ( ($SourceBranch -eq $DefaultBranch) -or (-not $version.IsPrerelease))
   122        {
   123            Write-Host "Submitting API Review for package $($pkg)"
   124            $respCode = Submit-APIReview -packagename $pkg -filePath $pkgPath -uri $APIViewUri -apiKey $APIKey -apiLabel $APILabel -releaseStatus $pkgInfo.ReleaseStatus
   125            Write-Host "HTTP Response code: $($respCode)"
   126            # HTTP status 200 means API is in approved status
   127            if ($respCode -eq '200')
   128            {
   129                Write-Host "API review is in approved status."
   130            }
   131            elseif ($version.IsPrerelease)
   132            {
   133                # Ignore API review status for prerelease version
   134                Write-Host "Package version is not GA. Ignoring API view approval status"
   135            }
   136            elseif (!$pkgInfo.ReleaseStatus -or $pkgInfo.ReleaseStatus -eq "Unreleased")
   137            {
   138                Write-Host "Release date is not set for current version in change log file for package. Ignoring API review approval status since package is not yet ready for release."
   139            }
   140            else
   141            {
   142                # Return error code if status code is 201 for new data plane package
   143                # Temporarily enable API review for spring SDK types. Ideally this should be done be using 'IsReviewRequired' method in language side
   144                # to override default check of SDK type client
   145                if (($pkgInfo.SdkType -eq "client" -or $pkgInfo.SdkType -eq "spring") -and $pkgInfo.IsNewSdk)
   146                {
   147                    if ($respCode -eq '201')
   148                    {
   149                        Write-Host "Package version $($version) is GA and automatic API Review is not yet approved for package $($PackageName)."
   150                        Write-Host "Build and release is not allowed for GA package without API review approval."
   151                        Write-Host "You will need to queue another build to proceed further after API review is approved"
   152                        Write-Host "You can check http://aka.ms/azsdk/engsys/apireview/faq for more details on API Approval."
   153                    }
   154                    else
   155                    {
   156                        Write-Host "Failed to create API Review for package $($PackageName). Please reach out to Azure SDK engineering systems on teams channel and share this build details."
   157                    }
   158                    exit 1
   159                }
   160                else
   161                {
   162                    Write-Host "API review is not approved for package $($PackageName), however it is not required for this package type so it can still be released without API review approval."
   163                }
   164            }
   165        }
   166        else
   167        {
   168            Write-Host "Build is triggered from $($SourceBranch) with prerelease version. Skipping API review status check."
   169        }
   170    }
   171}
   172else
   173{
   174    Write-Host "No package is found in artifact path to submit review request"
   175}

View as plain text