@@ -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+ }
0 commit comments