Skip to content

Commit 3d6d3f6

Browse files
authored
Add some error checking and validation
1 parent b3dcdbe commit 3d6d3f6

File tree

1 file changed

+69
-31
lines changed

1 file changed

+69
-31
lines changed

.github/workflows/Shared-StaleBranch.yml

Lines changed: 69 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ jobs:
116116
$WatchListCount = 0
117117
$DataLossCount = 0
118118
$DataLossBlockedCount = 0
119+
$RetrieveBranchDataErrorCount = 0
120+
$RetrieveBranchDataError = $False
119121
120122
# Block the script from running if Ahead by is greater than 0 when maximum branch age is less than 90. Doing this could result in loss of data that may still be
121123
# wanted. Do not change the values unless you accept the high risk of unwanted data loss.
@@ -177,67 +179,95 @@ jobs:
177179
178180
}
179181
180-
$CompareData = Invoke-RestMethod -Headers $GitHubHeaders -Uri "$CompareUrl$BranchName" -Method GET -ResponseHeadersVariable ResponseHeaders
182+
Try {
181183
182-
$BehindBy = $CompareData.behind_by
183-
$AheadBy = $CompareData.ahead_by
184+
$CompareData = Invoke-RestMethod -Headers $GitHubHeaders -Uri "$CompareUrl$BranchName" -Method GET -ResponseHeadersVariable ResponseHeaders -ErrorAction Stop
184185
185-
Write-Host " Ahead of $DefaultBranch by: $AheadBy `n Behind by: $BehindBy."
186+
$BehindBy = [int]$CompareData.behind_by
187+
$AheadBy = [int]$CompareData.ahead_by
186188
187-
If ($AheadBy -gt $MaxCommitsAhead) {
188-
189-
Write-Host " Skipped. Branch exceeds `"ahead by`" limit of $MaxCommitsAhead."
190-
191-
$ReportBranchList += ">>> Branch watch list <<< $BranchName exceeds maximum age but has outstanding commits that exceed maximum Ahead By limit. Branch protected: $ProtectedBranch. Ahead by: $AheadBy. Behind by $BehindBy. Days since last commit: $($($(Get-Date) - $LastCommitDate).Days)."
189+
} Catch {
190+
191+
Write-Host " ERROR: Failed to retrieve branch data. Skipping. URL: "$CompareUrl$BranchName" Error: $($_)"
192192
193-
$WatchListCount++
193+
$RetrieveBranchDataError = $True
194+
$RetrieveBranchDataErrorCount++
194195
195196
continue
196-
197+
197198
}
198199
199-
If ($AheadBy -eq 0) {
200-
201-
Write-Host " $ReportOnlyString Delete branch $BranchName"
200+
If (($CompareData) -and ($BehindBy -is [int]) -and ($AheadBy -is [int]) -and ($LastCommitDate -is [datetime])) {
202201
203-
If (!$ReportOnly) {
202+
Write-Host " Ahead of $DefaultBranch by: $AheadBy `n Behind by: $BehindBy."
204203
205-
Invoke-RestMethod -Headers $GitHubHeaders -Uri $BranchDeleteUrl -Method DELETE -ResponseHeadersVariable ResponseHeaders | Out-Null
204+
If ($AheadBy -gt $MaxCommitsAhead) {
206205
207-
}
206+
Write-Host " Skipped. Branch exceeds `"ahead by`" limit of $MaxCommitsAhead."
208207
209-
$ReportBranchList += "$ReportOnlyString $BranchName deleted. Branch protected: $ProtectedBranch. Ahead by: $AheadBy. Behind by $BehindBy. Days since last commit: $($($(Get-Date) - $LastCommitDate).Days). "
208+
$ReportBranchList += ">>> Branch watch list <<< $BranchName exceeds maximum age but has outstanding commits that exceed maximum Ahead By limit. Branch protected: $ProtectedBranch. Ahead by: $AheadBy. Behind by $BehindBy. Days since last commit: $($($(Get-Date) - $LastCommitDate).Days)."
210209
211-
$DeleteBranchCount++
210+
$WatchListCount++
212211
213-
} Else {
214-
215-
If ($AllowDataLoss) {
212+
continue
213+
214+
}
215+
216+
If ($AheadBy -eq 0) {
216217
217-
Write-Host " $ReportOnlyString Delete branch $BranchName with data loss"
218+
Write-Host " $ReportOnlyString Delete branch $BranchName"
218219
219220
If (!$ReportOnly) {
220221
221222
Invoke-RestMethod -Headers $GitHubHeaders -Uri $BranchDeleteUrl -Method DELETE -ResponseHeadersVariable ResponseHeaders | Out-Null
222223
223224
}
224225
225-
$ReportBranchList += "$ReportOnlyString !!! DATA LOSS !!! $BranchName deleted. Branch protected: $ProtectedBranch. Ahead by: $AheadBy. Behind by $BehindBy. Days since last commit: $($($(Get-Date) - $LastCommitDate).Days). "
226+
$ReportBranchList += "$ReportOnlyString $BranchName deleted. Branch protected: $ProtectedBranch. Ahead by: $AheadBy. Behind by $BehindBy. Days since last commit: $($($(Get-Date) - $LastCommitDate).Days). "
226227
227228
$DeleteBranchCount++
228-
$DataLossCount++
229-
229+
230230
} Else {
231231
232-
Write-Host " $ReportOnlyString Branch $BranchName was marked for deletion with data loss but data loss flag is disabled."
232+
If ($AllowDataLoss) {
233+
234+
Write-Host " $ReportOnlyString Delete branch $BranchName with data loss"
233235
234-
$ReportBranchList += "$ReportOnlyString *** DATA LOSS BLOCKED *** $BranchName was marked for deletion with data loss but the data loss flag is disabled. Branch protected: $ProtectedBranch. Ahead by: $AheadBy. Behind by $BehindBy. Days since last commit: $($($(Get-Date) - $LastCommitDate).Days)."
236+
If (!$ReportOnly) {
235237
236-
$DataLossBlockedCount++
238+
Invoke-RestMethod -Headers $GitHubHeaders -Uri $BranchDeleteUrl -Method DELETE -ResponseHeadersVariable ResponseHeaders | Out-Null
239+
240+
}
241+
242+
$ReportBranchList += "$ReportOnlyString !!! DATA LOSS !!! $BranchName deleted. Branch protected: $ProtectedBranch. Ahead by: $AheadBy. Behind by $BehindBy. Days since last commit: $($($(Get-Date) - $LastCommitDate).Days). "
243+
244+
$DeleteBranchCount++
245+
$DataLossCount++
246+
247+
} Else {
248+
249+
Write-Host " $ReportOnlyString Branch $BranchName was marked for deletion with data loss but data loss flag is disabled."
250+
251+
$ReportBranchList += "$ReportOnlyString *** DATA LOSS BLOCKED *** $BranchName was marked for deletion with data loss but the data loss flag is disabled. Branch protected: $ProtectedBranch. Ahead by: $AheadBy. Behind by $BehindBy. Days since last commit: $($($(Get-Date) - $LastCommitDate).Days)."
252+
253+
$DataLossBlockedCount++
254+
255+
} # AllowDataLoss If
237256
238-
} # AllowDataLoss If
257+
} # AheadBy If
258+
259+
} Else {
239260
240-
} # AheadBy If
261+
Write-Host " ERROR - One or more variables doesn't contain a valid data type. CompareData: $($CompareData). AheadBy: $($AheadBy -is [int]). BehindBy: $($BehindBy -is [int]). LastCommitDate: $($LastCommitDate -is [datetime]). Skipping."
262+
Write-Host " AheadBy: $AheadBy"
263+
Write-Host " BehindBy: $BehindBy"
264+
Write-Host " LastCommitDate: $LastCommitDate"
265+
Write-Host $($CompareData | ConvertTo-Json -Depth 1)
266+
267+
$RetrieveBranchDataError = $True
268+
$RetrieveBranchDataErrorCount++
269+
270+
} # Data retrieval check If
241271
242272
} # Branch loop
243273

@@ -274,4 +304,12 @@ jobs:
274304
Write-Host "==========="
275305
Write-Host "$ReportOnlyString Branches deleted with data loss: $DataLossCount"
276306
Write-Host "$ReportOnlyString Total deleted branches: $DeleteBranchCount"
307+
308+
# Forcing the workflow to fail at the end to bring attention to the fact that there was a failure to process
309+
# one or more branches.
310+
If ($RetrieveBranchDataError) {
311+
312+
Throw "Failed to retrieve data for $RetrieveBranchDataErrorCount branches."
313+
314+
}
277315

0 commit comments

Comments
 (0)