Skip to content

Commit

Permalink
Test-RscSdkRelease
Browse files Browse the repository at this point in the history
  • Loading branch information
guirava committed Sep 28, 2024
1 parent 2813d44 commit 24f8101
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 1 deletion.
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
```
30 changes: 30 additions & 0 deletions Utils/Set-RscSdkVersion.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
param (
[Parameter(Mandatory = $true)]
[string]$NewVersion
)

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

# Stop on error
$ErrorActionPreference = "Stop"

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

# Path to the PSD1 file
$moduleFile = ".\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"
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.
.DESCRIPTION
ADMIN USE ONLY.
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

0 comments on commit 24f8101

Please sign in to comment.