Skip to content

Commit aabccec

Browse files
committed
Initial rewrite of tool-audit-prune
1 parent 7e8b415 commit aabccec

File tree

2 files changed

+256
-0
lines changed

2 files changed

+256
-0
lines changed

git-tool-audit-prune.ps1

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#!/usr/bin/env pwsh
2+
3+
Param(
4+
[switch] $noFetch,
5+
[switch] $quiet,
6+
[switch] $dryRun
7+
)
8+
9+
Import-Module -Scope Local "$PSScriptRoot/utils/query-state.psm1"
10+
Import-Module -Scope Local "$PSScriptRoot/utils/framework.psm1"
11+
Import-Module -Scope Local "$PSScriptRoot/utils/actions.psm1"
12+
13+
$diagnostics = New-Diagnostics
14+
$config = Get-Configuration
15+
if (-not $noFetch) {
16+
Update-GitRemote -quiet:$quiet
17+
}
18+
19+
$commonParams = @{
20+
diagnostics = $diagnostics
21+
}
22+
# Get all branches in upstreams
23+
24+
$originalUpstreams = Invoke-LocalAction @commonParams @{
25+
type = 'get-all-upstreams'
26+
parameters= @{}
27+
}
28+
Assert-Diagnostics $diagnostics
29+
30+
# Get branches that actually exist
31+
32+
$allBranches = Select-Branches
33+
34+
# For all keys (downstream) in the upstreams:
35+
# - If the downstream does not exist, replace it with its downstreams in all other upstreams
36+
37+
[string[]]$configuredBranches = @() + $originalUpstreams.Keys
38+
$resultUpstreams = @{}
39+
foreach ($branch in $configuredBranches) {
40+
if ($branch -in $allBranches) { continue }
41+
[string[]]$upstreams = $resultUpstreams[$branch] ?? $originalUpstreams[$branch]
42+
foreach ($downstream in $configuredBranches) {
43+
[string[]]$initial = $resultUpstreams[$downstream] ?? $originalUpstreams[$downstream]
44+
if ($branch -notin $initial) { continue }
45+
$resultUpstreams[$downstream] = Invoke-LocalAction @commonParams @{
46+
type = 'filter-branches'
47+
parameters = @{
48+
include = $initial + $upstreams
49+
exclude = @($branch)
50+
}
51+
}
52+
}
53+
}
54+
55+
Write-Host (ConvertTo-Json $resultUpstreams)
56+
57+
58+
# For all keys (downstream) in the upstreams:
59+
# - Remove entire branch configuration if the branch does not exist
60+
# - Remove upstreams that do not exist
61+
foreach ($branch in $configuredBranches) {
62+
if ($branch -notin $allBranches) {
63+
$resultUpstreams[$branch] = $null
64+
continue
65+
}
66+
[string[]]$upstreams = $resultUpstreams[$branch] ?? $originalUpstreams[$branch]
67+
[string[]]$resultUpstream = @()
68+
foreach ($upstream in $upstreams) {
69+
if ($upstream -in $allBranches) {
70+
$resultUpstream = $resultUpstream + @($upstream)
71+
}
72+
}
73+
}
74+
75+
# Simplify changed upstreams
76+
foreach ($branch in $configuredBranches) {
77+
if (-not $resultUpstreams[$branch]) { continue }
78+
[string[]]$result = Invoke-LocalAction @commonParams @{
79+
type = 'simplify-upstream'
80+
parameters = @{
81+
upstreamBranches = $resultUpstreams[$branch]
82+
overrideUpstreams = $resultUpstreams
83+
branchName = $branch
84+
}
85+
}
86+
if ($result.length -ne ([string[]]$resultUpstreams[$branch]).length) {
87+
$resultUpstreams[$branch] = $result
88+
}
89+
}
90+
Assert-Diagnostics $diagnostics
91+
92+
Write-Host (ConvertTo-Json $resultUpstreams)
93+
94+
# Set upstream branch
95+
96+
if ($resultUpstreams.Count -ne 0) {
97+
$upstreamHash = Invoke-LocalAction @commonParams @{
98+
type = 'set-upstream'
99+
parameters = @{
100+
upstreamBranches = $resultUpstreams
101+
message = "Applied changes from 'simplify' audit"
102+
}
103+
}
104+
Assert-Diagnostics $diagnostics
105+
}
106+
107+
# Finalize: Push upstream branch
108+
109+
$commonParams = @{
110+
diagnostics = $diagnostics
111+
dryRun = $dryRun
112+
}
113+
114+
if ($resultUpstreams.Count -ne 0) {
115+
Invoke-FinalizeAction @commonParams @{
116+
type = 'set-branches'
117+
parameters = @{
118+
branches = @{
119+
"$($config.upstreamBranch)" = $upstreamHash.commit
120+
}
121+
}
122+
}
123+
Assert-Diagnostics $diagnostics
124+
}

