Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sorting for diff on delete #146

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 32 additions & 13 deletions .github/actions/validate-deploy/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,19 @@ runs:
using: 'composite'
steps:
#
# Diff
# Diff List
# List index changes
#

- name: "Diff"
- name: "Diff List"
shell: pwsh
run: |
$gitDiff = git diff --name-status HEAD^ HEAD
Import-PSFConfig -Path settings.json -Schema MetaJson -EnableException
$stateDir = Get-PSFConfigValue -FullName 'AzOps.Core.State'
$gitDiff = git diff --name-status HEAD^ HEAD -- "$stateDir"
if ($null -ne $gitDiff) {
$gitDiff | Write-Host
$gitDiff | Out-File -FilePath '/tmp/diff.txt'

$deletedContent = git diff --diff-filter=D HEAD^ HEAD --no-prefix --no-renames
if($null -ne $deletedContent) {
$deletedContent = $deletedContent -match '^-' -replace '^([^-+ ]*)[-+ ]', '$1'
Write-Host '##[group]Deleted files content'
$deletedContent | Write-Host
Write-Host '##[endgroup]'
$deletedContent | Out-File -FilePath '/tmp/diffdeletedfiles.txt'
}
}
else {
Write-Error -Message 'The validation pipeline failed because there is currently no change to be processed'
Expand All @@ -50,7 +43,33 @@ runs:
./.scripts/customSorting.ps1

#
# Deploy
# Diff
# Get content from list
#

- name: "Diff"
shell: pwsh
run: |
$diff = Get-Content -Path '/tmp/diff.txt'
$deletedContent = $(foreach ($change in $diff) {
$path = ($change -split "`t")[-1]
$fileDelContent = git diff --diff-filter=D HEAD^ HEAD --no-prefix --no-renames -- "$path"
if($null -ne $fileDelContent) {
$fileDelContent = $fileDelContent -match '^-' -replace '^([^-+ ]*)[-+ ]', '$1'
Write-Output -InputObject $fileDelContent
}
}) | Where-Object { $_ }

if ($deletedContent.Count -gt 0) {
Write-Host '##[group]Deleted files content'
$deletedContent | Write-Host
Write-Host '##[endgroup]'
$deletedContent | Out-File -FilePath '/tmp/diffdeletedfiles.txt'
}

#
# Validate or Deploy
# If parameter "deploy" is set to true, then deploy the changes,
# Initial deployment of any index changes
#

Expand Down
70 changes: 45 additions & 25 deletions .pipelines/.templates/validate-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,32 @@ parameters:

steps:

#
# Diff
# List index changes
#

#
# Diff List
# List index changes
#
- task: PowerShell@2
displayName: "Diff"
displayName: "Diff List"
inputs:
targetType: "inline"
script: |
$gitDiff = git diff --name-status HEAD^ HEAD
if($null -ne $gitDiff) {
$gitDiff | Write-Host
$gitDiff | Out-File -FilePath '/tmp/diff.txt'

$deletedContent = git diff --diff-filter=D HEAD^ HEAD --no-prefix --no-renames
if($null -ne $deletedContent) {
$deletedContent = $deletedContent -match '^-' -replace '^([^-+ ]*)[-+ ]', '$1'
Write-Host '##[group]Deleted files content'
$deletedContent | Write-Host
Write-Host '##[endgroup]'
$deletedContent | Out-File -FilePath '/tmp/diffdeletedfiles.txt'
}
Import-PSFConfig -Path settings.json -Schema MetaJson -EnableException
$stateDir = Get-PSFConfigValue -FullName 'AzOps.Core.State'
$gitDiff = git diff --name-status HEAD^ HEAD -- "$stateDir"
if ($null -ne $gitDiff) {
$gitDiff | Write-Host
$gitDiff | Out-File -FilePath '/tmp/diff.txt'
}
else {
Write-Host '##[error]The validation pipeline failed because there is currently no change to be processed'
exit 1
Write-Host '##[error]The validation pipeline failed because there is currently no change to be processed'
exit 1
}

#
# CustomSorting
# If CustomSorting is enabled, sort files in diff by the .order file in each directory
#
#
# CustomSorting
# If CustomSorting is enabled, sort files in diff by the .order file in each directory
#

- task: PowerShell@2
displayName: "CustomSorting"
Expand All @@ -46,6 +39,33 @@ steps:
targetType: "filePath"
filePath: ".scripts/customSorting.ps1"

#
# Diff
# Get content from list
#

- task: PowerShell@2
displayName: "Diff"
inputs:
targetType: "inline"
script: |
$diff = Get-Content -Path '/tmp/diff.txt'
$deletedContent = $(foreach ($change in $diff) {
$path = ($change -split "`t")[-1]
$fileDelContent = git diff --diff-filter=D HEAD^ HEAD --no-prefix --no-renames -- "$path"
if($null -ne $fileDelContent) {
$fileDelContent = $fileDelContent -match '^-' -replace '^([^-+ ]*)[-+ ]', '$1'
Write-Output -InputObject $fileDelContent
}
}) | Where-Object { $_ }

if ($deletedContent.Count -gt 0) {
Write-Host '##[group]Deleted files content'
$deletedContent | Write-Host
Write-Host '##[endgroup]'
$deletedContent | Out-File -FilePath '/tmp/diffdeletedfiles.txt'
}

#
# Validate or Deploy
# If parameter "deploy" is set to true, then deploy the changes,
Expand Down
41 changes: 33 additions & 8 deletions .scripts/customSorting.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,22 @@ $EndGroup = $ENV:CI ? '::endgroup::' : '##[endgroup]'
$diff = Get-Content -Path $DiffFilePath

Write-Host "${StartGroup}Files found in diff:"

$diff | Write-Host
Write-Host "$EndGroup"

$diffTable = @{}
$diff | ForEach-Object -Process {
$change = $_
$path = ($change -split "`t")[-1]
# there can be 2 elements for Add, Delete, Modify operations
# there can be 3 elements if it's a rename
$changeParts = ($change -split "`t")
$operation = $changeParts[0]
$path = $changeParts[-1]

$entry = [pscustomobject]@{
fileName = Split-Path -Path $path -Leaf
directory = Split-Path -Path $path -Parent
operation = $operation
diffString = $change
}
if ($null -eq $diffTable[$entry.directory]) {
Expand All @@ -27,20 +34,38 @@ $diff | ForEach-Object -Process {
$diffTable[$entry.directory][$entry.fileName] = $entry
}
}
$sortedDiff = foreach ($directoryPath in ($diffTable.Keys | Sort-Object)) {
$sortedDiff = $(foreach ($directoryPath in ($diffTable.Keys | Sort-Object)) {
$orderPath = [System.IO.Path]::Combine($directoryPath,'.order')
if (Test-Path -Path $orderPath) {
$order = Get-Content -Path $orderPath | ForEach-Object { $_.Trim() }
foreach ($orderName in $order) {
$deleteSortedDiffs = @()
$addSortedDiffs = foreach ($orderName in $order) {
if ($null -ne $diffTable.$directoryPath.$orderName) {
Write-Output -InputObject $diffTable.$directoryPath.$orderName.diffString
$diffString = $diffTable.$directoryPath.$orderName.diffString
$operation = $diffTable.$directoryPath.$orderName.operation
$diffTable.$directoryPath.Remove($orderName)
if ($operation -eq 'D') {
$deleteSortedDiffs += $diffString
continue
}
elseif ($operation -in 'A', 'M', 'R' -or $operation -match '^R0[0-9][0-9]$') {
Write-Output -InputObject $diffString
}
else {
Write-Error -Message "Invalid changeset type '$operation' for $diffString"
}
}
}
# Deletes should happen in reverse order to add/modifys
[array]::Reverse($deleteSortedDiffs)

Write-Output -InputObject $addSortedDiffs
Write-Output -InputObject $deleteSortedDiffs
}
Write-Output ($diffTable.$directoryPath.Values.diffString | Sort-Object)
}
Write-Host "$EndGroup"
# make sure to return unaddressed diffs too
Write-Output -InputObject ($diffTable.$directoryPath.Values.diffString | Sort-Object)
}) | Where-Object { $_ }

Write-Host "${StartGroup}Sorted files:"

$sortedDiff | Write-Host
Expand Down