Skip to content

Commit 01ae26c

Browse files
Sync eng/common directory with azure-sdk-tools for PR 1979 (Azure#24102)
* Move logic for removing empty sections to ChangeLog-Operations.ps1 * Refactor sections regex * Add SanitizeEntry parameter Co-authored-by: Chidozie Ononiwu <chononiw@microsoft.com>
1 parent f6a1ec6 commit 01ae26c

File tree

2 files changed

+52
-41
lines changed

2 files changed

+52
-41
lines changed

eng/common/scripts/ChangeLog-Operations.ps1

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
. "${PSScriptRoot}\SemVer.ps1"
44

55
$RELEASE_TITLE_REGEX = "(?<releaseNoteTitle>^\#+\s+(?<version>$([AzureEngSemanticVersion]::SEMVER_REGEX))(\s+(?<releaseStatus>\(.+\))))"
6+
$SECTIONS_HEADER_REGEX = "^###\s(?<sectionName>.*)"
67
$CHANGELOG_UNRELEASED_STATUS = "(Unreleased)"
78
$CHANGELOG_DATE_FORMAT = "yyyy-MM-dd"
89
$RecommendedSectionHeaders = @("Features Added", "Breaking Changes", "Bugs Fixed", "Other Changes")
@@ -56,7 +57,7 @@ function Get-ChangeLogEntriesFromContent {
5657
}
5758
else {
5859
if ($changeLogEntry) {
59-
if ($line.Trim() -match "^###\s(?<sectionName>.*)")
60+
if ($line.Trim() -match $SECTIONS_HEADER_REGEX)
6061
{
6162
$sectionName = $matches["sectionName"].Trim()
6263
$changeLogEntry.Sections[$sectionName] = @()
@@ -289,3 +290,42 @@ function Set-ChangeLogContent {
289290

290291
Set-Content -Path $ChangeLogLocation -Value $changeLogContent
291292
}
293+
294+
function Remove-EmptySections {
295+
param (
296+
[Parameter(Mandatory = $true)]
297+
$ChangeLogEntry
298+
)
299+
300+
$releaseContent = $ChangeLogEntry.ReleaseContent
301+
$sectionsToRemove = @()
302+
303+
if ($releaseContent.Count -gt 0)
304+
{
305+
$parsedSections = $ChangeLogEntry.Sections
306+
$sanitizedReleaseContent = New-Object System.Collections.ArrayList(,$releaseContent)
307+
308+
foreach ($key in @($parsedSections.Key))
309+
{
310+
if ([System.String]::IsNullOrWhiteSpace($parsedSections[$key]))
311+
{
312+
for ($i = 0; $i -lt $sanitizedReleaseContent.Count; $i++)
313+
{
314+
$line = $sanitizedReleaseContent[$i]
315+
if ($line -match $SECTIONS_HEADER_REGEX -and $matches["sectionName"].Trim() -eq $key)
316+
{
317+
$sanitizedReleaseContent.RemoveAt($i)
318+
while($i -lt $sanitizedReleaseContent.Count -and [System.String]::IsNullOrWhiteSpace($sanitizedReleaseContent[$i]))
319+
{
320+
$sanitizedReleaseContent.RemoveAt($i)
321+
}
322+
$ChangeLogEntry.Sections.Remove($key)
323+
break
324+
}
325+
}
326+
}
327+
}
328+
$ChangeLogEntry.ReleaseContent = $sanitizedReleaseContent.ToArray()
329+
}
330+
return $changeLogEntry
331+
}

eng/common/scripts/Update-ChangeLog.ps1

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# Version : Version to add or replace in change log
55
# 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"
66
# ReplaceLatestEntryTitle: Replaces the latest changelog entry title.
7+
# SanitizeEntry: Removes all empty section in the entry that is updated
78

89
param (
910
[Parameter(Mandatory = $true)]
@@ -13,7 +14,8 @@ param (
1314
[Boolean]$Unreleased = $true,
1415
[Boolean]$ReplaceLatestEntryTitle = $false,
1516
[String]$ChangelogPath,
16-
[String]$ReleaseDate
17+
[String]$ReleaseDate,
18+
[Boolean]$SanitizeEntry = $false
1719
)
1820
Set-StrictMode -Version 3
1921

@@ -106,47 +108,12 @@ if ($LatestsSorted[0] -ne $Version) {
106108

107109
if ($ReplaceLatestEntryTitle)
108110
{
109-
# Remove empty sections from content
110-
$sanitizedContent = @()
111-
$sectionContent = @()
112-
$sectionContentCount = 0
113-
$latesVersionContent = $ChangeLogEntries[$LatestVersion].ReleaseContent
114-
115-
foreach ($line in $latesVersionContent)
116-
{
117-
if ($line.StartsWith("### ") -or $sectionContentCount -gt 0)
118-
{
119-
if ($line.StartsWith("#") -and $sectionContentCount -gt 1)
120-
{
121-
$sanitizedContent += $sectionContent
122-
$sectionContent = @()
123-
$sectionContentCount = 0
124-
}
125-
126-
if ($line.StartsWith("#") -and $sectionContentCount -eq 1)
127-
{
128-
$sectionContent = @()
129-
$sectionContentCount = 0
130-
}
131-
132-
$sectionContent += $line
133-
if (-not [System.String]::IsNullOrWhiteSpace($line))
134-
{
135-
$sectionContentCount++
136-
}
137-
}
138-
elseif ($sectionContent.Count -eq 0)
139-
{
140-
$sanitizedContent += $line
141-
}
142-
}
143-
144-
if ($sectionContentCount -gt 1)
111+
$entryToBeUpdated = $ChangeLogEntries[$LatestVersion]
112+
if ($SanitizeEntry)
145113
{
146-
$sanitizedContent += $sectionContent
114+
$entryToBeUpdated = Remove-EmptySections -ChangeLogEntry $entryToBeUpdated
147115
}
148-
149-
$newChangeLogEntry = New-ChangeLogEntry -Version $Version -Status $ReleaseStatus -Content $sanitizedContent
116+
$newChangeLogEntry = New-ChangeLogEntry -Version $Version -Status $ReleaseStatus -Content $entryToBeUpdated
150117
LogDebug "Resetting latest entry title to [$($newChangeLogEntry.ReleaseTitle)]"
151118
$ChangeLogEntries.Remove($LatestVersion)
152119
if ($newChangeLogEntry) {
@@ -162,6 +129,10 @@ elseif ($ChangeLogEntries.Contains($Version))
162129
LogDebug "Updating ReleaseStatus for Version [$Version] to [$($ReleaseStatus)]"
163130
$ChangeLogEntries[$Version].ReleaseStatus = $ReleaseStatus
164131
$ChangeLogEntries[$Version].ReleaseTitle = "## $Version $ReleaseStatus"
132+
if ($SanitizeEntry)
133+
{
134+
$ChangeLogEntries[$Version] = Remove-EmptySections -ChangeLogEntry $ChangeLogEntries[$Version]
135+
}
165136
}
166137
else
167138
{

0 commit comments

Comments
 (0)