git-tool-audit-prune.tests.ps1

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
Describe 'Invoke-PruneAudit' {
2+
BeforeAll {
3+
. "$PSScriptRoot/utils/testing.ps1"
4+
Import-Module -Scope Local "$PSScriptRoot/utils/framework.mocks.psm1"
5+
Import-Module -Scope Local "$PSScriptRoot/utils/input.mocks.psm1"
6+
Import-Module -Scope Local "$PSScriptRoot/utils/query-state.mocks.psm1"
7+
8+
function Initialize-ValidDownstreamBranchNames {
9+
$upstreams = Select-AllUpstreamBranches
10+
[string[]]$entries = @()
11+
foreach ($key in $upstreams.Keys) {
12+
foreach ($downstream in $upstreams[$key]) {
13+
if ($downstream -notin $entries) {
14+
[string[]]$entries = $entries + @($downstream)
15+
Initialize-AssertValidBranchName $downstream
16+
}
17+
}
18+
}
19+
}
20+
}
21+
22+
BeforeEach {
23+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUserDeclaredVarsMoreThanAssignments', '', Justification='This is put in scope and used in the tests below')]
24+
$fw = Register-Framework
25+
26+
27+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUserDeclaredVarsMoreThanAssignments', '', Justification='This is put in scope and used in the tests below')]
28+
$initialCommits = @{
29+
'rc/2022-07-14' = 'rc/2022-07-14-commitish'
30+
'main' = 'main-commitish'
31+
'feature/FOO-123' = 'feature/FOO-123-commitish'
32+
'feature/XYZ-1-services' = 'feature/XYZ-1-services-commitish'
33+
'infra/shared' = 'infra/shared-commitish'
34+
}
35+
}
36+
37+
function Add-StandardTests() {
38+
It 'does nothing when no branches are configured' {
39+
Initialize-SelectBranches @()
40+
Initialize-AllUpstreamBranches @{}
41+
42+
& $PSScriptRoot/git-tool-audit-prune.ps1
43+
$fw.assertDiagnosticOutput | Should -BeNullOrEmpty
44+
}
45+
46+
It 'does nothing when existing branches are configured correctly' -Pending {
47+
Initialize-AllUpstreamBranches @{
48+
'rc/2022-07-14' = @("feature/FOO-123")
49+
'feature/FOO-123' = @('infra/shared')
50+
'infra/shared' = @('main')
51+
}
52+
Initialize-ValidDownstreamBranchNames
53+
54+
& $PSScriptRoot/git-tool-audit-prune.ps1
55+
$fw.assertDiagnosticOutput | Should -BeNullOrEmpty
56+
}
57+
58+
It 'does not apply with a dry run' -Pending {
59+
Initialize-AllUpstreamBranches @{
60+
'rc/2022-07-14' = @("feature/FOO-123","feature/XYZ-1-services")
61+
'feature/FOO-123' = @('infra/shared')
62+
'infra/shared' = @('main')
63+
'feature/XYZ-1-services' = @('infra/shared') # intentionally have an extra configured branch here for removal
64+
}
65+
Initialize-ValidDownstreamBranchNames
66+
67+
& $PSScriptRoot/git-tool-audit-prune.ps1 -dryRun
68+
$fw.assertDiagnosticOutput | Should -BeNullOrEmpty
69+
}
70+
71+
It 'prunes configuration of extra branches' -Pending {
72+
Initialize-AllUpstreamBranches @{
73+
'rc/2022-07-14' = @("feature/FOO-123","feature/XYZ-1-services")
74+
'feature/FOO-123' = @('infra/shared')
75+
'feature/XYZ-1-services' = @('infra/shared') # intentionally have an extra configured branch here for removal
76+
'infra/shared' = @('main')
77+
}
78+
Initialize-ValidDownstreamBranchNames
79+
80+
$mock = @(
81+
# TODO - save
82+
)
83+
84+
& $PSScriptRoot/git-tool-audit-prune.ps1
85+
$fw.assertDiagnosticOutput | Should -BeNullOrEmpty
86+
87+
Invoke-VerifyMock $mock -Times 1
88+
}
89+
90+
It 'consolidates removed branches' -Pending {
91+
# TODO -setup
92+
93+
$mock = @(
94+
# TODO: save
95+
)
96+
97+
& $PSScriptRoot/git-tool-audit-prune.ps1
98+
$fw.assertDiagnosticOutput | Should -BeNullOrEmpty
99+
100+
Invoke-VerifyMock $mock -Times 1
101+
}
102+
}
103+
104+
Context 'with no remote' {
105+
BeforeAll {
106+
Initialize-ToolConfiguration -noRemote
107+
Initialize-SelectBranches @(
108+
'rc/2022-07-14',
109+
'feature/FOO-123',
110+
'infra/shared',
111+
'main'
112+
)
113+
}
114+
115+
Add-StandardTests
116+
}
117+
118+
Context 'with remote' {
119+
BeforeAll {
120+
Initialize-ToolConfiguration
121+
Initialize-UpdateGitRemote
122+
Initialize-SelectBranches @(
123+
'rc/2022-07-14',
124+
'feature/FOO-123',
125+
'infra/shared',
126+
'main'
127+
)
128+
}
129+
130+
Add-StandardTests
131+
}
132+
}

0 commit comments

Comments
 (0)