From d4ae30117c3f0472ee3d63efc2acb7bbf3903404 Mon Sep 17 00:00:00 2001 From: Chidozie Ononiwu Date: Thu, 2 Sep 2021 18:28:14 -0700 Subject: [PATCH 1/3] Move logic for removing empty sections to ChangeLog-Operations.ps1 --- eng/common/scripts/ChangeLog-Operations.ps1 | 49 +++++++++++++++++++++ eng/common/scripts/Update-ChangeLog.ps1 | 43 +----------------- 2 files changed, 51 insertions(+), 41 deletions(-) diff --git a/eng/common/scripts/ChangeLog-Operations.ps1 b/eng/common/scripts/ChangeLog-Operations.ps1 index 25423b0ea8ad5..0f81e44b396a8 100644 --- a/eng/common/scripts/ChangeLog-Operations.ps1 +++ b/eng/common/scripts/ChangeLog-Operations.ps1 @@ -289,3 +289,52 @@ function Set-ChangeLogContent { Set-Content -Path $ChangeLogLocation -Value $changeLogContent } + +function Remove-EmptySections { + param ( + [Parameter(Mandatory = $true)] + $ChangeLogEntry + ) + + $releaseContent = $ChangeLogEntry.ReleaseContent + $sectionsToRemove = @() + + if ($releaseContent.Count -gt 0) + { + $parsedSections = $ChangeLogEntry.Sections + $sanitizedReleaseContent = New-Object System.Collections.ArrayList(,$releaseContent) + + foreach ($key in $parsedSections.Keys) + { + if ([System.String]::IsNullOrWhiteSpace($parsedSections[$key])) + { + for ($i = 0; $i -lt $sanitizedReleaseContent.Count; $i++) + { + $line = $sanitizedReleaseContent[$i] + if ($line -eq "### $key") + { + $sanitizedReleaseContent.RemoveAt($i) + $j = $i + while($j -lt $sanitizedReleaseContent.Count) + { + $line = $sanitizedReleaseContent[$j] + if ([System.String]::IsNullOrWhiteSpace($line)) + { + $sanitizedReleaseContent.RemoveAt($j) + } + else + { + break + } + } + $sectionsToRemove += $key + break + } + } + } + } + $ChangeLogEntry.ReleaseContent = $sanitizedReleaseContent.ToArray() + $sectionsToRemove | % { $ChangeLogEntry.Sections.Remove($_) } + } + return $changeLogEntry +} diff --git a/eng/common/scripts/Update-ChangeLog.ps1 b/eng/common/scripts/Update-ChangeLog.ps1 index 1524bd7318c1c..d164534e81056 100644 --- a/eng/common/scripts/Update-ChangeLog.ps1 +++ b/eng/common/scripts/Update-ChangeLog.ps1 @@ -106,47 +106,8 @@ if ($LatestsSorted[0] -ne $Version) { if ($ReplaceLatestEntryTitle) { - # Remove empty sections from content - $sanitizedContent = @() - $sectionContent = @() - $sectionContentCount = 0 - $latesVersionContent = $ChangeLogEntries[$LatestVersion].ReleaseContent - - foreach ($line in $latesVersionContent) - { - if ($line.StartsWith("### ") -or $sectionContentCount -gt 0) - { - if ($line.StartsWith("#") -and $sectionContentCount -gt 1) - { - $sanitizedContent += $sectionContent - $sectionContent = @() - $sectionContentCount = 0 - } - - if ($line.StartsWith("#") -and $sectionContentCount -eq 1) - { - $sectionContent = @() - $sectionContentCount = 0 - } - - $sectionContent += $line - if (-not [System.String]::IsNullOrWhiteSpace($line)) - { - $sectionContentCount++ - } - } - elseif ($sectionContent.Count -eq 0) - { - $sanitizedContent += $line - } - } - - if ($sectionContentCount -gt 1) - { - $sanitizedContent += $sectionContent - } - - $newChangeLogEntry = New-ChangeLogEntry -Version $Version -Status $ReleaseStatus -Content $sanitizedContent + $sanitizedChangelogEntry = Remove-EmptySections -ChangeLogEntry $ChangeLogEntries[$LatestVersion] + $newChangeLogEntry = New-ChangeLogEntry -Version $Version -Status $ReleaseStatus -Content $sanitizedChangelogEntry.ReleaseContent LogDebug "Resetting latest entry title to [$($newChangeLogEntry.ReleaseTitle)]" $ChangeLogEntries.Remove($LatestVersion) if ($newChangeLogEntry) { From 65747b97123b7abc14035688ceb45ebf565e2a09 Mon Sep 17 00:00:00 2001 From: Chidozie Ononiwu Date: Fri, 3 Sep 2021 16:09:40 -0700 Subject: [PATCH 2/3] Refactor sections regex --- eng/common/scripts/ChangeLog-Operations.ps1 | 23 +++++++-------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/eng/common/scripts/ChangeLog-Operations.ps1 b/eng/common/scripts/ChangeLog-Operations.ps1 index 0f81e44b396a8..2de780150145a 100644 --- a/eng/common/scripts/ChangeLog-Operations.ps1 +++ b/eng/common/scripts/ChangeLog-Operations.ps1 @@ -3,6 +3,7 @@ . "${PSScriptRoot}\SemVer.ps1" $RELEASE_TITLE_REGEX = "(?^\#+\s+(?$([AzureEngSemanticVersion]::SEMVER_REGEX))(\s+(?\(.+\))))" +$SECTIONS_HEADER_REGEX = "^###\s(?.*)" $CHANGELOG_UNRELEASED_STATUS = "(Unreleased)" $CHANGELOG_DATE_FORMAT = "yyyy-MM-dd" $RecommendedSectionHeaders = @("Features Added", "Breaking Changes", "Bugs Fixed", "Other Changes") @@ -56,7 +57,7 @@ function Get-ChangeLogEntriesFromContent { } else { if ($changeLogEntry) { - if ($line.Trim() -match "^###\s(?.*)") + if ($line.Trim() -match $SECTIONS_HEADER_REGEX) { $sectionName = $matches["sectionName"].Trim() $changeLogEntry.Sections[$sectionName] = @() @@ -304,37 +305,27 @@ function Remove-EmptySections { $parsedSections = $ChangeLogEntry.Sections $sanitizedReleaseContent = New-Object System.Collections.ArrayList(,$releaseContent) - foreach ($key in $parsedSections.Keys) + foreach ($key in @($parsedSections.Key)) { if ([System.String]::IsNullOrWhiteSpace($parsedSections[$key])) { for ($i = 0; $i -lt $sanitizedReleaseContent.Count; $i++) { $line = $sanitizedReleaseContent[$i] - if ($line -eq "### $key") + if ($line -match $SECTIONS_HEADER_REGEX -and $matches["sectionName"].Trim() -eq $key) { $sanitizedReleaseContent.RemoveAt($i) - $j = $i - while($j -lt $sanitizedReleaseContent.Count) + while($i -lt $sanitizedReleaseContent.Count -and [System.String]::IsNullOrWhiteSpace($sanitizedReleaseContent[$i])) { - $line = $sanitizedReleaseContent[$j] - if ([System.String]::IsNullOrWhiteSpace($line)) - { - $sanitizedReleaseContent.RemoveAt($j) - } - else - { - break - } + $sanitizedReleaseContent.RemoveAt($i) } - $sectionsToRemove += $key + $ChangeLogEntry.Sections.Remove($key) break } } } } $ChangeLogEntry.ReleaseContent = $sanitizedReleaseContent.ToArray() - $sectionsToRemove | % { $ChangeLogEntry.Sections.Remove($_) } } return $changeLogEntry } From 338b964e9d3f093550a75ece564b3525b2c53e48 Mon Sep 17 00:00:00 2001 From: Chidozie Ononiwu Date: Fri, 10 Sep 2021 17:31:07 -0700 Subject: [PATCH 3/3] Add SanitizeEntry parameter --- eng/common/scripts/Update-ChangeLog.ps1 | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/eng/common/scripts/Update-ChangeLog.ps1 b/eng/common/scripts/Update-ChangeLog.ps1 index d164534e81056..43e97918bea12 100644 --- a/eng/common/scripts/Update-ChangeLog.ps1 +++ b/eng/common/scripts/Update-ChangeLog.ps1 @@ -4,6 +4,7 @@ # Version : Version to add or replace in change log # Unreleased: Default is true. If it is set to false, then today's date will be set in verion title. If it is True then title will show "Unreleased" # ReplaceLatestEntryTitle: Replaces the latest changelog entry title. +# SanitizeEntry: Removes all empty section in the entry that is updated param ( [Parameter(Mandatory = $true)] @@ -13,7 +14,8 @@ param ( [Boolean]$Unreleased = $true, [Boolean]$ReplaceLatestEntryTitle = $false, [String]$ChangelogPath, - [String]$ReleaseDate + [String]$ReleaseDate, + [Boolean]$SanitizeEntry = $false ) Set-StrictMode -Version 3 @@ -106,8 +108,12 @@ if ($LatestsSorted[0] -ne $Version) { if ($ReplaceLatestEntryTitle) { - $sanitizedChangelogEntry = Remove-EmptySections -ChangeLogEntry $ChangeLogEntries[$LatestVersion] - $newChangeLogEntry = New-ChangeLogEntry -Version $Version -Status $ReleaseStatus -Content $sanitizedChangelogEntry.ReleaseContent + $entryToBeUpdated = $ChangeLogEntries[$LatestVersion] + if ($SanitizeEntry) + { + $entryToBeUpdated = Remove-EmptySections -ChangeLogEntry $entryToBeUpdated + } + $newChangeLogEntry = New-ChangeLogEntry -Version $Version -Status $ReleaseStatus -Content $entryToBeUpdated LogDebug "Resetting latest entry title to [$($newChangeLogEntry.ReleaseTitle)]" $ChangeLogEntries.Remove($LatestVersion) if ($newChangeLogEntry) { @@ -123,6 +129,10 @@ elseif ($ChangeLogEntries.Contains($Version)) LogDebug "Updating ReleaseStatus for Version [$Version] to [$($ReleaseStatus)]" $ChangeLogEntries[$Version].ReleaseStatus = $ReleaseStatus $ChangeLogEntries[$Version].ReleaseTitle = "## $Version $ReleaseStatus" + if ($SanitizeEntry) + { + $ChangeLogEntries[$Version] = Remove-EmptySections -ChangeLogEntry $ChangeLogEntries[$Version] + } } else {