File: ApiView-Helpers.ps1

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (308 lines) | stat: -rw-r--r-- 11,346 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
. ${PSScriptRoot}\..\logging.ps1

function MapLanguageToRequestParam($language)
{
    $lang = $language
    # Update language name to match those in API cosmos DB. Cosmos SQL is case sensitive and handling this within the query makes it slow.
    if($lang -eq 'javascript'){
        $lang = "JavaScript"
    }
    elseif ($lang -eq "dotnet"){
        $lang = "C%23"
    }
    elseif ($lang -eq "java"){
        $lang = "Java"
    }
    elseif ($lang -eq "python"){
        $lang = "Python"
    }
    else{
      $lang = $null
    }
    return $lang
}

function Check-ApiReviewStatus($packageName, $packageVersion, $language, $url, $apiKey, $apiApprovalStatus = $null, $packageNameStatus = $null)
{
  # Get API view URL and API Key to check status
  Write-Host "Checking API review status for package: ${packageName}"
  $lang = MapLanguageToRequestParam -language $language
  if ($lang -eq $null) {
    return
  }
  $headers = @{ "ApiKey" = $apiKey }

  if (!$apiApprovalStatus) {
    $apiApprovalStatus = [PSCustomObject]@{
      IsApproved = $false
      Details = ""
    }
  }

  if (!$packageNameStatus) {
    $packageNameStatus = [PSCustomObject]@{
      IsApproved = $false
      Details = ""
    }
  }

  try
  {
    $requestUrl = "${url}?language=${lang}&packageName=${packageName}&packageVersion=${packageVersion}"
    Write-Host "Request to APIView: [${requestUrl}]"
    $response = Invoke-WebRequest $requestUrl -Method 'GET' -Headers $headers
    Write-Host "Response: $($response.StatusCode)"
    Process-ReviewStatusCode -statusCode $response.StatusCode -packageName $packageName -apiApprovalStatus $apiApprovalStatus -packageNameStatus $packageNameStatus
    if ($apiApprovalStatus.IsApproved) {
      Write-Host $($apiApprovalStatus.Details)
    }
    else {
      Write-warning $($apiApprovalStatus.Details)
    }
    if ($packageNameStatus.IsApproved) {
      Write-Host $($packageNameStatus.Details)
    }
    else {
      Write-warning $($packageNameStatus.Details)
    }
  }
  catch
  {
    Write-Warning "Failed to check API review status for package $($PackageName). You can check http://aka.ms/azsdk/engsys/apireview/faq for more details on API Approval."
  }
}

function Process-ReviewStatusCode($statusCode, $packageName, $apiApprovalStatus, $packageNameStatus)
{
  $apiApproved = $false
  $apiApprovalDetails = "API Review is not approved for package $($packageName). Release pipeline will fail if API review is not approved for a GA version release. You can check http://aka.ms/azsdk/engsys/apireview/faq for more details on API Approval."
  $apiApprovalDetails += " Once your API is approved, re-trigger the release pipeline again."

  $packageNameApproved = $false
  $packageNameApprovalDetails = ""

  # 200 API approved and Package name approved
  # 201 API review is not approved, Package name is approved
  # 202 API review is not approved, Package name is not approved

  switch ($statusCode)
  {
    200
    {
      $apiApprovalDetails = "API Review is approved for package $($packageName)"
      $apiApproved = $true

      $packageNameApproved = $true
      $packageNameApprovalDetails = "Package name is approved for package $($packageName)"
    }
    201
    {
      $packageNameApproved = $true
      $packageNameApprovalDetails = "Package name is approved for package $($packageName)"
    }
    202
    {
      $packageNameApprovalDetails = "Package name $($packageName) is not yet approved by an SDK API approver. Package name must be approved to release a beta version if $($packageName) was never released as a stable version."
      $packageNameApprovalDetails += " You can check http://aka.ms/azsdk/engsys/apireview/faq for more details on package name Approval."
    }
    default
    {
      $apiApprovalDetails = "Invalid status code from APIView. status code $($statusCode)"
      $packageNameApprovalDetails = "Invalid status code from APIView. status code $($statusCode)"
      Write-Error "Failed to process API Review status for for package $($PackageName). Please reach out to Azure SDK engineering systems on teams channel."
    }
  }

  $apiApprovalStatus.IsApproved = $apiApproved
  $apiApprovalStatus.Details = $apiApprovalDetails

  $packageNameStatus.IsApproved = $packageNameApproved
  $packageNameStatus.Details = $packageNameApprovalDetails
}

