Skip to content

Commit 9c9a720

Browse files
Sync eng/common directory with azure-sdk-tools for PR 10004 (Azure#2404)
* Add logic for seting APIView Pull Request comment * Update script to use source and head commish * Make API Change Check a header * Add Fetch URI to APIView comment * Pass APIView host as a parameter * Update APIVBiew comment header and add state to search api call --------- Co-authored-by: Chidozie Ononiwu <[email protected]>
1 parent 765da71 commit 9c9a720

File tree

2 files changed

+168
-1
lines changed

2 files changed

+168
-1
lines changed

eng/common/scripts/Helpers/ApiView-Helpers.ps1

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,106 @@ function Process-ReviewStatusCode($statusCode, $packageName, $apiApprovalStatus,
117117
$packageNameStatus.IsApproved = $packageNameApproved
118118
$packageNameStatus.Details = $packageNameApprovalDetails
119119
}
120+
121+
function Set-ApiViewCommentForRelatedIssues {
122+
param (
123+
[Parameter(Mandatory = $true)]
124+
[string]$HeadCommitish,
125+
[string]$APIViewHost = "https://apiview.dev",
126+
[ValidateNotNullOrEmpty()]
127+
[Parameter(Mandatory = $true)]
128+
$AuthToken
129+
)
130+
. ${PSScriptRoot}\..\common.ps1
131+
$issuesForCommit = $null
132+
try {
133+
$issuesForCommit = Search-GitHubIssues -CommitHash $HeadCommitish
134+
if ($issuesForCommit.items.Count -eq 0) {
135+
LogError "No issues found for commit: $HeadCommitish"
136+
exit 1
137+
}
138+
} catch {
139+
LogError "No issues found for commit: $HeadCommitish"
140+
exit 1
141+
}
142+
$issuesForCommit.items | ForEach-Object {
143+
$urlParts = $_.url -split "/"
144+
Set-ApiViewCommentForPR -RepoOwner $urlParts[4] -RepoName $urlParts[5] -PrNumber $urlParts[7] -HeadCommitish $HeadCommitish -APIViewHost $APIViewHost -AuthToken $AuthToken
145+
}
146+
}
147+
148+
function Set-ApiViewCommentForPR {
149+
param (
150+
[Parameter(Mandatory = $true)]
151+
[string]$RepoOwner,
152+
[Parameter(Mandatory = $true)]
153+
[string]$RepoName,
154+
[Parameter(Mandatory = $true)]
155+
[string]$PrNumber,
156+
[Parameter(Mandatory = $true)]
157+
[string]$HeadCommitish,
158+
[Parameter(Mandatory = $true)]
159+
[string]$APIViewHost,
160+
[ValidateNotNullOrEmpty()]
161+
[Parameter(Mandatory = $true)]
162+
$AuthToken
163+
)
164+
$repoFullName = "$RepoOwner/$RepoName"
165+
$apiviewEndpoint = "$APIViewHost/api/pullrequests?pullRequestNumber=$PrNumber&repoName=$repoFullName&commitSHA=$HeadCommitish"
166+
LogDebug "Get APIView information for PR using endpoint: $apiviewEndpoint"
167+
168+
$commentText = @()
169+
$commentText += "## API Change Check"
170+
try {
171+
$response = Invoke-RestMethod -Uri $apiviewEndpoint -Method Get -MaximumRetryCount 3
172+
if ($response.Count -eq 0) {
173+
LogWarning "API changes are not detected in this pull request."
174+
$commentText += ""
175+
$commentText += "API changes are not detected in this pull request."
176+
}
177+
else {
178+
LogSuccess "APIView identified API level changes in this PR and created $($response.Count) API reviews"
179+
$commentText += ""
180+
$commentText += "APIView identified API level changes in this PR and created the following API reviews"
181+
$commentText += ""
182+
$commentText += "| Language | API Review for Package |"
183+
$commentText += "|----------|---------|"
184+
$response | ForEach-Object {
185+
$commentText += "| $($_.language) | [$($_.packageName)]($($_.url)) |"
186+
}
187+
}
188+
} catch{
189+
LogError "Failed to get API View information for PR: $PrNumber in repo: $repoFullName with commitSHA: $Commitish. Error: $_"
190+
exit 1
191+
}
192+
193+
$commentText += "<!-- Fetch URI: $apiviewEndpoint -->"
194+
$commentText = $commentText -join "`r`n"
195+
$existingComment = $null;
196+
$existingAPIViewComment = $null;
197+
198+
try {
199+
$existingComment = Get-GitHubIssueComments -RepoOwner $RepoOwner -RepoName $RepoName -IssueNumber $PrNumber -AuthToken $AuthToken
200+
$existingAPIViewComment = $existingComment | Where-Object {
201+
$_.body.StartsWith("**API Change Check**", [StringComparison]::OrdinalIgnoreCase) -or $_.body.StartsWith("## API Change Check", [StringComparison]::OrdinalIgnoreCase) }
202+
} catch {
203+
LogWarning "Failed to get comments from Pull Request: $PrNumber in repo: $repoFullName"
204+
}
205+
206+
try {
207+
if ($existingAPIViewComment) {
208+
LogDebug "Updating existing APIView comment..."
209+
Update-GitHubIssueComment -RepoOwner $RepoOwner -RepoName $RepoName `
210+
-CommentId $existingAPIViewComment.id -Comment $commentText `
211+
-AuthToken $AuthToken
212+
} else {
213+
LogDebug "Creating new APIView comment..."
214+
Add-GitHubIssueComment -RepoOwner $RepoOwner -RepoName $RepoName `
215+
-IssueNumber $PrNumber -Comment $commentText `
216+
-AuthToken $AuthToken
217+
}
218+
} catch {
219+
LogError "Failed to set PR comment for APIView. Error: $_"
220+
exit 1
221+
}
222+
}

eng/common/scripts/Invoke-GitHubAPI.ps1

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,6 @@ function Get-GitHubIssues {
228228
-Headers (Get-GitHubApiHeaders -token $AuthToken) `
229229
-MaximumRetryCount 3
230230
}
231-
232231
function Add-GitHubIssueComment {
233232
param (
234233
[Parameter(Mandatory = $true)]
@@ -258,6 +257,56 @@ function Add-GitHubIssueComment {
258257
-MaximumRetryCount 3
259258
}
260259

260+
function Get-GitHubIssueComments {
261+
param (
262+
[Parameter(Mandatory = $true)]
263+
$RepoOwner,
264+
[Parameter(Mandatory = $true)]
265+
$RepoName,
266+
[Parameter(Mandatory = $true)]
267+
$IssueNumber,
268+
[ValidateNotNullOrEmpty()]
269+
[Parameter(Mandatory = $true)]
270+
$AuthToken
271+
272+
)
273+
$uri = "$GithubAPIBaseURI/$RepoOwner/$RepoName/issues/$IssueNumber/comments"
274+
return Invoke-RestMethod `
275+
-Method GET `
276+
-Uri $uri `
277+
-Headers (Get-GitHubApiHeaders -token $AuthToken) `
278+
-MaximumRetryCount 3
279+
}
280+
281+
function Update-GitHubIssueComment {
282+
param (
283+
[Parameter(Mandatory = $true)]
284+
$RepoOwner,
285+
[Parameter(Mandatory = $true)]
286+
$RepoName,
287+
[Parameter(Mandatory = $true)]
288+
[String]$CommentId,
289+
[Parameter(Mandatory = $true)]
290+
$Comment,
291+
[ValidateNotNullOrEmpty()]
292+
[Parameter(Mandatory = $true)]
293+
$AuthToken
294+
295+
)
296+
$uri = "$GithubAPIBaseURI/$RepoOwner/$RepoName/issues/comments/$CommentId"
297+
298+
$parameters = @{
299+
body = $Comment
300+
}
301+
302+
return Invoke-RestMethod `
303+
-Method PATCH `
304+
-Body ($parameters | ConvertTo-Json) `
305+
-Uri $uri `
306+
-Headers (Get-GitHubApiHeaders -token $AuthToken) `
307+
-MaximumRetryCount 3
308+
}
309+
261310
# Will delete label from the issue if it exists
262311
function Remove-GitHubIssueLabel {
263312
param (
@@ -505,3 +554,18 @@ function Search-GitHubCommit {
505554
-Headers (Get-GitHubApiHeaders -token $AuthToken) `
506555
-MaximumRetryCount 3
507556
}
557+
558+
function Search-GitHubIssues {
559+
param (
560+
[ValidateNotNullOrEmpty()]
561+
[Parameter(Mandatory = $true)]
562+
$CommitHash,
563+
$State="open"
564+
)
565+
$uri = "https://api.github.com/search/issues?q=sha:$CommitHash+state:$State"
566+
567+
return Invoke-RestMethod `
568+
-Method GET `
569+
-Uri $uri `
570+
-MaximumRetryCount 3
571+
}

0 commit comments

Comments
 (0)