File: Create-APIReview.ps1

package info (click to toggle)
golang-github-azure-azure-sdk-for-go 68.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 556,256 kB
  • sloc: javascript: 196; sh: 96; makefile: 7
file content (175 lines) | stat: -rw-r--r-- 7,646 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
[CmdletBinding()]
Param (
  [Parameter(Mandatory=$True)]
  [string] $ArtifactPath,
  [Parameter(Mandatory=$True)]
  [string] $APIViewUri,
  [Parameter(Mandatory=$True)]
  [string] $APIKey,
  [Parameter(Mandatory=$True)]
  [string] $APILabel,
  [string] $PackageName,
  [string] $SourceBranch,
  [string] $DefaultBranch,
  [string] $ConfigFileDir = ""
)

# Submit API review request and return status whether current revision is approved or pending or failed to create review
function Submit-APIReview($packagename, $filePath, $uri, $apiKey, $apiLabel, $releaseStatus)
{
    $multipartContent = [System.Net.Http.MultipartFormDataContent]::new()
    $FileStream = [System.IO.FileStream]::new($filePath, [System.IO.FileMode]::Open)
    $fileHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
    $fileHeader.Name = "file"
    $fileHeader.FileName = $packagename
    $fileContent = [System.Net.Http.StreamContent]::new($FileStream)
    $fileContent.Headers.ContentDisposition = $fileHeader
    $fileContent.Headers.ContentType = [System.Net.Http.Headers.MediaTypeHeaderValue]::Parse("application/octet-stream")
    $multipartContent.Add($fileContent)


    $stringHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
    $stringHeader.Name = "label"
    $StringContent = [System.Net.Http.StringContent]::new($apiLabel)
    $StringContent.Headers.ContentDisposition = $stringHeader
    $multipartContent.Add($stringContent)
    Write-Host "Request param, label: $apiLabel"

    if ($releaseStatus -and ($releaseStatus -ne "Unreleased"))
    {
        $compareAllParam = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
        $compareAllParam.Name = "compareAllRevisions"
        $compareAllParamContent = [System.Net.Http.StringContent]::new($true)
        $compareAllParamContent.Headers.ContentDisposition = $compareAllParam
        $multipartContent.Add($compareAllParamContent)
        Write-Host "Request param, compareAllRevisions: true"
    }

    $headers = @{
        "ApiKey" = $apiKey;
        "content-type" = "multipart/form-data"
    }

    try
    {
        $Response = Invoke-WebRequest -Method 'POST' -Uri $uri -Body $multipartContent -Headers $headers
        Write-Host "API Review URL: $($Response.Content)"
        $StatusCode = $Response.StatusCode
    }
    catch
    {
        Write-Host "Exception details: $($_.Exception.Response)"
        $StatusCode = $_.Exception.Response.StatusCode
    }

    return $StatusCode
}


. (Join-Path $PSScriptRoot common.ps1)

Write-Host "Artifact path: $($ArtifactPath)"
Write-Host "Package Name: $($PackageName)"
Write-Host "Source branch: $($SourceBranch)"
Write-Host "Config File directory: $($ConfigFileDir)"

$packages = @{}
if ($FindArtifactForApiReviewFn -and (Test-Path "Function:$FindArtifactForApiReviewFn"))
{
    $packages = &$FindArtifactForApiReviewFn $ArtifactPath $PackageName
}
else
{
    Write-Host "The function for 'FindArtifactForApiReviewFn' was not found.`
    Make sure it is present in eng/scripts/Language-Settings.ps1 and referenced in eng/common/scripts/common.ps1.`
    See https://github.com/Azure/azure-sdk-tools/blob/main/doc/common/common_engsys.md#code-structure"
    exit(1)
}

# Check if package config file is present. This file has package version, SDK type etc info.
if (-not $ConfigFileDir)
{
    $ConfigFileDir = Join-Path -Path $ArtifactPath "PackageInfo"
}

if ($packages)
{
    foreach($pkgPath in $packages.Values)
    {
        $pkg = Split-Path -Leaf $pkgPath
        $pkgPropPath = Join-Path -Path $ConfigFileDir "$PackageName.json"
        if (-Not (Test-Path $pkgPropPath))
        {
            Write-Host " Package property file path $($pkgPropPath) is invalid."
            continue
        }
        # Get package info from json file created before updating version to daily dev
        $pkgInfo = Get-Content $pkgPropPath | ConvertFrom-Json
        $version = [AzureEngSemanticVersion]::ParseVersionString($pkgInfo.Version)
        if ($version -eq $null)
        {
            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."
            exit 1
        }

        Write-Host "Version: $($version)"
        Write-Host "SDK Type: $($pkgInfo.SdkType)"
        Write-Host "Release Status: $($pkgInfo.ReleaseStatus)"

        # Run create review step only if build is triggered from main branch or if version is GA.
        # This is to avoid invalidating review status by a build triggered from feature branch
        if ( ($SourceBranch -eq $DefaultBranch) -or (-not $version.IsPrerelease))
        {
            Write-Host "Submitting API Review for package $($pkg)"
            $respCode = Submit-APIReview -packagename $pkg -filePath $pkgPath -uri $APIViewUri -apiKey $APIKey -apiLabel $APILabel -releaseStatus $pkgInfo.ReleaseStatus
            Write-Host "HTTP Response code: $($respCode)"
            # HTTP status 200 means API is in approved status
            if ($respCode -eq '200')
            {
                Write-Host "API review is in approved status."
            }
            elseif ($version.IsPrerelease)
            {
                # Ignore API review status for prerelease version
                Write-Host "Package version is not GA. Ignoring API view approval status"
            }
            elseif (!$pkgInfo.ReleaseStatus -or $pkgInfo.ReleaseStatus -eq "Unreleased")
            {
                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."
            }
            else
            {
                # Return error code if status code is 201 for new data plane package
                # Temporarily enable API review for spring SDK types. Ideally this should be done be using 'IsReviewRequired' method in language side
                # to override default check of SDK type client
                if (($pkgInfo.SdkType -eq "client" -or $pkgInfo.SdkType -eq "spring") -and $pkgInfo.IsNewSdk)
                {
                    if ($respCode -eq '201')
                    {
                        Write-Host "Package version $($version) is GA and automatic API Review is not yet approved for package $($PackageName)."
                        Write-Host "Build and release is not allowed for GA package without API review approval."
                        Write-Host "You will need to queue another build to proceed further after API review is approved"
                        Write-Host "You can check http://aka.ms/azsdk/engsys/apireview/faq for more details on API Approval."
                    }
                    else
                    {
                        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."
                    }
                    exit 1
                }
                else
                {
                    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."
                }
            }
        }
        else
        {
            Write-Host "Build is triggered from $($SourceBranch) with prerelease version. Skipping API review status check."
        }
    }
}
else
{
    Write-Host "No package is found in artifact path to submit review request"
}