function Set-ApiViewCommentForRelatedIssues {
  param (
    [Parameter(Mandatory = $true)]
    [string]$HeadCommitish,
    [string]$APIViewHost = "https://apiview.dev",
    [ValidateNotNullOrEmpty()]
    [Parameter(Mandatory = $true)]
    $AuthToken
  )
  . ${PSScriptRoot}\..\common.ps1
  $issuesForCommit = $null
  try {
    $issuesForCommit = Search-GitHubIssues -CommitHash $HeadCommitish
    if ($issuesForCommit.items.Count -eq 0) {
      LogInfo "No issues found for commit: $HeadCommitish"
      Write-Host "##vso[task.complete result=SucceededWithIssues;]DONE"
      exit 0
    }
  } catch {
    LogError "No issues found for commit: $HeadCommitish"
    exit 1
  }
  $issuesForCommit.items | ForEach-Object {
    $urlParts = $_.url -split "/"
    Set-ApiViewCommentForPR -RepoOwner $urlParts[4] -RepoName $urlParts[5] -PrNumber $urlParts[7] -HeadCommitish $HeadCommitish -APIViewHost $APIViewHost -AuthToken $AuthToken
  }
}

function Set-ApiViewCommentForPR {
  param (
    [Parameter(Mandatory = $true)]
    [string]$RepoOwner,
    [Parameter(Mandatory = $true)]
    [string]$RepoName,
    [Parameter(Mandatory = $true)]
    [string]$PrNumber,
    [Parameter(Mandatory = $true)]
    [string]$HeadCommitish,
    [Parameter(Mandatory = $true)]
    [string]$APIViewHost,
    [ValidateNotNullOrEmpty()]
    [Parameter(Mandatory = $true)]
    $AuthToken
  )
  $repoFullName = "$RepoOwner/$RepoName"
  $apiviewEndpoint = "$APIViewHost/api/pullrequests?pullRequestNumber=$PrNumber&repoName=$repoFullName&commitSHA=$HeadCommitish"
  LogDebug "Get APIView information for PR using endpoint: $apiviewEndpoint"

  $correlationId = [System.Guid]::NewGuid().ToString()
  $headers = @{
    "x-correlation-id" = $correlationId
  }
  LogInfo "Correlation ID: $correlationId"

  $commentText = @()
  $commentText += "## API Change Check"
  try {
    $response = Invoke-WebRequest -Uri $apiviewEndpoint -Method Get -Headers $headers -MaximumRetryCount 3
    LogInfo "OperationId: $($response.Headers['X-Operation-Id'])"
    if ($response.StatusCode -ne 200) {
      LogInfo "API changes are not detected in this pull request."
      exit 0
    }
    else {
      LogSuccess "APIView identified API level changes in this PR and created $($response.Count) API reviews"
      $commentText += ""
      $commentText += "APIView identified API level changes in this PR and created the following API reviews"
      $commentText += ""

      $responseContent = $response.Content | ConvertFrom-Json
      if ($RepoName.StartsWith(("azure-sdk-for-"))) {
        $responseContent | ForEach-Object {
          $commentText += "[$($_.packageName)]($($_.url))"
        }
      } else {
        $commentText += "| Language | API Review for Package |"
        $commentText += "|----------|---------|"
        $responseContent | ForEach-Object {
          $commentText += "| $($_.language) | [$($_.packageName)]($($_.url)) |"
        }
      }
    }
  } catch{
    LogError "Failed to get API View information for PR: $PrNumber in repo: $repoFullName with commitSHA: $HeadCommitish. Error: $_"
    exit 1
  }

  $commentText += "<!-- Fetch URI: $apiviewEndpoint -->"
  $commentText = $commentText -join "`r`n"
  $existingComment = $null;
  $existingAPIViewComment = $null;

  try {
    $existingComment = Get-GitHubIssueComments -RepoOwner $RepoOwner -RepoName $RepoName -IssueNumber $PrNumber -AuthToken $AuthToken
    $existingAPIViewComment = $existingComment | Where-Object {
      $_.body.StartsWith("**API Change Check**", [StringComparison]::OrdinalIgnoreCase) -or $_.body.StartsWith("## API Change Check", [StringComparison]::OrdinalIgnoreCase) }
  } catch {
    LogWarning "Failed to get comments from Pull Request: $PrNumber in repo: $repoFullName"
  }

  try {
    if ($existingAPIViewComment) {
      LogDebug "Updating existing APIView comment..."
      Update-GitHubIssueComment -RepoOwner $RepoOwner -RepoName $RepoName `
                                -CommentId $existingAPIViewComment.id -Comment $commentText `
                                -AuthToken $AuthToken
    } else {
      LogDebug "Creating new APIView comment..."
      Add-GitHubIssueComment -RepoOwner $RepoOwner -RepoName $RepoName `
                             -IssueNumber $PrNumber -Comment $commentText `
                             -AuthToken $AuthToken
    }
  } catch {
    LogError "Failed to set PR comment for APIView. Error: $_"
    exit 1
  }
}

