Provisioning: Fix deletion order (#111043)

This commit is contained in:
Stephanie Hingtgen
2025-09-12 20:02:13 +00:00
committed by GitHub
parent c52eedbf23
commit c5ed2780ab
2 changed files with 95 additions and 7 deletions
@@ -160,14 +160,36 @@ func sortResourceListForDeletion(list *provisioning.ResourceList) {
// Sort by the following logic:
// - Put folders at the end so that we empty them first.
// - Sort folders by depth so that we remove the deepest first
// - If the repo is created within a folder in grafana, make sure that folder is last.
sort.Slice(list.Items, func(i, j int) bool {
switch {
case list.Items[i].Group != folders.RESOURCE:
return true
case list.Items[j].Group != folders.RESOURCE:
return false
default:
return len(strings.Split(list.Items[i].Path, "/")) > len(strings.Split(list.Items[j].Path, "/"))
isFolderI := list.Items[i].Group == folders.GroupVersion.Group
isFolderJ := list.Items[j].Group == folders.GroupVersion.Group
// non-folders always go first in the order of deletion.
if isFolderI != isFolderJ {
return !isFolderI
}
// if both are not folders, keep order (doesn't matter)
if !isFolderI && !isFolderJ {
return false
}
hasFolderI := list.Items[i].Folder != ""
hasFolderJ := list.Items[j].Folder != ""
// if one folder is in the root (i.e. does not have a folder specified), put that last
if hasFolderI != hasFolderJ {
return hasFolderI
}
// if both are nested folder, sort by depth, with the deepest one being first
depthI := len(strings.Split(list.Items[i].Path, "/"))
depthJ := len(strings.Split(list.Items[j].Path, "/"))
if depthI != depthJ {
return depthI > depthJ
}
// otherwise, keep order (doesn't matter)
return false
})
}