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 13, 2020
1 parent 608ed6f commit 3b220d0
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 112 deletions.
68 changes: 47 additions & 21 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 @@ -14,7 +15,7 @@ function Get-ChangeLogEntries {

$changeLogEntries = @{}
if (!(Test-Path $ChangeLogLocation)) {
Write-Error "ChangeLog[${ChangeLogLocation}] does not exist"
LogError "ChangeLog[${ChangeLogLocation}] does not exist"
return $null
}

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 @@ -93,7 +98,7 @@ function Confirm-ChangeLogEntry {
$changeLogEntry = Get-ChangeLogEntry -ChangeLogLocation $ChangeLogLocation -VersionString $VersionString

if (!$changeLogEntry) {
Write-Error "ChangeLog[${ChangeLogLocation}] does not have an entry for version ${VersionString}."
LogError "ChangeLog[${ChangeLogLocation}] does not have an entry for version ${VersionString}."
return $false
}

Expand All @@ -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."
LogError "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
10 changes: 6 additions & 4 deletions eng/common/scripts/Package-Properties.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Helper functions for retireving useful information from azure-sdk-for-* repo
. "${PSScriptRoot}\logging.ps1"

class PackageProps
{
[string]$Name
Expand Down Expand Up @@ -81,7 +83,7 @@ function Get-PkgProperties
$serviceDirectoryPath = Join-Path $RepoRoot "sdk" $ServiceDirectory
if (!(Test-Path $serviceDirectoryPath))
{
Write-Error "Service Directory $ServiceDirectory does not exist"
LogError "Service Directory $ServiceDirectory does not exist"
exit 1
}

Expand All @@ -97,15 +99,15 @@ function Get-PkgProperties
}
else
{
Write-Error "The function '$GetPackageInfoFromRepoFn' was not found."
LogError "The function '$GetPackageInfoFromRepoFn' was not found."
}

if ($pkgProps -ne $null)
{
return $pkgProps
}
}
Write-Error "Failed to retrive Properties for $PackageName"
LogError "Failed to retrive Properties for $PackageName"
}

# Takes ServiceName and Repo Root Directory
Expand Down Expand Up @@ -175,7 +177,7 @@ function Get-PkgListFromYml ($ciYmlPath)
}
if ($artifactsInCI -eq $null)
{
Write-Error "Failed to retrive package names in ci $ciYmlPath"
LogError "Failed to retrive package names in ci $ciYmlPath"
}
return $artifactsInCI
}
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
Loading

0 comments on commit 3b220d0

Please sign in to comment.