Skip to content

Commit

Permalink
Merge pull request #86 from theohbrothers/change/use-versions.json-as…
Browse files Browse the repository at this point in the history
…-config-file-in-update-dockerimagevariantsversions-and-add-new-dockerimagevariantspr-package

Change: Use `versions.json` as config file in `Update-DockerImageVariantsVersions` and add `New-DockerImageVariantsPR -Package`
  • Loading branch information
leojonathanoh authored Sep 21, 2023
2 parents 5cbe7ea + 0d002ee commit b7db4d7
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 148 deletions.
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,18 @@ $repo = Clone-TempRepo
cd $repo
# Get generate/definitions/versions.json
Get-DockerImageVariantsVersions
$versionsConfig = Get-DockerImageVariantsVersions
# Set generate/definitions/versions.json
Set-DockerImageVariantsVersions -Versions @( '0.1.0', '0.2.0' ) #-WhatIf
Set-DockerImageVariantsVersions -Versions @{
coolpackage = @{
versionsNewScript = 'Invoke-RestMethod https://example.com/versions.json'
versions = @(
'0.1.0'
'0.2.0'
)
}
} #-WhatIf
# Execute commands
{ git status } | Execute-Command -ErrorAction Stop #-WhatIf
Expand All @@ -49,10 +57,10 @@ $env:GITHUB_TOKEN = 'xxx'
$prs = @(
foreach ($c in $versionsChanged.Values) {
if ($c['kind'] -eq 'new') {
New-DockerImageVariantsPR -Version $c['to'] -Verb add #-WhatIf
New-DockerImageVariantsPR -Package coolpackage -Version $c['to'] -Verb add #-WhatIf
}
if ($c['kind'] -eq 'update') {
New-DockerImageVariantsPR -Version $c['from'] -VersionNew $c['to'] -Verb update #-WhatIf
New-DockerImageVariantsPR -Package coolpackage -Version $c['from'] -VersionNew $c['to'] -Verb update #-WhatIf
}
}
)
Expand All @@ -64,10 +72,10 @@ foreach ($pr in $prs) {
# Update generate/definitions/versions.json and open a PR for each changed version, and merge successful PRs one after another (to prevent merge conflicts)
$env:GITHUB_TOKEN = 'xxx'
$autoMergeResults = Update-DockerImageVariantsVersions -VersionsChanged $versionsChanged -PR -AutoMergeQueue #-WhatIf
$autoMergeResults = Update-DockerImageVariantsVersions -PR -AutoMergeQueue #-WhatIf
# Update generate/definitions/versions.json and open a PR for each changed version, and merge successful PRs one after another (to prevent merge conflicts), and create a tagged release and close milestone
$autoMergeResults = Update-DockerImageVariantsVersions -VersionsChanged $versionsChanged -PR -AutoMergeQueue -AutoRelease -AutoReleaseTagConvention calver #-WhatIf
$autoMergeResults = Update-DockerImageVariantsVersions -VersionsChanged $versionsChanged -PR -AutoMergeQueue -AutoRelease -AutoReleaseTagConvention semver #-WhatIf
$autoMergeResults = Update-DockerImageVariantsVersions -PR -AutoMergeQueue -AutoRelease -AutoReleaseTagConvention calver #-WhatIf
$autoMergeResults = Update-DockerImageVariantsVersions -PR -AutoMergeQueue -AutoRelease -AutoReleaseTagConvention semver #-WhatIf
# Get next tag
$tag = Get-TagNext -TagConvention calver
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Describe "New-DockerImageVariantsPR" -Tag 'Unit' {
ref = 'master'
}
head = [pscustomobject]@{
ref = "enhancement/add-$version-variants"
ref = "enhancement/add-$package-$version-variants"
}
}
}
Expand All @@ -59,11 +59,12 @@ Describe "New-DockerImageVariantsPR" -Tag 'Unit' {
ref = 'master'
}
head = [pscustomobject]@{
ref = "enhancement/bump-$( $Version.Major ).$( $Version.Minor )-variants-to-$( $VersionNew )"
ref = "enhancement/bump-$package-$( $Version.Major ).$( $Version.Minor )-variants-to-$( $VersionNew )"
}
}
}
$env:GITHUB_TOKEN = 'foo'
$package = 'coolpackage'
$version = [version]'1.0.0'
$versionNew = [version]'2.0.0'
}
Expand All @@ -77,7 +78,7 @@ Describe "New-DockerImageVariantsPR" -Tag 'Unit' {
throw "some exception"
}

