Sync eng/common directory with azure-sdk-tools for PR 10004 (#6490)

* 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 <chononiw@microsoft.com>
This commit is contained in:
Azure SDK Bot 2025-03-27 17:23:28 -07:00 committed by GitHub
parent 5652d3aecc
commit 9ed08c9750
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 168 additions and 1 deletions

View File

@ -117,3 +117,106 @@ function Process-ReviewStatusCode($statusCode, $packageName, $apiApprovalStatus,
$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) {
LogError "No issues found for commit: $HeadCommitish"
exit 1
}
} 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"
$commentText = @()
$commentText += "## API Change Check"
try {
$response = Invoke-RestMethod -Uri $apiviewEndpoint -Method Get -MaximumRetryCount 3
if ($response.Count -eq 0) {
LogWarning "API changes are not detected in this pull request."
$commentText += ""
$commentText += "API changes are not detected in this pull request."
}
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 += ""
$commentText += "| Language | API Review for Package |"
$commentText += "|----------|---------|"
$response | ForEach-Object {
$commentText += "| $($_.language) | [$($_.packageName)]($($_.url)) |"
}
}
} catch{
LogError "Failed to get API View information for PR: $PrNumber in repo: $repoFullName with commitSHA: $Commitish. 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
}
}

View File

@ -228,7 +228,6 @@ function Get-GitHubIssues {
-Headers (Get-GitHubApiHeaders -token $AuthToken) `
-MaximumRetryCount 3
}
function Add-GitHubIssueComment {
param (
[Parameter(Mandatory = $true)]
@ -258,6 +257,56 @@ function Add-GitHubIssueComment {
-MaximumRetryCount 3
}
function Get-GitHubIssueComments {
param (
[Parameter(Mandatory = $true)]
$RepoOwner,
[Parameter(Mandatory = $true)]
$RepoName,
[Parameter(Mandatory = $true)]
$IssueNumber,
[ValidateNotNullOrEmpty()]
[Parameter(Mandatory = $true)]
$AuthToken
)
$uri = "$GithubAPIBaseURI/$RepoOwner/$RepoName/issues/$IssueNumber/comments"
return Invoke-RestMethod `
-Method GET `
-Uri $uri `
-Headers (Get-GitHubApiHeaders -token $AuthToken) `
-MaximumRetryCount 3
}
function Update-GitHubIssueComment {
param (
[Parameter(Mandatory = $true)]
$RepoOwner,
[Parameter(Mandatory = $true)]
$RepoName,
[Parameter(Mandatory = $true)]
[String]$CommentId,
[Parameter(Mandatory = $true)]
$Comment,
[ValidateNotNullOrEmpty()]
[Parameter(Mandatory = $true)]
$AuthToken
)
$uri = "$GithubAPIBaseURI/$RepoOwner/$RepoName/issues/comments/$CommentId"
$parameters = @{
body = $Comment
}
return Invoke-RestMethod `
-Method PATCH `
-Body ($parameters | ConvertTo-Json) `
-Uri $uri `
-Headers (Get-GitHubApiHeaders -token $AuthToken) `
-MaximumRetryCount 3
}
# Will delete label from the issue if it exists
function Remove-GitHubIssueLabel {
param (
@ -505,3 +554,18 @@ function Search-GitHubCommit {
-Headers (Get-GitHubApiHeaders -token $AuthToken) `
-MaximumRetryCount 3
}
function Search-GitHubIssues {
param (
[ValidateNotNullOrEmpty()]
[Parameter(Mandatory = $true)]
$CommitHash,
$State="open"
)
$uri = "https://api.github.com/search/issues?q=sha:$CommitHash+state:$State"
return Invoke-RestMethod `
-Method GET `
-Uri $uri `
-MaximumRetryCount 3
}