# Helper function used to create API review requests for Spec generation SDKs pipelines
function Create-API-Review {
  param (
    [string]$apiviewEndpoint = "https://apiview.dev/api/PullRequests/CreateAPIRevisionIfAPIHasChanges",
    [string]$specGenSDKArtifactPath,
    [string]$apiviewArtifactName,
    [string]$buildId,
    [string]$commitish,
    [string]$repoName,
    [string]$pullRequestNumber
  )
  $specGenSDKContent = Get-Content -Path $SpecGenSDKArtifactPath -Raw | ConvertFrom-Json
  $language = ($specGenSDKContent.language -split "-")[-1]

  foreach ($requestData in $specGenSDKContent.apiViewRequestData) {
    $requestUri = [System.UriBuilder]$apiviewEndpoint
    $requestParam = [System.Web.HttpUtility]::ParseQueryString('')
    $requestParam.Add('artifactName', $apiviewArtifactName)
    $requestParam.Add('buildId', $buildId)
    $requestParam.Add('commitSha', $commitish)
    $requestParam.Add('repoName', $repoName)
    $requestParam.Add('pullRequestNumber', $pullRequestNumber)
    $requestParam.Add('packageName', $requestData.packageName)
    $requestParam.Add('filePath', $requestData.filePath)
    if ($language -ieq "python") {
      $requestParam.Add('codeFile', (Split-Path -Path $requestData.filePath -Leaf))
    }
    $requestParam.Add('language', $language)
    $requestUri.query = $requestParam.toString()
    $correlationId = [System.Guid]::NewGuid().ToString()

    $headers = @{
      "x-correlation-id" = $correlationId
    }

    LogInfo "Request URI: $($requestUri.Uri.OriginalString)"
    LogInfo "Correlation ID: $correlationId"

    try
    {
      $response = Invoke-WebRequest -Method 'GET' -Uri $requestUri.Uri -Headers $headers -MaximumRetryCount 3
      if ($response.StatusCode -eq 201 -or $response.StatusCode -eq 208) {
        if ($response.StatusCode -eq 201) {
          LogSuccess "Status Code: $($response.StatusCode)`nAPI review request created successfully"
        }
        elseif ($response.StatusCode -eq 208) {
          LogSuccess "Status Code: $($response.StatusCode)`nThere is no API change compared with the previous version."
        }
        if ($response.Headers['Content-Type'] -like 'application/json*') {
          $responseContent = $response.Content | ConvertFrom-Json | ConvertTo-Json -Depth 10
          LogSuccess "Response:`n$($responseContent)"
        }
        else {
          LogSuccess "Response: $($response.Content)"
        }
      }
      else {
        LogError "Failed to create API review request. $($response)"
        exit 1
      }
    }
    catch
    {
      LogError "Error : $($_.Exception)"
      exit 1
    }
  }
}