Skip to content

Commit

Permalink
Utils update
Browse files Browse the repository at this point in the history
  • Loading branch information
guirava committed Sep 29, 2024
1 parent 2039143 commit 80b82df
Show file tree
Hide file tree
Showing 8 changed files with 294 additions and 26 deletions.
20 changes: 20 additions & 0 deletions Utils/Get-RscSdkLatestChangelog.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
<#
.SYNOPSIS
Get the latest version entry from the CHANGELOG.md file.
.DESCRIPTION
CHANGELOG.md must have the following format:
- Starts with '# Changelog'
- then a blank line
- then the latest version entry '## Version <version>'
This script reads the CHANGELOG.md file and extracts the latest version entry.
For example, if the latest version entry is '## Version 1.9', the output will be:
@{
latestVersion = "1.9"
latestVersionTag = "Version_1.9"
latestVersionEntry = "..Text blurb for version 1.9.."
}
#>
[CmdletBinding()]
param()

$changelogPath = Join-Path -Path $PSScriptRoot\.. -ChildPath "CHANGELOG.md"
$changelogContent = Get-Content -Path $changelogPath -Raw

Expand All @@ -14,9 +32,11 @@ if ($hits.Success -and $hits.Groups.Count -gt 1) {
$latestVersionEntry = $hits.Groups[1].Value
$latestVersionEntry = $latestVersionEntry -replace '## ', '' -replace "`r`n", "`n"
$latestVersionTag = ($latestVersionEntry -split "`n")[0] -replace ' ', '_'
$latestVersion = $latestVersionTag -replace 'Version_', ''

# Output the findings
[PSCustomObject]@{
latestVersion = $latestVersion
latestVersionTag = $latestVersionTag
latestVersionEntry = $latestVersionEntry
}
Expand Down
3 changes: 2 additions & 1 deletion Utils/Get-RscSdkVersion.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ $ErrorActionPreference = "Stop"
# Uncomment this to enable Write-Debug output
# $DebugPreference = "Continue"

# Path to the PSD1 file
$moduleFile = ".\RubrikSecurityCloud\RubrikSecurityCloud.PowerShell\RubrikSecurityCloud.psd1"

$moduleInfo = Import-PowerShellDataFile $moduleFile
return $moduleInfo.ModuleVersion
```
18 changes: 16 additions & 2 deletions Utils/New-RscSdkChangelogEntry.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,29 @@
.SYNOPSIS
Update CHANGELOG.md with a new empty version entry
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $false)]
[string]$Version = "TBD",
[Parameter(Mandatory = $false)]
[switch]$Commit = $false
)

$latestVersion = (.\Utils\Get-RscSdkLatestChangelog.ps1).latestVersion

if ($Version -eq $latestVersion) {
Write-Host "Error: The new version $Version is the same as the latest version." -ForegroundColor Red
throw "New version is the same as the latest version"
}

$body = "`nNew Features:`n`nFixes:`n`nBreaking Changes:`n"
$changelogPath = Join-Path -Path $PSScriptRoot\.. -ChildPath "CHANGELOG.md"
$changelogContent = Get-Content -Path $changelogPath -Raw
$changelogContent = $changelogContent -replace "`r`n", "`n"
$changelogContent = $changelogContent -replace "(?<=# Changelog`n)", "`n## ~ Upcoming Version ~`n`nNew Features:`n`nFixes:`n`nBreaking Changes:`n"
$changelogContent = $changelogContent -replace "(?<=# Changelog`n)", "`n## Version $Version`n$body"
Set-Content -Path $changelogPath -Value $changelogContent

if ($Commit) {
git add $changelogPath
git commit -m "Prepare for next development iteration"
git commit -m "Update changelog with new version entry"
}
18 changes: 10 additions & 8 deletions Utils/New-RscSdkRelease.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ function RunIfNotDry {
# Change to the root of the repository
Set-Location $PSScriptRoot\..

# bail out if not on the devel branch
$currentBranch = git rev-parse --abbrev-ref HEAD
if ($currentBranch -ne 'devel') {
throw "You are not on the 'devel' branch. Current branch is '$currentBranch'."
# bail out if on the main branch
$sourceBranch = git rev-parse --abbrev-ref HEAD
if ($sourceBranch -eq 'main') {
throw "You are on the 'main' branch. Start from the source branch."
}

# Get the latest changelog entry
Expand Down Expand Up @@ -139,10 +139,12 @@ if ($script:NotDry) {

# Prepare devel branch for further development
RunIfNotDry {
git checkout devel
Set-Location $PSScriptRoot\..
.\Utils\New-RscSdkChangeLogEntry.ps1 -Commit
git push origin devel
git checkout $sourceBranch
if ($sourceBranch -eq 'devel') {
Set-Location $PSScriptRoot\..
.\Utils\New-RscSdkChangeLogEntry.ps1 -Commit
git push origin devel
}
}

Write-Host "Done." -ForegroundColor Green
Expand Down
51 changes: 51 additions & 0 deletions Utils/Set-RscSdkLatestChangelog.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<#
.SYNOPSIS
Updates the latest version entry in the CHANGELOG.md file.
.DESCRIPTION
This script updates the latest version entry in the CHANGELOG.md file.
It takes a -Version parameter, replacing the current version in the first version entry.
For example, if the provided version is '1.10', the latest version entry will be updated to '## Version 1.10'.
.PARAMETER Version
The new version number to set as the latest version entry.
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]$Version
)

# Path to CHANGELOG.md
$changelogPath = Join-Path -Path $PSScriptRoot\.. -ChildPath "CHANGELOG.md"

# Ensure the changelog file exists
if (-not (Test-Path -Path $changelogPath)) {
Write-Error "CHANGELOG.md not found at path: $changelogPath"
return
}

# Read the changelog file
$changelogContent = Get-Content -Path $changelogPath -Raw

# Normalize line endings to Unix style
$changelogContent = $changelogContent -replace "`r`n", "`n"

# Regex to match the latest version entry
$hits = [regex]::Match($changelogContent, "(## Version .+?)(?=`n## Version |$)", 'Singleline')
if ($hits.Success -and $hits.Groups.Count -gt 1) {
$latestVersionTitle = ($hits.Groups[1].Value -split "`n")[0]
Write-Host "latestVersionTitle: $latestVersionTitle"
$newVersionTitle = "## Version $Version"

# Replace the old version entry with the new version entry in the content
$updatedContent = $changelogContent -replace [regex]::Escape($latestVersionTitle), $newVersionTitle

# Write the updated content back to CHANGELOG.md
Set-Content -Path $changelogPath -Value $updatedContent.TrimEnd()

Write-Host "Successfully updated CHANGELOG.md to Version $Version."
} else {
Write-Error "Failed to find the latest version entry in CHANGELOG.md."
}
45 changes: 45 additions & 0 deletions Utils/Set-RscSdkVersion.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
param (
[Parameter(Mandatory = $true)]
[string]$NewVersion,
[Parameter(Mandatory = $false)]
[switch]$Force
)

# Stop on error
$ErrorActionPreference = "Stop"

# Uncomment this to enable Write-Debug output
# $DebugPreference = "Continue"

function updateModuleVersion {

# Path to the PSD1 file
$moduleFile = "$PSScriptRoot\..\RubrikSecurityCloud\RubrikSecurityCloud.PowerShell\RubrikSecurityCloud.psd1"

# Read the existing psd1 file as text
$psd1Content = Get-Content -Path $ModuleFile -Raw

# Use regex to find and replace the ModuleVersion value
$updatedContent = $psd1Content -replace 'ModuleVersion\s*=\s*''([^'']+)''', "ModuleVersion = '$NewVersion'"

# Trim any extra newlines to prevent adding a new line
$updatedContent = $updatedContent.TrimEnd()

# Save the updated content back to the psd1 file
Set-Content -Path $ModuleFile -Value $updatedContent

Write-Host "Updated ModuleVersion to $NewVersion in $ModuleFile"
}

$mainSdkVersion = & "$PSScriptRoot\Test-RscSdkRelease.ps1"

if ($NewVersion -eq $mainSdkVersion -and -not $Force) {
Write-Host "Error: The new version $NewVersion is the same as the main branch version." -ForegroundColor Red
throw "New version is the same as the main branch version"
}

# Update PSD file
updateModuleVersion

# Update CHANGELOG.md
& "$PSScriptRoot\Set-RscSdkLatestChangelog.ps1" -Version $NewVersion
141 changes: 141 additions & 0 deletions Utils/Test-RscSdkRelease.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<#
.SYNOPSIS
ADMIN USE ONLY. Test if the SDK is released correctly. (No-op if run as non-admin.)
.DESCRIPTION
ADMIN USE ONLY. (No-op if run as non-admin.)
This script tests if the SDK is released correctly.
The latest published version on The PowerShell Gallery must match:
- The SDK module version on the main branch.
- The latest version tag in the CHANGELOG.md file on the main branch.
- The latest release tag on the GitHub repository.
.OUTPUTS
The published SDK version as a string (example: "1.9") if all
checks pass successfully. Otherwise, an empty string is returned.
#>
[CmdletBinding()]
param()

# Change to the root of the repository
$originalCwd = $PWD
Set-Location $PSScriptRoot\..

# Stop on error
$ErrorActionPreference = "Stop"

# Uncomment this to enable Write-Debug output
# $DebugPreference = "Continue"

# Private function to retrieve information from the 'main' branch
function RetrieveMainBranchInfo {
# Store the initial branch to always return to it
$initialBranch = git rev-parse --abbrev-ref HEAD
$mainSdkVersion = ""
try {
# Switch to the main branch
if ( $initialBranch -ne "main" ) {
git checkout main | Out-Null
if ($LASTEXITCODE -ne 0) {
Write-Host "Error: Failed to switch to the 'main' branch." -ForegroundColor Red
throw "Failed to switch to main branch"
}
}
git pull --rebase | Out-Null
if ($LASTEXITCODE -ne 0) {
Write-Host "Error: Failed to pull and rebase on the 'main' branch." -ForegroundColor Red
throw "Failed to pull and rebase"
}

# Get the SDK version on the main branch
$mainSdkVersion = .\Utils\Get-RscSdkVersion.ps1
Write-Host "SDK Version on main branch: $mainSdkVersion" -ForegroundColor Blue

# Get the latest changelog version
$mainChangelogLatestVersionTag = (.\Utils\Get-RscSdkLatestChangelog.ps1).latestVersionTag
Write-Host "Latest version tag from changelog on main branch: $mainChangelogLatestVersionTag" -ForegroundColor Blue


# Ensure the changelog version matches the SDK version
$expectedTag = "Version_$mainSdkVersion"
if ($mainChangelogLatestVersionTag -ne $expectedTag) {
Write-Host "Error: Changelog version tag mismatch on main branch." -ForegroundColor Red
Write-Host "Expected: $expectedTag, Found: $mainChangelogLatestVersionTag" -ForegroundColor Red
throw "Changelog version mismatch on main branch"
}
}
finally {
# Switch back to the devel branch, regardless of success or failure
$currentBranch = git rev-parse --abbrev-ref HEAD
if ($currentBranch -ne $initialBranch) {
Write-Host "Switching back to the ' $initialBranch' branch..."
git checkout $initialBranch | Out-Null
if ($LASTEXITCODE -ne 0) {
Write-Host "Error: Failed to switch back to the ' $initialBranch' branch." -ForegroundColor Red
}
}
}
return $mainSdkVersion
}

function Test-RscSdkRelease {
try {
$statusOutput = git status --porcelain
if ($statusOutput) {
Write-Host "Error: Your working directory has pending changes." -ForegroundColor Red
Write-Host "Please commit, stash, or discard changes before proceeding." -ForegroundColor Red
throw "Pending changes in working directory"
}

# Get the latest released version from the PowerShell Gallery
Write-Host "Checking latest release from the PowerShell Gallery..."
$moduleInfo = Find-Module -Name RubrikSecurityCloud -Repository PSGallery
if ($moduleInfo) {
$latestGalleryRelease = $moduleInfo.Version
Write-Host "Latest release version from the PowerShell Gallery: $latestGalleryRelease" -ForegroundColor Blue
}
else {
Write-Host "Error: Failed to fetch the latest release from the PowerShell Gallery." -ForegroundColor Red
throw "PowerShell Gallery release fetch failed"
}

# Get the latest released version from GitHub
Write-Host "Checking latest release from GitHub repository..."
$latestGitHubRelease = gh release view --json tagName --jq '.tagName'
if ($LASTEXITCODE -ne 0) {
Write-Host "Error: Failed to fetch the latest release from GitHub." -ForegroundColor Red
throw "GitHub release fetch failed"
}
Write-Host "Latest release version from GitHub: $latestGitHubRelease" -ForegroundColor Blue

# Get the SDK version on the main branch
$mainSdkVersion = RetrieveMainBranchInfo

# Ensure the GitHub latest release matches the main branch SDK version
if ($latestGitHubRelease -ne "Version_$mainSdkVersion") {
Write-Host "Error: Latest GitHub release version does not match the main branch's SDK version." -ForegroundColor Red
Write-Host "Expected: Version_$mainSdkVersion, Found: $latestGitHubRelease" -ForegroundColor Red
throw "GitHub release version mismatch"
}

# Ensure the PowerShell Gallery latest release matches the main branch SDK version
if ($latestGalleryRelease -ne $mainSdkVersion) {
Write-Host "Error: Latest PowerShell Gallery release version does not match the main branch's SDK version." -ForegroundColor Red
Write-Host "Expected: $mainSdkVersion, Found: $latestGalleryRelease" -ForegroundColor Red
throw "PowerShell Gallery release version mismatch"
}

}
catch {
Write-Host "An error occurred: $_" -ForegroundColor Red
return ""
}
Write-Host "All checks passed successfully." -ForegroundColor Green
return $mainSdkVersion
}

# Call the function
Test-RscSdkRelease

# Return to the original directory
Set-Location $originalCwd
Loading

0 comments on commit 80b82df

Please sign in to comment.