Skip to content

Commit fb01329

Browse files
authored
Merge pull request #13812 from MicrosoftDocs/workflows-test
Add report/delete functionality
2 parents c8c6eba + 04878a1 commit fb01329

1 file changed

Lines changed: 90 additions & 71 deletions

File tree

.github/workflows/Shared-StaleBranch.yml

Lines changed: 90 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,37 @@ jobs:
7575
return $count
7676
}
7777
78+
Function Get-NextDeletionDate {
79+
80+
[CmdletBinding()]
81+
Param(
82+
[Parameter(Mandatory)]
83+
[ValidateRange(1,31)]
84+
[Int]$DeletionDayOfMonth
85+
)
86+
87+
$Today = Get-Date
88+
89+
# Use this month if today’s date is on or before the deletion day;
90+
# otherwise move to next month (AddMonths handles year rollover)
91+
If ($Today.Day -le $DeletionDayOfMonth) {
92+
$TargetMonth = $Today.Month
93+
$TargetYear = $Today.Year
94+
} Else {
95+
$Next = $Today.AddMonths(1)
96+
$TargetMonth = $Next.Month
97+
$TargetYear = $Next.Year
98+
}
99+
100+
# Clamp to last day of month if necessary
101+
$DaysInMonth = [DateTime]::DaysInMonth($TargetYear, $TargetMonth)
102+
$TargetDay = [Math]::Min($DeletionDayOfMonth, $DaysInMonth)
103+
104+
# Return midnight on the target day
105+
Get-Date -Year $TargetYear -Month $TargetMonth -Day $TargetDay -Hour 0 -Minute 0 -Second 0
106+
}
107+
108+
78109
79110
# 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.
80111
$SkipBranchList = $DefaultSkipBranchList + $RepoBranchSkipList | Select-Object -Unique
@@ -87,11 +118,12 @@ jobs:
87118
88119
$MaxDaysBehind = 90
89120
90-
$DateLimit = (Get-Date).AddDays(-$MaxDaysBehind)
91-
$ReportDate = Get-Date -Format "dddd MMMM dd, yyyy"
92-
$CurrentDay = (Get-Date).Day
93-
$RunMonth = (Get-Date).AddMonths((Get-Date).Day -ge $DeleteOnDayOfMonth).ToString('MMMM') # If current day is greater or equal to $DeleteOnDayOfMonth, flip to next month ($True = 1). If not, stay on current month ($False = 0).
94-
121+
$DeletionDate = Get-NextDeletionDate -DeletionDayOfMonth $DeleteOnDayOfMonth
122+
$DateLimit = $DeletionDate.AddDays(-$MaxDaysBehind)
123+
$ReportDate = Get-Date -Format "dddd MMMM d, yyyy"
124+
$CurrentDate = Get-Date
125+
$FriendlyDeletionDate = $DeletionDate.ToString('MMMM d')
126+
95127
# Create github HTTP authentication header
96128
$UserAgent = "officedocs"
97129
$GitHubHeaders = @{}
@@ -124,9 +156,9 @@ jobs:
124156
$RetrieveBranchDataErrorCount = 0
125157
$RetrieveBranchDataError = $False
126158
127-
# Workflow will only delete branches on the date of the month specified by $DeleteOnDayOfMonth. On all other days the workflow runs, it will only generate
159+
# Workflow will only delete branches on $DeletionDate. On all other days the workflow runs, it will only generate
128160
# a report of what would have been deleted on that date.
129-
If ($CurrentDay -eq $DeleteOnDayOfMonth) {
161+
If ($CurrentDate.Date -eq $DeletionDate.Date) {
130162
131163
$DeletionRun = $True
132164
@@ -167,13 +199,6 @@ jobs:
167199
168200
}
169201
170-
# Make sure $DeleteOnDayOfMonth is a valid month day. Not allowing above 28 so we don't have to deal with leap years or days with 30/31 days.
171-
If (($DeleteOnDayOfMonth -notin 1..28)) {
172-
173-
Throw "ERROR: DeleteOnDayOfMonth must be between 1 and 28."
174-
175-
}
176-
177202
ForEach ($Page in $Branches) {
178203
179204
ForEach ($Branch in $Page) {
@@ -328,7 +353,7 @@ jobs:
328353
# If the workflow is in reporting mode, don't delete the branch. If it isn't, delete it.
329354
If (!$ReportOnly) {
330355
331-
Invoke-RestMethod -Headers $GitHubHeaders -Uri $BranchDeleteUrl -Method DELETE -ResponseHeadersVariable ResponseHeaders | Out-Null
356+
#Invoke-RestMethod -Headers $GitHubHeaders -Uri $BranchDeleteUrl -Method DELETE -ResponseHeadersVariable ResponseHeaders | Out-Null
332357
333358
}
334359
@@ -360,7 +385,7 @@ jobs:
360385
# If the workflow is in reporting mode, don't delete the branch. If it isn't, delete it.
361386
If (!$ReportOnly) {
362387
363-
Invoke-RestMethod -Headers $GitHubHeaders -Uri $BranchDeleteUrl -Method DELETE -ResponseHeadersVariable ResponseHeaders | Out-Null
388+
#Invoke-RestMethod -Headers $GitHubHeaders -Uri $BranchDeleteUrl -Method DELETE -ResponseHeadersVariable ResponseHeaders | Out-Null
364389
365390
}
366391
@@ -451,7 +476,16 @@ jobs:
451476
$DeleteBranchListCount = $DeleteBranchList.Count
452477
$WatchBranchListCount = $WatchBranchList.Count
453478

454-
echo "## Deleted stale branches" >> $env:GITHUB_STEP_SUMMARY
479+
If ($DeletionRun) {
480+
481+
echo "## Deleted stale branches" >> $env:GITHUB_STEP_SUMMARY
482+
483+
} Else {
484+
485+
echo "## Stale branches pending deletion" >> $env:GITHUB_STEP_SUMMARY
486+
487+
}
488+
455489
echo "" >> $env:GITHUB_STEP_SUMMARY
456490

457491
If ($DeleteBranchlistCount -gt 0) {
@@ -470,8 +504,10 @@ jobs:
470504

471505
} Else {
472506

473-
echo "The following branches will be deleted on **$RunMonth $DeleteOnDayOfMonth** because they are over $MaxDaysBehind days behind the $DefaultBranch branch and contain $MaxCommitsAhead or fewer commits not in the $DefaultBranch branch." >> $env:GITHUB_STEP_SUMMARY
474-
echo "**If you don't want a branch to be deleted, merge $DefaultBranch into it before $RunMonth $DeleteOnDayOfMonth.**" >> $env:GITHUB_STEP_SUMMARY
507+
echo "The following branches will be deleted on **$FriendlyDeletionDate** because they will be over $MaxDaysBehind days behind the $DefaultBranch branch on that date. They also contain $MaxCommitsAhead or fewer commits not in the $DefaultBranch branch." >> $env:GITHUB_STEP_SUMMARY
508+
echo "" >> $env:GITHUB_STEP_SUMMARY
509+
echo "> [!IMPORTANT]" >> $env:GITHUB_STEP_SUMMARY
510+
echo "> **If you don't want a branch to be deleted, merge $DefaultBranch into it before $FriendlyDeletionDate.**" >> $env:GITHUB_STEP_SUMMARY
475511
echo "" >> $env:GITHUB_STEP_SUMMARY
476512

477513
}
@@ -500,7 +536,7 @@ jobs:
500536

501537
} Else {
502538

503-
echo "No branches were deleted during this run." >> $env:GITHUB_STEP_SUMMARY
539+
echo "No branches were deleted or were identified as pending deletion during this run." >> $env:GITHUB_STEP_SUMMARY
504540
echo "" >> $env:GITHUB_STEP_SUMMARY
505541

506542
}
@@ -551,99 +587,82 @@ jobs:
551587
echo "## Workflow overview" >> $env:GITHUB_STEP_SUMMARY
552588
echo "" >> $env:GITHUB_STEP_SUMMARY
553589

554-
$ReportOnlyMode = "Report only mode: $ReportOnly"
590+
$ReportOnlyMode = "**Report only mode**: $ReportOnly"
555591
Write-Host $ReportOnlyMode
556-
echo $ReportOnlyMode >> $env:GITHUB_STEP_SUMMARY
557-
echo "" >> $env:GITHUB_STEP_SUMMARY
592+
echo "$ReportOnlyMode\" >> $env:GITHUB_STEP_SUMMARY
558593

559-
$DeletionRunMode = "Deletion run: $DeletionRun"
594+
$DeletionRunMode = "**Deletion run**: $DeletionRun"
560595
Write-Host $DeletionRunMode
561-
echo $DeletionRunMode >> $env:GITHUB_STEP_SUMMARY
562-
echo "" >> $env:GITHUB_STEP_SUMMARY
596+
echo "$DeletionRunMode\" >> $env:GITHUB_STEP_SUMMARY
563597

564-
$AllowDataLossSetting = "Allow data loss: $AllowDataLoss"
598+
$AllowDataLossSetting = "**Allow data loss**: $AllowDataLoss"
565599
Write-Host $AllowDataLossSetting
566-
echo "$AllowDataLossSetting" >> $env:GITHUB_STEP_SUMMARY
567-
echo "" >> $env:GITHUB_STEP_SUMMARY
600+
echo "$AllowDataLossSetting\" >> $env:GITHUB_STEP_SUMMARY
568601

569-
$MaximumCommitsAheadByLimit = "Maximum commits ahead by limit: $MaxCommitsAhead"
602+
$MaximumCommitsAheadByLimit = "**Maximum commits ahead by limit**: $MaxCommitsAhead"
570603
Write-Host $MaximumCommitsAheadByLimit
571-
echo "$MaximumCommitsAheadByLimit" >> $env:GITHUB_STEP_SUMMARY
572-
echo "" >> $env:GITHUB_STEP_SUMMARY
604+
echo "$MaximumCommitsAheadByLimit\" >> $env:GITHUB_STEP_SUMMARY
573605

574-
$MaximumDaysBehindLimit = "Maximum days behind limit: $MaxDaysBehind"
606+
$MaximumDaysBehindLimit = "**Maximum days behind limit**: $MaxDaysBehind"
575607
Write-Host $MaximumDaysBehindLimit
576-
echo "$MaximumDaysBehindLimit" >> $env:GITHUB_STEP_SUMMARY
577-
echo "" >> $env:GITHUB_STEP_SUMMARY
608+
echo "$MaximumDaysBehindLimit\" >> $env:GITHUB_STEP_SUMMARY
578609

579-
$MaximumBranchAgeBasedOnDaysBehindLimit = "Maximum branch age based on days behind limit: $DateLimit"
610+
$MaximumBranchAgeBasedOnDaysBehindLimit = "**Maximum branch age based on days behind limit**: $DateLimit"
580611
Write-Host $MaximumBranchAgeBasedOnDaysBehindLimit
581-
echo "$MaximumBranchAgeBasedOnDaysBehindLimit" >> $env:GITHUB_STEP_SUMMARY
582-
echo "" >> $env:GITHUB_STEP_SUMMARY
612+
echo "$MaximumBranchAgeBasedOnDaysBehindLimit\" >> $env:GITHUB_STEP_SUMMARY
583613

584614
$SeparatorLine = "==========="
585615
Write-Host $SeparatorLine
586-
echo $SeparatorLine >> $env:GITHUB_STEP_SUMMARY
587-
echo "" >> $env:GITHUB_STEP_SUMMARY
616+
echo "$SeparatorLine\" >> $env:GITHUB_STEP_SUMMARY
588617

589-
$DefaultBranchSkipList = "Default branch skip list: $DefaultSkipBranchList"
618+
$DefaultBranchSkipList = "**Default branch skip list**: $DefaultSkipBranchList"
590619
Write-Host $DefaultBranchSkipList
591-
echo $DefaultBranchSkipList >> $env:GITHUB_STEP_SUMMARY
592-
echo "" >> $env:GITHUB_STEP_SUMMARY
620+
echo "$DefaultBranchSkipList\" >> $env:GITHUB_STEP_SUMMARY
593621

594-
$RepoBranchSkipListText = "Repo branch skip list: $RepoBranchSkipList"
622+
$RepoBranchSkipListText = "**Repo branch skip list**: $RepoBranchSkipList"
595623
Write-Host $RepoBranchSkipListText
596-
echo "$RepoBranchSkipListText" >> $env:GITHUB_STEP_SUMMARY
597-
echo "" >> $env:GITHUB_STEP_SUMMARY
624+
echo "$RepoBranchSkipListText\" >> $env:GITHUB_STEP_SUMMARY
598625

599626
Write-Host $SeparatorLine
600-
echo "$SeparatorLine" >> $env:GITHUB_STEP_SUMMARY
601-
echo "" >> $env:GITHUB_STEP_SUMMARY
627+
echo "$SeparatorLine\" >> $env:GITHUB_STEP_SUMMARY
602628

603-
$TotalBranchesBeforeRun = "Total branches before run: $StartBranchCount"
629+
$TotalBranchesBeforeRun = "**Total branches before run**: $StartBranchCount"
604630
Write-Host $TotalBranchesBeforeRun
605-
echo "$TotalBranchesBeforeRun" >> $env:GITHUB_STEP_SUMMARY
606-
echo "" >> $env:GITHUB_STEP_SUMMARY
631+
echo "$TotalBranchesBeforeRun\" >> $env:GITHUB_STEP_SUMMARY
607632

608-
$TotalBranchesAfterRun = "Total branches after run: $EndBranchCount"
633+
$TotalBranchesAfterRun = "**Total branches after run**: $EndBranchCount"
609634
Write-Host $TotalBranchesAfterRun
610-
echo "$TotalBranchesAfterRun" >> $env:GITHUB_STEP_SUMMARY
611-
echo "" >> $env:GITHUB_STEP_SUMMARY
635+
echo "$TotalBranchesAfterRun\" >> $env:GITHUB_STEP_SUMMARY
612636

613637
Write-Host $SeparatorLine
614-
echo "$SeparatorLine" >> $env:GITHUB_STEP_SUMMARY
615-
echo "" >> $env:GITHUB_STEP_SUMMARY
638+
echo "$SeparatorLine\" >> $env:GITHUB_STEP_SUMMARY
616639

617-
$WatchListBranches = "Watch list branches: $WatchListCount"
640+
$WatchListBranches = "**Watch list branches**: $WatchListCount"
618641
Write-Host $WatchListBranches
619-
echo "$WatchListBranches" >> $env:GITHUB_STEP_SUMMARY
620-
echo "" >> $env:GITHUB_STEP_SUMMARY
642+
echo "$WatchListBranches\" >> $env:GITHUB_STEP_SUMMARY
621643

622644
If ($DeletionRun) {
623645

624-
$DataLossBlockedBranches = "$ReportOnlyString Data loss blocked branches: $DataLossBlockedCount"
625-
$BranchesDeletedWithDataLoss = "$ReportOnlyString Branches deleted with data loss: $DataLossCount"
626-
$TotalDeletedBranches = "$ReportOnlyString Total deleted branches: $DeleteBranchCount"
646+
$DataLossBlockedBranches = "**$ReportOnlyString Data loss blocked branches**: $DataLossBlockedCount"
647+
$BranchesDeletedWithDataLoss = "**$ReportOnlyString Branches deleted with data loss**: $DataLossCount"
648+
$TotalDeletedBranches = "**$ReportOnlyString Total deleted branches**: $DeleteBranchCount"
627649

628650
} Else {
629651

630-
$DataLossBlockedBranches = "Branches pending deletion (data loss blocked): $DataLossBlockedCount"
631-
$BranchesDeletedWithDataLoss = "Branches pending deletion (data loss): $DataLossCount"
632-
$TotalDeletedBranches = "Total branches pending deletion: $DeleteBranchCount"
652+
$DataLossBlockedBranches = "**Branches pending deletion (data loss blocked)**: $DataLossBlockedCount"
653+
$BranchesDeletedWithDataLoss = "**Branches pending deletion (data loss)**: $DataLossCount"
654+
$TotalDeletedBranches = "**Total branches pending deletion**: $DeleteBranchCount"
633655

634656
}
635657

636658
Write-Host $DataLossBlockedBranches
637-
echo "$DataLossBlockedBranches" >> $env:GITHUB_STEP_SUMMARY
638-
echo "" >> $env:GITHUB_STEP_SUMMARY
659+
echo "$DataLossBlockedBranches\" >> $env:GITHUB_STEP_SUMMARY
639660

640661
Write-Host $SeparatorLine
641-
echo "$SeparatorLine" >> $env:GITHUB_STEP_SUMMARY
642-
echo "" >> $env:GITHUB_STEP_SUMMARY
662+
echo "$SeparatorLine\" >> $env:GITHUB_STEP_SUMMARY
643663

644664
Write-Host $BranchesDeletedWithDataLoss
645-
echo "$BranchesDeletedWithDataLoss" >> $env:GITHUB_STEP_SUMMARY
646-
echo "" >> $env:GITHUB_STEP_SUMMARY
665+
echo "$BranchesDeletedWithDataLoss\" >> $env:GITHUB_STEP_SUMMARY
647666

648667
Write-Host $TotalDeletedBranches
649668
echo "$TotalDeletedBranches" >> $env:GITHUB_STEP_SUMMARY

0 commit comments

Comments
 (0)