diff --git a/README.md b/README.md index b3eab38..a112e14 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 } } ) @@ -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 diff --git a/src/Generate-DockerImageVariantsHelpers/public/New-DockerImageVariantsPR.Tests.ps1 b/src/Generate-DockerImageVariantsHelpers/public/New-DockerImageVariantsPR.Tests.ps1 index ff3a4be..9be2caa 100644 --- a/src/Generate-DockerImageVariantsHelpers/public/New-DockerImageVariantsPR.Tests.ps1 +++ b/src/Generate-DockerImageVariantsHelpers/public/New-DockerImageVariantsPR.Tests.ps1 @@ -48,7 +48,7 @@ Describe "New-DockerImageVariantsPR" -Tag 'Unit' { ref = 'master' } head = [pscustomobject]@{ - ref = "enhancement/add-$version-variants" + ref = "enhancement/add-$package-$version-variants" } } } @@ -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' } @@ -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 } @@ -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" } @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/src/Generate-DockerImageVariantsHelpers/public/New-DockerImageVariantsPR.ps1 b/src/Generate-DockerImageVariantsHelpers/public/New-DockerImageVariantsPR.ps1 index b5de90f..73e94f2 100644 --- a/src/Generate-DockerImageVariantsHelpers/public/New-DockerImageVariantsPR.ps1 +++ b/src/Generate-DockerImageVariantsHelpers/public/New-DockerImageVariantsPR.ps1 @@ -1,6 +1,10 @@ function New-DockerImageVariantsPR { [CmdletBinding(SupportsShouldProcess)] param ( + [Parameter()] + [ValidateNotNullOrEmpty()] + [string]$Package + , [Parameter(Mandatory,Position=0)] [ValidateNotNullOrEmpty()] [version]$Version @@ -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) { diff --git a/src/Generate-DockerImageVariantsHelpers/public/Update-DockerImageVariantsVersions.Tests.ps1 b/src/Generate-DockerImageVariantsHelpers/public/Update-DockerImageVariantsVersions.Tests.ps1 index fc67a5f..4135806 100644 --- a/src/Generate-DockerImageVariantsHelpers/public/Update-DockerImageVariantsVersions.Tests.ps1 +++ b/src/Generate-DockerImageVariantsHelpers/public/Update-DockerImageVariantsVersions.Tests.ps1 @@ -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 ( @@ -20,7 +22,6 @@ Describe "Update-DockerImageVariantsVersions" -Tag 'Unit' { Invoke-Command $Command } function git {} - function Get-DockerImageVariantsVersions {} function Set-DockerImageVariantsVersions { # param ($Versions, $WhatIf) } @@ -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]@{ @@ -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 @@ -119,24 +124,14 @@ 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 @@ -144,9 +139,9 @@ Describe "Update-DockerImageVariantsVersions" -Tag 'Unit' { } 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 @@ -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 @@ -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 @@ -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 @@ -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 } diff --git a/src/Generate-DockerImageVariantsHelpers/public/Update-DockerImageVariantsVersions.ps1 b/src/Generate-DockerImageVariantsHelpers/public/Update-DockerImageVariantsVersions.ps1 index 0cb330c..2e39ae0 100644 --- a/src/Generate-DockerImageVariantsHelpers/public/Update-DockerImageVariantsVersions.ps1 +++ b/src/Generate-DockerImageVariantsHelpers/public/Update-DockerImageVariantsVersions.ps1 @@ -1,10 +1,6 @@ function Update-DockerImageVariantsVersions { [CmdletBinding(DefaultParameterSetName='Default',SupportsShouldProcess)] param ( - [Parameter(Mandatory,ParameterSetName='Default',Position=0)] - [ValidateNotNullOrEmpty()] - [System.Collections.Specialized.OrderedDictionary]$VersionsChanged - , [Parameter(HelpMessage='Scriptblock to run before git add and git commit on a PR branch')] [Parameter(ParameterSetName='Default')] [Parameter(ParameterSetName='Pipeline')] @@ -30,26 +26,13 @@ function Update-DockerImageVariantsVersions { [Parameter(ParameterSetName='Pipeline')] [ValidateSet('calver', 'semver')] [string]$AutoReleaseTagConvention - , - [Parameter(ValueFromPipeline,ParameterSetName='Pipeline')] - [ValidateNotNullOrEmpty()] - [System.Collections.Specialized.OrderedDictionary]$InputObject ) process { + Set-StrictMode -Version Latest $callerEA = $ErrorActionPreference $ErrorActionPreference = 'Stop' try { - if ($InputObject) { - $VersionsChanged = $InputObject - } - - $changedCount = ($versionsChanged.Values | ? { $_['kind'] -ne 'existing' } | Measure-Object).Count - if ($changedCount -eq 0) { - "No changed versions. Nothing to do" | Write-Host -ForegroundColor Green - return - } - $prs = @() $autoMergeResults = [ordered]@{ AllPRs = @() @@ -57,62 +40,84 @@ function Update-DockerImageVariantsVersions { FailCount = 0 } $_pr = $null - foreach ($vc in $VersionsChanged.Values) { - # Update versions.json and open PR - if ($vc['kind'] -eq 'new' -or $vc['kind'] -eq 'update') { - if ($PR) { - { git checkout master } | Execute-Command | Write-Host - { git pull origin master } | Execute-Command | Write-Host - } + $versionsConfig = Get-DockerImageVariantsVersions + foreach ($pkg in $versionsConfig.psobject.Properties.Name) { + $versions = $versionsConfig.$pkg.versions + $versionsNew = try { + Invoke-Command -ScriptBlock ([scriptblock]::Create($versionsConfig.$pkg.versionsNewScript)) + }catch { + "Error in $pkg.versionsNewScript. Please review." | Write-Warning + throw + } + if ($null -eq $versionsNew) { + throw "$pkg.versionsNewScript returned null. It should return an array of versions (semver)." + } + $versionsChanged = Get-VersionsChanged -Versions $versions -VersionsNew $versionsNew -AsObject -Descending - if ($vc['kind'] -eq 'new') { - if ($vc['to'] -notin (Get-DockerImageVariantsVersions)) { - "> New: $( $vc['to'] )" | Write-Host -ForegroundColor Green - $versions = @( - $vc['to'] - Get-DockerImageVariantsVersions - ) - $versions = $versions | Select-Object -Unique | Sort-Object { [version]$_ } -Descending - Set-DockerImageVariantsVersions -Versions $versions + $changedCount = ($versionsChanged.Values | ? { $_['kind'] -ne 'existing' } | Measure-Object).Count + if ($changedCount -eq 0) { + "No changed versions. Nothing to do" | Write-Host -ForegroundColor Green + }else { + foreach ($vc in $VersionsChanged.Values) { + # Update versions.json and open PR + if ($vc['kind'] -eq 'new' -or $vc['kind'] -eq 'update') { if ($PR) { - $prs += $_pr = New-DockerImageVariantsPR -Version $vc['to'] -Verb add -CommitPreScriptblock $CommitPreScriptblock + { git checkout master } | Execute-Command | Write-Host + { git pull origin master } | Execute-Command | Write-Host } - } - }elseif ($vc['kind'] -eq 'update') { - $versions = [System.Collections.ArrayList]@() - foreach ($v in (Get-DockerImageVariantsVersions)) { - if ($v -eq $vc['from']) { - "> Update: $( $vc['from'] ) to $( $vc['to'] )" | Write-Host -ForegroundColor Green - $versions.Add($vc['to']) > $null - }else { - $versions.Add($v) > $null + + if ($vc['kind'] -eq 'new') { + if ($vc['to'] -notin $versions) { + "> New: $( $vc['to'] )" | Write-Host -ForegroundColor Green + $versionsUpdated = @( + $vc['to'] + $versions + ) + $versionsUpdated = $versionsUpdated | Select-Object -Unique | Sort-Object { [version]$_ } -Descending + $versionsConfig.$pkg.versions = $versionsUpdated + Set-DockerImageVariantsVersions -Versions $versionsConfig + if ($PR) { + $prs += $_pr = New-DockerImageVariantsPR -Package $pkg -Version $vc['to'] -Verb add -CommitPreScriptblock $CommitPreScriptblock + } + } + }elseif ($vc['kind'] -eq 'update') { + $versionsUpdated = [System.Collections.ArrayList]@() + foreach ($v in $versions) { + if ($v -eq $vc['from']) { + "> Update: $( $vc['from'] ) to $( $vc['to'] )" | Write-Host -ForegroundColor Green + $versionsUpdated.Add($vc['to']) > $null + }else { + $versionsUpdated.Add($v) > $null + } + } + $versionsUpdated = $versionsUpdated | Select-Object -Unique | Sort-Object { [version]$_ } -Descending + $versionsConfig.$pkg.versions = $versionsUpdated + Set-DockerImageVariantsVersions -Versions $versionsConfig + if ($PR) { + $prs += $_pr = New-DockerImageVariantsPR -Package $pkg -Version $vc['from'] -VersionNew $vc['to'] -Verb update -CommitPreScriptblock $CommitPreScriptblock + } } - } - $versions = $versions | Select-Object -Unique | Sort-Object { [version]$_ } -Descending - Set-DockerImageVariantsVersions -Versions $versions - if ($PR) { - $prs += $_pr = New-DockerImageVariantsPR -Version $vc['from'] -VersionNew $vc['to'] -Verb update -CommitPreScriptblock $CommitPreScriptblock - } - } - # Merge PR - if ($PR -and $AutoMergeQueue) { - if ($WhatIfPreference) { - $_pr = [pscustomobject]@{ - number = 1 + # Merge PR + if ($PR -and $AutoMergeQueue) { + if ($WhatIfPreference) { + $_pr = [pscustomobject]@{ + number = 1 + } + } + try { + "Will automerge PR #$( $_pr.number )" | Write-Host -ForegroundColor Green + $autoMergeResults['AllPRs'] += Automerge-DockerImageVariantsPR -PR $_pr + "Automerge succeeded for PR #$( $_pr.number )" | Write-Host -ForegroundColor Green + }catch { + "Automerge failed for PR #$( $_pr.number )" | Write-Warning + $_ | Write-Error -ErrorAction Continue + $autoMergeResults['AllPRs'] += $_pr + $autoMergeResults['FailPRNumbers'] += $_pr.number + $autoMergeResults['FailCount']++ + } } } - try { - "Will automerge PR #$( $_pr.number )" | Write-Host -ForegroundColor Green - $autoMergeResults['AllPRs'] += Automerge-DockerImageVariantsPR -PR $_pr - "Automerge succeeded for PR #$( $_pr.number )" | Write-Host -ForegroundColor Green - }catch { - "Automerge failed for PR #$( $_pr.number )" | Write-Warning - $_ | Write-Error -ErrorAction Continue - $autoMergeResults['AllPRs'] += $_pr - $autoMergeResults['FailPRNumbers'] += $_pr.number - $autoMergeResults['FailCount']++ - } } } }