@@ -7,6 +7,7 @@ name: (Scheduled) Stale branch removal
77
88permissions :
99 contents : write
10+ pull-requests : read
1011
1112on :
1213 workflow_call :
@@ -105,7 +106,129 @@ jobs:
105106 Get-Date -Year $TargetYear -Month $TargetMonth -Day $TargetDay -Hour 0 -Minute 0 -Second 0
106107 }
107108
109+ Function Get-PrData {
108110
111+ [CmdletBinding()]
112+ Param(
113+ [Parameter(Mandatory)]
114+ $Branch
115+ )
116+
117+ $PrUrl = $($GitHubData.event.repository.pulls_url).Replace("{/number}", "?state=all&head=$($GitHubData.event.repository.owner.login):$Branch&base=$($GitHubData.event.repository.default_branch)")
118+
119+ Try {
120+
121+ Write-Host "PR URL: $PrUrl"
122+
123+ $PrData = Invoke-RestMethod -Uri $PrUrl -Method Get -Headers $GitHubHeaders -ContentType "application/json" -ErrorAction Stop
124+
125+ If ($PrData.count -gt 0) {
126+
127+ If ($PrData[0].state -eq "open") {
128+
129+ Write-Host "The branch $Branch has an open pull request. PR number: $($PrData[0].number)"
130+
131+
132+ $ReturnData = $PrData[0]
133+
134+ } Else {
135+
136+ Write-Host "The branch $Branch has $($PrData.count) closed pull requests. Last PR number: $($PrData[0].number)"
137+
138+ $ReturnData = $PrData[0]
139+
140+ }
141+
142+ } Else {
143+
144+ Write-Host "No pull requests found for the branch $Branch."
145+
146+ $ReturnData = $Null
147+ }
148+
149+ } Catch {
150+
151+ Write-Host "ERROR: An error occurred while fetching pull request data: $_"
152+
153+ $ReturnData = $Null
154+
155+ }
156+
157+ Return $ReturnData
158+
159+ }
160+
161+ Function Get-PrMarkdown {
162+
163+ [CmdletBinding()]
164+ Param(
165+ $PrData
166+ )
167+
168+ If ($PrData) {
169+
170+ $PrNumber = $PrData.number
171+ $PrHtmlUrl = $PrData.html_url
172+
173+ If ($PrData.state -eq "open") {
174+
175+ $PrIconText = "Open"
176+ $PrIconUrl = "https://learn.microsoft.com/en-us/Office/media/internal/git-pull-request-16.svg"
177+
178+ } Else {
179+
180+ If ($PrData.merged_at -eq $Null) {
181+
182+ $PrIconText = "Closed"
183+ $PrIconUrl = "https://learn.microsoft.com/en-us/Office/media/internal/git-pull-request-closed-16.svg"
184+
185+ } Else {
186+
187+ $PrIconText = "Merged"
188+ $PrIconUrl = "https://learn.microsoft.com/en-us/Office/media/internal/git-merge-16.svg"
189+
190+ }
191+
192+ }
193+
194+ $PrMarkdown = " [$PrNumber]($PrHtmlUrl)"
195+
196+ } Else {
197+
198+ $PrMarkdown = "-"
199+
200+ }
201+
202+ Return $PrMarkdown
203+
204+ }
205+
206+ Function Get-PrStaleStatus {
207+
208+ [CmdletBinding()]
209+ Param(
210+ $PrData
211+ )
212+
213+ $PrState = " "
214+ $Labels = $PrData.labels
215+
216+ If ($Labels.name -contains "Auto Closed") {
217+
218+ $PrState = "Auto Closed"
219+
220+ } Else {
221+
222+ If ($Labels.name -contains "Inactive") {
223+
224+ $PrState = "Inactive"
225+ }
226+
227+ }
228+
229+ Return $PrState
230+
231+ }
109232
110233 # Create the branch skip list that is a combination of the central workflow list and the branch skip list that can be populated in each individual repo.
111234 $SkipBranchList = $DefaultSkipBranchList + $RepoBranchSkipList | Select-Object -Unique
@@ -456,8 +579,8 @@ jobs:
456579 $DeleteTableHeaderRow1 = "| Branch name | Commits ahead by | Commits behind by | Days since last commit<br>(on $FriendlyDeletionDate) | Processing result |"
457580 $DeleteTableHeaderRow2 = "|-------------|:----------------:|:-----------------:|:----------------------------------------------------:|-------------------|"
458581
459- $WatchTableHeaderRow1 = "| Branch name | Branch creator | Commits ahead by | Commits behind by | Days since last commit<br>(on $FriendlyDeletionDate) | Processing result |"
460- $WatchTableHeaderRow2 = "|-------------|----------------|:---------------- :|:----------------- :|:----------------------------------------------------:| ------------------- |"
582+ $WatchTableHeaderRow1 = "| Branch name | Branch creator | Pull request | Commits ahead by | Commits behind by | Days since last commit<br>(on $FriendlyDeletionDate) |"
583+ $WatchTableHeaderRow2 = "|-------------|----------------|:------------:|:---------------:|:-----------------:|: ----------------------------------------------------: |"
461584
462585 # Set job summary and create the "Deleted stale branches" section.
463586 echo "# Stale branch results" >> $env:GITHUB_STEP_SUMMARY
@@ -586,13 +709,16 @@ jobs:
586709 $BB = $BranchReport.BehindBy
587710 $LC = $BranchReport.DaysSinceLastCommit
588711 $DD = $BranchReport.DaysSinceLastCommitOnDeleteDay
589- $PR = $BranchReport.ProcessingResult
590712 $BC = $BranchReport.BranchCreator
591713
714+ $PrData = Get-PrData -Branch $BN
715+ $PrMarkdown = Get-PrMarkdown -PrData $PrData
716+ $PrStaleState = Get-PrStaleStatus -PrData $PrData
717+
592718 $BranchDiffHtmlUrl = "$($GitHubData.event.repository.html_url)/compare/$DefaultBranch...$($BN)#files_bucket"
593719
594720 Write-Host "$PR` : Branch name: $BN. Branch creator: $BC. Ahead by: $AB. Behind by: $BB. Days since last commit: $LC. On $FriendlyDeletionDate`: ($DD)."
595- echo "| [$BN]($BranchDiffHtmlUrl) | [$BC]($GitHubBaseUrl/$BC) | $AB | $BB | $LC<br>($DD) | $PR |" >> $env:GITHUB_STEP_SUMMARY
721+ echo "| [$BN]($BranchDiffHtmlUrl) | [$BC]($GitHubBaseUrl/$BC) | $PrMarkdown<br>$PrStaleState | $ AB | $BB | $LC<br>($DD) |" >> $env:GITHUB_STEP_SUMMARY
596722
597723 }
598724
0 commit comments