Skip to content

Commit

Permalink
Update date parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
chidozieononiwu committed Nov 10, 2020
1 parent 1498564 commit 3c6091b
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 96 deletions.
64 changes: 45 additions & 19 deletions eng/common/scripts/ChangeLog-Operations.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
. "${PSScriptRoot}\logging.ps1"
. "${PSScriptRoot}\SemVer.ps1"

$RELEASE_TITLE_REGEX = "(?<releaseNoteTitle>^\#+.*(?<version>\b\d+\.\d+\.\d+([^0-9\s][^\s:]+)?)(\s(?<releaseStatus>\(Unreleased\)|\(\d{4}-\d{2}-\d{2}\)))?)"
$UNRELEASED_TAG = "(Unreleased)"
$RELEASE_TITLE_REGEX = "(?<releaseNoteTitle>^\#+.*(?<version>\b\d+\.\d+\.\d+([^0-9\s][^\s:]+)?)(\s+(?<releaseStatus>\(Unreleased\)|\(\d{4}-\d{2}-\d{2}\)))?)"
$CHANGELOG_UNRELEASED_STATUS = "(Unreleased)"
$CHANGELOG_DATE_FORMAT = "yyyy-MM-dd"

# Returns a Collection of changeLogEntry object containing changelog info for all version present in the gived CHANGELOG
function Get-ChangeLogEntries {
Expand All @@ -24,8 +25,12 @@ function Get-ChangeLogEntries {
$changeLogEntry = $null
foreach ($line in $contents) {
if ($line -match $RELEASE_TITLE_REGEX) {
$changeLogEntry = New-ChangeLogEntry -Version $matches["version"] -Status $matches["releaseStatus"] `
-Title $line
$changeLogEntry = [pscustomobject]@{
ReleaseVersion = $matches["version"]
ReleaseStatus = $matches["releaseStatus"]
ReleaseTitle = "## {0} {1}" -f $matches["version"], $matches["releaseStatus"]
ReleaseContent = @()
}
$changeLogEntries[$changeLogEntry.ReleaseVersion] = $changeLogEntry
}
else {
Expand Down Expand Up @@ -103,18 +108,28 @@ function Confirm-ChangeLogEntry {
Write-Host "-----"

if ([System.String]::IsNullOrEmpty($changeLogEntry.ReleaseStatus)) {
Write-Error "Entry does not have a correct release status. Please ensure the status is set to a date '(yyyy-MM-dd)' or '$UNRELEASED_TAG' if not yet released."
Write-Error "Entry does not have a correct release status. Please ensure the status is set to a date '($CHANGELOG_DATE_FORMAT)' or '$CHANGELOG_UNRELEASED_STATUS' if not yet released."
return $false
}

if ($ForRelease -eq $True) {
if ($changeLogEntry.ReleaseStatus -eq $UNRELEASED_TAG) {
Write-Error "Entry has no release date set. Please ensure to set a release date with format 'yyyy-MM-dd'."
if ($changeLogEntry.ReleaseStatus -eq $CHANGELOG_UNRELEASED_STATUS) {
LogError "Entry has no release date set. Please ensure to set a release date with format '$CHANGELOG_DATE_FORMAT'."
return $false
}
else {
$status = $changeLogEntry.ReleaseStatus.Trim().Trim("()")
try {
[DateTime]$status
}
catch {
LogError "Invalid date [ $status ] passed as status for Version [$($changeLogEntry.ReleaseVersion)]."
return $false
}
}

if ([System.String]::IsNullOrWhiteSpace($changeLogEntry.ReleaseContent)) {
Write-Error "Entry has no content. Please ensure to provide some content of what changed in this version."
LogError "Entry has no content. Please ensure to provide some content of what changed in this version."
return $false
}
}
Expand All @@ -126,32 +141,38 @@ function New-ChangeLogEntry {
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String]$Version,
[String]$Status=$UNRELEASED_TAG,
[String]$Title,
[String]$Status=$CHANGELOG_UNRELEASED_STATUS,
[String[]]$Content
)

# Validate relase Status
# Validate RelaseStatus
$Status = $Status.Trim().Trim("()")
if ($Status -ne "Unreleased") {
$dateFormat = "yyyy-MM-dd"
$provider = [System.Globalization.CultureInfo]::InvariantCulture
try {
$Status = ([System.DateTime]::ParseExact($Status, $dateFormat, $provider)).ToString($dateFormat)
$Status = ([DateTime]$Status).ToString($CHANGELOG_DATE_FORMAT)
}
catch {
LogWarning "Invalid date [ $Status ] passed as status for Version [$Version]. Please use a valid date in the format '$dateFormat' or use '$UNRELEASED_TAG'"
LogWarning "Invalid date [ $Status ] passed as status for Version [$Version]. Please use a valid date in the format '$CHANGELOG_DATE_FORMAT' or use '$CHANGELOG_UNRELEASED_STATUS'"
return $null
}
}

$Status = "($Status)"

# Validate Version
try {
$Version = ([AzureEngSemanticVersion]::ParseVersionString($Version)).ToString()
}
catch {
LogWarning "Invalid version [ $Version ]."
return $null
}

if (!$Content) { $Content = @() }
if (!$Title) { $Title = "## $Version $Status" }

$newChangeLogEntry = [pscustomobject]@{
ReleaseVersion = $Version
ReleaseStatus = $Status
ReleaseTitle = $Title
ReleaseTitle = "## $Version $Status"
ReleaseContent = $Content
}

Expand Down Expand Up @@ -182,7 +203,12 @@ function Set-ChangeLogContent {
foreach ($version in $VersionsSorted) {
$changeLogEntry = $ChangeLogEntries[$version]
$changeLogContent += $changeLogEntry.ReleaseTitle
$changeLogContent += $changeLogEntry.ReleaseContent
if ($changeLogEntry.ReleaseContent.Count -eq 0) {
$changeLogContent += @("","")
}
else {
$changeLogContent += $changeLogEntry.ReleaseContent
}
}

Set-Content -Path $ChangeLogLocation -Value $changeLogContent
Expand Down
4 changes: 1 addition & 3 deletions eng/common/scripts/Invoke-GitHubAPI.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
if (!(Test-Path Function:LogWarning)) {
. "${PSScriptRoot}\logging.ps1"
}
. "${PSScriptRoot}\logging.ps1"

$GithubAPIBaseURI = "https://api.github.com/repos"

Expand Down
19 changes: 9 additions & 10 deletions eng/common/scripts/SemVer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ class AzureEngSemanticVersion {
[string] $RawVersion
[bool] $IsSemVerFormat
[string] $DefaultPrereleaseLabel
[string] $ParseLanguage = $Language

# Regex inspired but simplified from https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
static [string] $SEMVER_REGEX = "(?<major>0|[1-9]\d*)\.(?<minor>0|[1-9]\d*)\.(?<patch>0|[1-9]\d*)(?:(?<presep>-?)(?<prelabel>[a-zA-Z-]*)(?<prenumsep>\.?)(?<prenumber>0|[1-9]\d*))?"
static [string] $ParseLanguage = $Language

static [AzureEngSemanticVersion] ParseVersionString([string] $versionString)
{
Expand Down Expand Up @@ -58,7 +59,12 @@ class AzureEngSemanticVersion {
$this.Minor = [int]$matches.Minor
$this.Patch = [int]$matches.Patch

$this.SetupDefaultConventions()
if ([AzureEngSemanticVersion]::ParseLanguage -eq "python") {
$this.SetupPythonConventions()
}
else {
$this.SetupDefaultConventions()
}

if ($null -eq $matches['prelabel'])
{
Expand Down Expand Up @@ -144,14 +150,7 @@ class AzureEngSemanticVersion {

static [string[]] SortVersionStrings([string[]] $versionStrings)
{
$versions = $versionStrings | ForEach-Object {
if ($ParseLanguage -eq "python") {
[AzureEngSemanticVersion]::ParsePythonVersionString($_)
}
else {
[AzureEngSemanticVersion]::ParseVersionString($_)
}
}
$versions = $versionStrings | ForEach-Object { [AzureEngSemanticVersion]::ParseVersionString($_) }
$sortedVersions = [AzureEngSemanticVersion]::SortVersions($versions)
return ($sortedVersions | ForEach-Object { $_.ToString() })
}
Expand Down
133 changes: 70 additions & 63 deletions eng/common/scripts/Update-ChangeLog.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,94 +14,101 @@ param (
[Parameter(Mandatory = $true)]
[String]$PackageName,
[boolean]$Unreleased=$True,
[boolean]$ReplaceVersion = $False
[boolean]$ReplaceVersion = $False,
[String]$ReleaseDate
)

DynamicParam {
if ($Unreleased -eq $False) {
$releaseStatusAttribute = New-Object System.Management.Automation.ParameterAttribute
$releaseStatusAttribute.Mandatory = $False
$attributeCollection = New-object System.Collections.ObjectModel.Collection[System.Attribute]
$attributeCollection.Add($releaseStatusAttribute)
$releaseStatusParam = New-Object System.Management.Automation.RuntimeDefinedParameter('ReleaseDate', [string], $attributeCollection)
$paramDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
$paramDictionary.Add('ReleaseDate', $releaseStatusParam)
return $paramDictionary
}
if ($ReleaseDate -and ($Unreleased -eq $True)) {
LogError "Do not pass 'ReleaseDate' arguement when 'Unreleased' is true"
exit 1
}

Begin {
. "${PSScriptRoot}\common.ps1"
$UNRELEASED_TAG = "(Unreleased)"
. "${PSScriptRoot}\common.ps1"

$dateFormat = "yyyy-MM-dd"
$provider = [System.Globalization.CultureInfo]::InvariantCulture
if ($ReleaseDate)
{
try {
$ReleaseStatus = "({0})" -f ([System.DateTime]::ParseExact($ReleaseDate, $dateFormat, $provider)).ToString($dateFormat)
}
catch {
LogError "Invalid Release date. Please use a valid date in the format '$dateFormat'"
}
if ($ReleaseDate)
{
try {
$ReleaseStatus = ([DateTime]$ReleaseDate).ToString($CHANGELOG_DATE_FORMAT)
$ReleaseStatus = "($ReleaseStatus)"
}
elseif ($Unreleased) {
$ReleaseStatus = "$UNRELEASED_TAG"
}
else {
$ReleaseStatus = "($(Get-Date -Format $dateFormat))"
catch {
LogError "Invalid 'ReleaseDate'. Please use a valid date in the format '$CHANGELOG_DATE_FORMAT'"
exit 1
}
}
elseif ($Unreleased) {
$ReleaseStatus = $CHANGELOG_UNRELEASED_STATUS
}
else {
$ReleaseStatus = "$(Get-Date -Format $CHANGELOG_DATE_FORMAT)"
$ReleaseStatus = "($ReleaseStatus)"
}

Process {
$PkgProperties = Get-PkgProperties -PackageName $PackageName -ServiceDirectory $ServiceDirectory
$ChangeLogEntries = Get-ChangeLogEntries -ChangeLogLocation $PkgProperties.ChangeLogPath
$PkgProperties = Get-PkgProperties -PackageName $PackageName -ServiceDirectory $ServiceDirectory
$ChangeLogEntries = Get-ChangeLogEntries -ChangeLogLocation $PkgProperties.ChangeLogPath

if ($ChangeLogEntries.Count -gt 0)
if ($ChangeLogEntries.Count -gt 0)
{
if ($ChangeLogEntries.Contains($Version))
{
if ($ChangeLogEntries.Contains($Version))
if ($ChangeLogEntries[$Version].ReleaseStatus -eq $ReleaseStatus)
{
if ($ChangeLogEntries[$Version].ReleaseStatus -eq $ReleaseStatus)
{
LogWarning "Version is already present in change log with specificed ReleaseStatus [$ReleaseStatus]"
exit(0)
}
LogWarning "Version is already present in change log with specificed ReleaseStatus [$ReleaseStatus]"
exit(0)
}

if ($Unreleased -and ($ChangeLogEntries[$Version].ReleaseStatus -ne $ReleaseStatus))
{
LogWarning "Version is already present in change log with a release date. Please review [$($PkgProperties.ChangeLogPath)]"
exit(0)
}
if ($Unreleased -and ($ChangeLogEntries[$Version].ReleaseStatus -ne $ReleaseStatus))
{
LogWarning "Version is already present in change log with a release date. Please review [$($PkgProperties.ChangeLogPath)]"
exit(0)
}

if (!$Unreleased -and ($ChangeLogEntries[$Version].ReleaseStatus -ne $UNRELEASED_TAG))
if (!$Unreleased -and ($ChangeLogEntries[$Version].ReleaseStatus -ne $CHANGELOG_UNRELEASED_STATUS))
{
if (Get-Date ($ChangeLogEntries[$Version].ReleaseStatus).Trim("()") -gt Get-Date $ReleaseStatus.Trim("()"))
{
if ((Get-Date ($ChangeLogEntries[$Version].ReleaseStatus).Trim("()")) -gt (Get-Date $ReleaseStatus.Trim("()")))
{
LogWarning "New ReleaseDate is older than existing release date in changelog. Please review [$($PkgProperties.ChangeLogPath)]"
exit(0)
}
LogWarning "New ReleaseDate is older than existing release date in changelog. Please review [$($PkgProperties.ChangeLogPath)]"
exit(0)
}
}
}

$PresentVersionsSorted = [AzureEngSemanticVersion]::SortVersionStrings($ChangeLogEntries.Keys)
$LatestVersion = $PresentVersionsSorted[0]
$PresentVersionsSorted = [AzureEngSemanticVersion]::SortVersionStrings($ChangeLogEntries.Keys)
$LatestVersion = $PresentVersionsSorted[0]

if ($ReplaceVersion)
{
$ChangeLogEntries.Remove($LatestVersion)
$ChangeLogEntries[$Version] = New-ChangeLogEntry -Version $Version -Status $ReleaseStatus
if ($ReplaceVersion)
{
$ChangeLogEntries.Remove($LatestVersion)
$newChangeLogEntry = New-ChangeLogEntry -Version $Version -Status $ReleaseStatus
if ($newChangeLogEntry) {
$ChangeLogEntries[$Version] = $newChangeLogEntry
}
else
{
$ChangeLogEntries[$Version] = New-ChangeLogEntry -Version $Version -Status $ReleaseStatus
else {
LogError "Failed to create new changelog entry"
}
}
else
{
$ChangeLogEntries[$Version] = New-ChangeLogEntry -Version $Version -Status $ReleaseStatus
$newChangeLogEntry = New-ChangeLogEntry -Version $Version -Status $ReleaseStatus
if ($newChangeLogEntry) {
$ChangeLogEntries[$Version] = $newChangeLogEntry
}
else {
LogError "Failed to create new changelog entry"
}
}

Set-ChangeLogContent -ChangeLogLocation $PkgProperties.ChangeLogPath -ChangeLogEntries $ChangeLogEntries
}
else
{
$newChangeLogEntry = New-ChangeLogEntry -Version $Version -Status $ReleaseStatus
if ($newChangeLogEntry) {
$ChangeLogEntries[$Version] = $newChangeLogEntry
}
else {
LogError "Failed to create new changelog entry"
}
}

Set-ChangeLogContent -ChangeLogLocation $PkgProperties.ChangeLogPath -ChangeLogEntries $ChangeLogEntries


2 changes: 1 addition & 1 deletion eng/common/scripts/copy-docs-to-blobstorage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ function Upload-Blobs
}


if (Test-Path Function:PublishGithubIODocsFn)
if ((Get-ChildItem -Path Function: | ? { $_.Name -eq $PublishGithubIODocsFn }).Count -gt 0)
{
&$PublishGithubIODocsFn -DocLocation $DocLocation -PublicArtifactLocation $PublicArtifactLocation
}
Expand Down

0 comments on commit 3c6091b

Please sign in to comment.