New-DockerImageVariantsPR -Version $version -Verb add -ErrorVariable err 2>$null 6>$null
New-DockerImageVariantsPR -Package $package -Version $version -Verb add -ErrorVariable err 2>$null 6>$null

$err | Should -Not -Be $null
}
Expand All @@ -88,7 +89,7 @@ Describe "New-DockerImageVariantsPR" -Tag 'Unit' {
}

{
New-DockerImageVariantsPR -Version $version -Verb add -ErrorAction Stop 6>$null
New-DockerImageVariantsPR -Package $package -Version $version -Verb add -ErrorAction Stop 6>$null
} | Should -Throw "some exception"
}

Expand All @@ -98,7 +99,7 @@ Describe "New-DockerImageVariantsPR" -Tag 'Unit' {
Mock Get-GitHubPullRequest {}
Mock New-GitHubPullRequest { Get-AddPR }

$pr = New-DockerImageVariantsPR -Version $version -Verb add -ErrorAction Stop 6>$null
$pr = New-DockerImageVariantsPR -Package $package -Version $version -Verb add -ErrorAction Stop 6>$null

Assert-MockCalled Get-GitHubMilestone -Scope It -Times 1
Assert-MockCalled New-GitHubMilestone -Scope It -Times 1
Expand All @@ -113,7 +114,7 @@ Describe "New-DockerImageVariantsPR" -Tag 'Unit' {
Mock Get-GitHubPullRequest {}
Mock New-GitHubPullRequest { Get-AddPR }

$pr = New-DockerImageVariantsPR -Version $version -Verb add -WhatIf -ErrorVariable err 6>$null
$pr = New-DockerImageVariantsPR -Package $package -Version $version -Verb add -WhatIf -ErrorVariable err 6>$null

Assert-MockCalled Get-GitHubMilestone -Scope It -Times 0
Assert-MockCalled New-GitHubMilestone -Scope It -Times 0
Expand All @@ -129,7 +130,7 @@ Describe "New-DockerImageVariantsPR" -Tag 'Unit' {
Mock Get-GitHubPullRequest { Get-AddPR }
Mock New-GitHubPullRequest {}

$pr = New-DockerImageVariantsPR -Version $version -Verb add -ErrorAction Stop 6>$null
$pr = New-DockerImageVariantsPR -Package $package -Version $version -Verb add -ErrorAction Stop 6>$null

Assert-MockCalled Get-GitHubMilestone -Scope It -Times 1
Assert-MockCalled New-GitHubMilestone -Scope It -Times 0
Expand All @@ -155,7 +156,7 @@ Describe "New-DockerImageVariantsPR" -Tag 'Unit' {
Mock Get-GitHubPullRequest {}
Mock New-GitHubPullRequest { Get-UpdatePR }

$pr = New-DockerImageVariantsPR -Version $version -VersionNew $VersionNew -Verb update -ErrorAction Stop 6>$null
$pr = New-DockerImageVariantsPR -Package $package -Version $version -VersionNew $VersionNew -Verb update -ErrorAction Stop 6>$null

Assert-MockCalled Get-GitHubMilestone -Scope It -Times 1
Assert-MockCalled New-GitHubMilestone -Scope It -Times 1
Expand All @@ -170,7 +171,7 @@ Describe "New-DockerImageVariantsPR" -Tag 'Unit' {
Mock Get-GitHubPullRequest { Get-UpdatePR }
Mock New-GitHubPullRequest {}

$pr = New-DockerImageVariantsPR -Version $version -VersionNew $VersionNew -Verb update -ErrorAction Stop 6>$null
$pr = New-DockerImageVariantsPR -Package $package -Version $version -VersionNew $VersionNew -Verb update -ErrorAction Stop 6>$null

Assert-MockCalled Get-GitHubMilestone -Scope It -Times 1
Assert-MockCalled New-GitHubMilestone -Scope It -Times 0
Expand All @@ -187,7 +188,7 @@ Describe "New-DockerImageVariantsPR" -Tag 'Unit' {
function Cool-Function {}
Mock Cool-Function {}

$pr = New-DockerImageVariantsPR -Version $version -VersionNew $VersionNew -Verb update -CommitPreScriptblock { Cool-Function } -ErrorAction Stop 6>$null
$pr = New-DockerImageVariantsPR -Package $package -Version $version -VersionNew $VersionNew -Verb update -CommitPreScriptblock { Cool-Function } -ErrorAction Stop 6>$null

Assert-MockCalled Cool-Function -Scope It -Times 1
Assert-MockCalled Get-GitHubMilestone -Scope It -Times 1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
function New-DockerImageVariantsPR {
[CmdletBinding(SupportsShouldProcess)]
param (
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$Package
,
[Parameter(Mandatory,Position=0)]
[ValidateNotNullOrEmpty()]
[version]$Version
Expand Down Expand Up @@ -41,14 +45,14 @@ function New-DockerImageVariantsPR {
{ Generate-DockerImageVariants . } | Execute-Command | Write-Host
}
$BRANCH = if ($Verb -eq 'add') {
"enhancement/add-$( $Version.Major ).$( $Version.Minor ).$( $Version.Build )-variants"
"enhancement/add-$Package-$( $Version.Major ).$( $Version.Minor ).$( $Version.Build )-variants"
}elseif ($Verb -eq 'update') {
"enhancement/bump-$( $Version.Major ).$( $Version.Minor )-variants-to-$( $VersionNew )"
"enhancement/bump-$Package-$( $Version.Major ).$( $Version.Minor )-variants-to-$( $VersionNew )"
}
$COMMIT_MSG = if ($Verb -eq 'add') {
"Enhancement: Add $( $Version.Major ).$( $Version.Minor ).$( $Version.Build ) variants"
"Enhancement: Add $Package $( $Version.Major ).$( $Version.Minor ).$( $Version.Build ) variants"
}elseif ($Verb -eq 'update') {
"Enhancement: Bump $( $Version.Major ).$( $Version.Minor ) variants to $( $VersionNew )"
"Enhancement: Bump $Package $( $Version.Major ).$( $Version.Minor ) variants to $( $VersionNew )"
}
$existingBranch = { git rev-parse --verify $BRANCH } | Execute-Command -ErrorAction Continue
if ($existingBranch) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'
Describe "Update-DockerImageVariantsVersions" -Tag 'Unit' {

BeforeEach {
function Get-DockerImageVariantsVersions {}
function Get-VersionsChanged {}
function Execute-Command {
[CmdletBinding(DefaultParameterSetName='Default')]
param (
Expand All @@ -20,7 +22,6 @@ Describe "Update-DockerImageVariantsVersions" -Tag 'Unit' {
Invoke-Command $Command
}
function git {}
function Get-DockerImageVariantsVersions {}
function Set-DockerImageVariantsVersions {
# param ($Versions, $WhatIf)
}
Expand All @@ -38,19 +39,33 @@ Describe "Update-DockerImageVariantsVersions" -Tag 'Unit' {
Context 'Behavior' {

BeforeEach {
$versionsChanged = [ordered]@{
'0.1.1' = @{
from = '0.1.0'
to = '0.1.1'
kind = 'update'
}
'1.2.0' = @{
from = '1.2.0'
to = '1.2.0'
kind = 'new'
Mock Get-VersionsChanged {
[ordered]@{
'0.1.1' = @{
from = '0.1.0'
to = '0.1.1'
kind = 'update'
}
'1.2.0' = @{
from = '1.2.0'
to = '1.2.0'
kind = 'new'
}
}
}
Mock Get-DockerImageVariantsVersions { @( '0.1.0' ) }
Mock Get-DockerImageVariantsVersions {
@"
{
"somepackage": {
"versions": [
"0.1.0"
],
"versionsNewScript": "@( 'some versions' )"
}
}
"@ | ConvertFrom-Json

}
Mock Set-DockerImageVariantsVersions {}
Mock New-DockerImageVariantsPR {
[PSCustomObject]@{
Expand All @@ -68,45 +83,35 @@ Describe "Update-DockerImageVariantsVersions" -Tag 'Unit' {
}
}

It 'Errors on empty ordered hashtable' {
$versionsChanged = [ordered]@{}

{
Update-DockerImageVariantsVersions -VersionsChanged $versionsChanged 6>$null
} | Should -Throw

{
$versionsChanged | Update-DockerImageVariantsVersions -ErrorAction Stop 6>$null
} | Should -Throw
}

It 'Errors (non-terminating)' {
Mock Get-DockerImageVariantsVersions {
throw
}

Update-DockerImageVariantsVersions -VersionsChanged $versionsChanged -ErrorVariable err 6>$null 2>$null
Update-DockerImageVariantsVersions -ErrorVariable err 6>$null 2>$null

$err | Should -Not -Be $null
}

It 'Does nothing when there are no changed versions' {
$versionsChanged = [ordered]@{
'0.1.0' = @{
from = '0.1.0'
to = '0.1.0'
kind = 'existing'
}
'1.2.0' = @{
from = '1.2.0'
to = '1.2.0'
kind = 'existing'
Mock Get-VersionsChanged {
[ordered]@{
'0.1.0' = @{
from = '0.1.0'
to = '0.1.0'
kind = 'existing'
}
'1.2.0' = @{
from = '1.2.0'
to = '1.2.0'
kind = 'existing'
}
}
}

Update-DockerImageVariantsVersions -VersionsChanged $versionsChanged 6>$null
Update-DockerImageVariantsVersions 6>$null

Assert-MockCalled Get-DockerImageVariantsVersions -Scope It -Times 0
Assert-MockCalled Get-DockerImageVariantsVersions -Scope It -Times 1
Assert-MockCalled Set-DockerImageVariantsVersions -Scope It -Times 0
Assert-MockCalled New-DockerImageVariantsPR -Scope It -Times 0
Assert-MockCalled Automerge-DockerImageVariantsPR -Scope It -Times 0
Expand All @@ -119,34 +124,24 @@ Describe "Update-DockerImageVariantsVersions" -Tag 'Unit' {
}

{
$versionsChanged | Update-DockerImageVariantsVersions -ErrorAction Stop 6>$null
Update-DockerImageVariantsVersions -ErrorAction Stop 6>$null
} | Should -Throw "some exception"
}

It 'Updates versions.json (pipeline)' {
$versionsChanged | Update-DockerImageVariantsVersions 6>$null

Assert-MockCalled Get-DockerImageVariantsVersions -Scope It -Times 2
Assert-MockCalled Set-DockerImageVariantsVersions -Scope It -Times 2
Assert-MockCalled New-DockerImageVariantsPR -Scope It -Times 0
Assert-MockCalled Automerge-DockerImageVariantsPR -Scope It -Times 0
Assert-MockCalled New-Release -Scope It -Times 0
}

It 'Updates versions.json' {
Update-DockerImageVariantsVersions -VersionsChanged $versionsChanged 6>$null
Update-DockerImageVariantsVersions 6>$null

Assert-MockCalled Get-DockerImageVariantsVersions -Scope It -Times 2
Assert-MockCalled Get-DockerImageVariantsVersions -Scope It -Times 1
Assert-MockCalled Set-DockerImageVariantsVersions -Scope It -Times 2
Assert-MockCalled New-DockerImageVariantsPR -Scope It -Times 0
Assert-MockCalled Automerge-DockerImageVariantsPR -Scope It -Times 0
Assert-MockCalled New-Release -Scope It -Times 0
}

It 'Opens PRs with -PR' {
$prs = Update-DockerImageVariantsVersions -VersionsChanged $versionsChanged -PR 6>$null
$prs = Update-DockerImageVariantsVersions -PR 6>$null

Assert-MockCalled Get-DockerImageVariantsVersions -Scope It -Times 2
Assert-MockCalled Get-DockerImageVariantsVersions -Scope It -Times 1
Assert-MockCalled Set-DockerImageVariantsVersions -Scope It -Times 2
Assert-MockCalled New-DockerImageVariantsPR -Scope It -Times 2
Assert-MockCalled Automerge-DockerImageVariantsPR -Scope It -Times 0
Expand All @@ -156,9 +151,9 @@ Describe "Update-DockerImageVariantsVersions" -Tag 'Unit' {
}

It 'Automerges PRs (success)' {
$autoMergeResults = Update-DockerImageVariantsVersions -VersionsChanged $versionsChanged -PR -AutoMergeQueue 6>$null
$autoMergeResults = Update-DockerImageVariantsVersions -PR -AutoMergeQueue 6>$null

Assert-MockCalled Get-DockerImageVariantsVersions -Scope It -Times 2
Assert-MockCalled Get-DockerImageVariantsVersions -Scope It -Times 1
Assert-MockCalled Set-DockerImageVariantsVersions -Scope It -Times 2
Assert-MockCalled New-DockerImageVariantsPR -Scope It -Times 2
Assert-MockCalled Automerge-DockerImageVariantsPR -Scope It -Times 2
Expand All @@ -175,9 +170,9 @@ Describe "Update-DockerImageVariantsVersions" -Tag 'Unit' {
throw
}

$autoMergeResults = Update-DockerImageVariantsVersions -VersionsChanged $versionsChanged -PR -AutoMergeQueue -ErrorVariable err -ErrorAction Continue 6>$null 2>$null 3>$null
$autoMergeResults = Update-DockerImageVariantsVersions -PR -AutoMergeQueue -ErrorVariable err -ErrorAction Continue 6>$null 2>$null 3>$null

Assert-MockCalled Get-DockerImageVariantsVersions -Scope It -Times 2
Assert-MockCalled Get-DockerImageVariantsVersions -Scope It -Times 1
Assert-MockCalled Set-DockerImageVariantsVersions -Scope It -Times 2
Assert-MockCalled New-DockerImageVariantsPR -Scope It -Times 2
Assert-MockCalled Automerge-DockerImageVariantsPR -Scope It -Times 2
Expand All @@ -187,9 +182,9 @@ Describe "Update-DockerImageVariantsVersions" -Tag 'Unit' {
}

It 'Automerges PRs and autoreleases' {
$returns = Update-DockerImageVariantsVersions -VersionsChanged $versionsChanged -PR -AutoMergeQueue -AutoRelease -AutoReleaseTagConvention 'semver' 6>$null
$returns = Update-DockerImageVariantsVersions -PR -AutoMergeQueue -AutoRelease -AutoReleaseTagConvention 'semver' 6>$null

Assert-MockCalled Get-DockerImageVariantsVersions -Scope It -Times 2
Assert-MockCalled Get-DockerImageVariantsVersions -Scope It -Times 1
Assert-MockCalled Set-DockerImageVariantsVersions -Scope It -Times 2
Assert-MockCalled New-DockerImageVariantsPR -Scope It -Times 2
Assert-MockCalled Automerge-DockerImageVariantsPR -Scope It -Times 2
Expand All @@ -204,9 +199,9 @@ Describe "Update-DockerImageVariantsVersions" -Tag 'Unit' {
# Mock Automerge-DockerImageVariantsPR {} #-ParameterFilter { $PR -and $WhatIf }
# Mock New-Release {} #-ParameterFilter { $PR -and $WhatIf }

$returns = Update-DockerImageVariantsVersions -VersionsChanged $versionsChanged -PR -AutoMergeQueue -AutoRelease -AutoReleaseTagConvention 'semver' -WhatIf -ErrorVariable err 6>$null
$returns = Update-DockerImageVariantsVersions -PR -AutoMergeQueue -AutoRelease -AutoReleaseTagConvention 'semver' -WhatIf -ErrorVariable err 6>$null

Assert-MockCalled Get-DockerImageVariantsVersions -Scope It -Times 2
Assert-MockCalled Get-DockerImageVariantsVersions -Scope It -Times 1
Assert-MockCalled Set-DockerImageVariantsVersions -Scope It -Times 2 #-ParameterFilter { $Versions -and $WhatIf }
Assert-MockCalled New-DockerImageVariantsPR -Scope It -Times 2 #-ParameterFilter { $Version -and $Verb -and $WhatIf }
Assert-MockCalled Automerge-DockerImageVariantsPR -Scope It -Times 2 #-ParameterFilter { $PR -and $WhatIf }
Expand Down
Loading

0 comments on commit b7db4d7

Please sign in to comment.