Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 6 commits into from
Mar 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions eng/common/scripts/Helpers/ApiView-Helpers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
66 changes: 65 additions & 1 deletion eng/common/scripts/Invoke-GitHubAPI.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,6 @@ function Get-GitHubIssues {
-Headers (Get-GitHubApiHeaders -token $AuthToken) `
-MaximumRetryCount 3
}

function Add-GitHubIssueComment {
param (
[Parameter(Mandatory = $true)]
Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -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
}