From 5c7948d277caf52e4ba92d0daae8310a97c52722 Mon Sep 17 00:00:00 2001 From: Gui <12204241+guirava@users.noreply.github.com> Date: Sat, 12 Oct 2024 18:33:19 -0700 Subject: [PATCH] util script update --- Utils/admin/New-RscSdkRelease.ps1 | 82 ++++-------------------- Utils/admin/Set-RscSdkVersion.ps1 | 4 +- Utils/admin/Test-RscSdkCandidate.ps1 | 85 +++++++++++++++++++++++++ Utils/admin/Update-RscSdkMainBranch.ps1 | 4 +- 4 files changed, 102 insertions(+), 73 deletions(-) create mode 100644 Utils/admin/Test-RscSdkCandidate.ps1 diff --git a/Utils/admin/New-RscSdkRelease.ps1 b/Utils/admin/New-RscSdkRelease.ps1 index 81649b39a..e14d225a5 100644 --- a/Utils/admin/New-RscSdkRelease.ps1 +++ b/Utils/admin/New-RscSdkRelease.ps1 @@ -20,7 +20,7 @@ This script will: 7. Create a new GitHub release .EXAMPLE -.\Utils\New-RscSdkRelease.ps1 +.\Utils\admin\New-RscSdkRelease.ps1 #> param( [switch]$NotDry = $false @@ -48,75 +48,19 @@ function RunIfNotDry { } } -# Change to the root of the repository -Set-Location $PSScriptRoot\.. - -# 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 -$changelogScriptPath = ".\Utils\Get-RscSdkLatestChangelog.ps1" -$changelogLatest = & $changelogScriptPath -if ( -Not $changelogLatest ) { - throw "Failed to get the latest changelog entry." -} -$versionTag = $changelogLatest.latestVersionTag -$versionEntry = $changelogLatest.latestVersionEntry - -# $versionTag must have the format "Version_.[]" -if ($versionTag -notmatch "^Version_\d+\.\d+.*$") { - throw "Latest version in ./CHANGELOG.md is invalid. It must have the format 'Version_.'." -} - -# extract the module version from $versionTag -$changelogSemanticVersion = $versionTag -replace 'Version_', '' - -# Check if the version tag matches the module version -$psd1SemanticVersion = (Import-PowerShellDataFile .\RubrikSecurityCloud\RubrikSecurityCloud.PowerShell\RubrikSecurityCloud.psd1).ModuleVersion - -if ( $changelogSemanticVersion -ne $psd1SemanticVersion ) { - Write-Host @" - -Version mismatch between +$SdkRoot = Join-Path -Path $PSScriptRoot -ChildPath '..\..' -Resolve -./CHANGELOG.md - -and - -./RubrikSecurityCloud/RubrikSecurityCloud.PowerShell/RubrikSecurityCloud.psd1 - -The latest entry in ./CHANGELOG.md must have the format "Version M.m", -where "M.m" is the ModuleVersion field in -./RubrikSecurityCloud/RubrikSecurityCloud.PowerShell/RubrikSecurityCloud.psd1 . - -./CHANGELOG.md shows: - -$changelogSemanticVersion - -./RubrikSecurityCloud/RubrikSecurityCloud.PowerShell/RubrikSecurityCloud.psd1 shows: - -$psd1SemanticVersion - -"@ -ForegroundColor Yellow - throw "Version mismatch. CHANGELOG: $changelogSemanticVersion, psd1: $psd1SemanticVersion." +$candidate = & "$SdkRoot\Utils\admin\Test-RscSdkCandidate.ps1" +if (-Not $candidate) { + throw "Could not gather info about current branch." } +$versionEntry = $candidate.VersionEntry +$versionTag = $candidate.VersionTag -Write-Host "Latest version tag: " -NoNewline -Write-Host $versionTag -ForegroundColor Cyan -Write-Host "Latest version entry:" -Write-Host $versionEntry -ForegroundColor Cyan +# Change to the root of the repository +Set-Location $SdkRoot -# Make sure this version tag is not already published: -$existingTag = gh release list --json tagName --jq ".[] | select(.tagName == `"$versionTag`")" -if ($null -ne $existingTag) { - $existingTag = $existingTag.Trim() -} -if ( -not [string]::IsNullOrEmpty($existingTag)) { - throw "Version tag $versionTag already exists in the GitHub repository." -} +$sourceBranch = git rev-parse --abbrev-ref HEAD # Update main branch $CommitMessage = "" @@ -125,7 +69,7 @@ RunIfNotDry { } # Passing an empty CommitMessage makes it a dry run -.\Utils\Update-RscSdkMainBranch.ps1 -CommitMessage "$CommitMessage" -StayOnMain +.\Utils\admin\Update-RscSdkMainBranch.ps1 -CommitMessage "$CommitMessage" -StayOnMain # Create a new GitHub release RunIfNotDry { @@ -144,8 +88,8 @@ git checkout $sourceBranch # Prepare devel branch for further development RunIfNotDry { if ($sourceBranch -eq 'devel') { - Set-Location $PSScriptRoot\.. - .\Utils\New-RscSdkChangeLogEntry.ps1 -Commit + Set-Location $PSScriptRoot\..\.. + .\Utils\admin\New-RscSdkChangeLogEntry.ps1 -Commit git push origin devel } } diff --git a/Utils/admin/Set-RscSdkVersion.ps1 b/Utils/admin/Set-RscSdkVersion.ps1 index d379b7ff9..1a2b3069f 100644 --- a/Utils/admin/Set-RscSdkVersion.ps1 +++ b/Utils/admin/Set-RscSdkVersion.ps1 @@ -37,7 +37,7 @@ function updateModuleVersion { # branch version. # Also, Test-RscSdkRelease will throw an error if the current release # is corrupted (e.g. the main branch's version is not what's on the gallery). -$mainSdkVersion = & "$SdkRoot\Utils\Test-RscSdkRelease.ps1" +$mainSdkVersion = & "$SdkRoot\Utils\admin\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 @@ -48,4 +48,4 @@ if ($NewVersion -eq $mainSdkVersion -and -not $Force) { updateModuleVersion # Update CHANGELOG.md -& "$PSScriptRoot\Set-RscSdkLatestChangelog.ps1" -Version $NewVersion \ No newline at end of file +& "$SdkRoot\Utils\admin\Set-RscSdkLatestChangelog.ps1" -Version $NewVersion \ No newline at end of file diff --git a/Utils/admin/Test-RscSdkCandidate.ps1 b/Utils/admin/Test-RscSdkCandidate.ps1 new file mode 100644 index 000000000..358bf9ab8 --- /dev/null +++ b/Utils/admin/Test-RscSdkCandidate.ps1 @@ -0,0 +1,85 @@ +<# +.SYNOPSIS + Test if the current branch could be a candidate for a release. + +.DESCRIPTION +Checks that: +- The current branch is not the 'main' branch. +- The latest version tag in the CHANGELOG.md file matches the module version in the psd1 file. +- The version tag is not already published on the GitHub repository. +- The version tag has the format "Version_.[]". +#> + +$SdkRoot = Join-Path -Path $PSScriptRoot -ChildPath '..\..' -Resolve + +# 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 +$changelogLatest = & "$SdkRoot\Utils\admin\Get-RscSdkLatestChangelog.ps1" +if ( -Not $changelogLatest ) { + throw "Failed to get the latest changelog entry." +} +$versionTag = $changelogLatest.latestVersionTag +$versionEntry = $changelogLatest.latestVersionEntry + +# extract the module version from $versionTag +$changelogSemanticVersion = $versionTag -replace 'Version_', '' + +# $versionTag must have the format "Version_.[]" +if ($versionTag -notmatch "^Version_\d+\.\d+.*$") { + throw "Latest version '$changelogSemanticVersion' in ./CHANGELOG.md is invalid. It must have the format 'Version_.'." +} + +# Check if the version tag matches the module version +$psd1SemanticVersion = (& "$SdkRoot\Utils\Get-RscSdkVersion.ps1") + +if ( $changelogSemanticVersion -ne $psd1SemanticVersion ) { + Write-Host @" + +Version mismatch between + +./CHANGELOG.md + +and + +./RubrikSecurityCloud/RubrikSecurityCloud.PowerShell/RubrikSecurityCloud.psd1 + +The latest entry in ./CHANGELOG.md must have the format "Version M.m", +where "M.m" is the ModuleVersion field in +./RubrikSecurityCloud/RubrikSecurityCloud.PowerShell/RubrikSecurityCloud.psd1 . + +./CHANGELOG.md shows: + +$changelogSemanticVersion + +./RubrikSecurityCloud/RubrikSecurityCloud.PowerShell/RubrikSecurityCloud.psd1 shows: + +$psd1SemanticVersion + +"@ -ForegroundColor Yellow + throw "Version mismatch. CHANGELOG: $changelogSemanticVersion, psd1: $psd1SemanticVersion." +} + +Write-Host "Latest version tag: " -NoNewline +Write-Host $versionTag -ForegroundColor Cyan +Write-Host "Latest version entry:" +Write-Host $versionEntry -ForegroundColor Cyan + +# Make sure this version tag is not already published: +$existingTag = gh release list --json tagName --jq ".[] | select(.tagName == `"$versionTag`")" +if ($null -ne $existingTag) { + $existingTag = $existingTag.Trim() +} +if ( -not [string]::IsNullOrEmpty($existingTag)) { + throw "Version tag $versionTag already exists in the GitHub repository." +} + +[PSCustomObject]@{ + versionTag = $versionTag + versionEntry = $versionEntry + semanticVersion = $psd1SemanticVersion +} \ No newline at end of file diff --git a/Utils/admin/Update-RscSdkMainBranch.ps1 b/Utils/admin/Update-RscSdkMainBranch.ps1 index 02842e989..a84a0cd1a 100644 --- a/Utils/admin/Update-RscSdkMainBranch.ps1 +++ b/Utils/admin/Update-RscSdkMainBranch.ps1 @@ -21,7 +21,7 @@ If not given, the script will not commit or push the changes to the main branch, so it is effectively a dry run. .EXAMPLE -.\Utils\Update-RscSdkMainBranch.ps1 -SkipBuild +.\Utils\admin\Update-RscSdkMainBranch.ps1 -SkipBuild #> param( [switch]$SkipBuild = $false, @@ -30,7 +30,7 @@ param( ) # Change to the root of the repository -Set-Location $PSScriptRoot\.. +Set-Location $PSScriptRoot\..\.. # Check for a clean repo $gitStatus = git status --porcelain