83 lines
3.1 KiB
Go
83 lines
3.1 KiB
Go
package app
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
|
|
"gitea.maximumdirect.net/eric/distributor/internal/bundle"
|
|
"gitea.maximumdirect.net/eric/distributor/internal/config"
|
|
"gitea.maximumdirect.net/eric/distributor/internal/publish"
|
|
"gitea.maximumdirect.net/eric/distributor/internal/storage"
|
|
)
|
|
|
|
type destinationBundleSelection struct {
|
|
SourceBundle bundle.Bundle
|
|
DestinationBundlePath string
|
|
}
|
|
|
|
func selectDestinationBundles(destination config.Destination, bundles []bundle.Bundle) []destinationBundleSelection {
|
|
if !isFixedPathDestination(destination) {
|
|
selections := make([]destinationBundleSelection, 0, len(bundles))
|
|
for _, sourceBundle := range bundles {
|
|
selections = append(selections, destinationBundleSelection{
|
|
SourceBundle: sourceBundle,
|
|
DestinationBundlePath: sourceBundle.RootRelativePath,
|
|
})
|
|
}
|
|
return selections
|
|
}
|
|
if len(bundles) == 0 {
|
|
return nil
|
|
}
|
|
sourceBundle := newestBundle(bundles)
|
|
return []destinationBundleSelection{{
|
|
SourceBundle: sourceBundle,
|
|
DestinationBundlePath: "",
|
|
}}
|
|
}
|
|
|
|
func newestBundle(bundles []bundle.Bundle) bundle.Bundle {
|
|
if len(bundles) == 0 {
|
|
return bundle.Bundle{}
|
|
}
|
|
sorted := append([]bundle.Bundle(nil), bundles...)
|
|
sort.Slice(sorted, func(i, j int) bool {
|
|
if sorted[i].Manifest.Created.Equal(sorted[j].Manifest.Created) {
|
|
return sorted[i].RootRelativePath < sorted[j].RootRelativePath
|
|
}
|
|
return sorted[i].Manifest.Created.After(sorted[j].Manifest.Created)
|
|
})
|
|
return sorted[0]
|
|
}
|
|
|
|
func isFixedPathDestination(destination config.Destination) bool {
|
|
return destination.PathMap.Mode == config.PathMappingFixed
|
|
}
|
|
|
|
func fixedPathSelectionWarning(pipelineID, destinationID string, selections []destinationBundleSelection, candidateCount int) OutputWarning {
|
|
selected := "none"
|
|
if len(selections) > 0 {
|
|
selected = storage.DisplayPath(selections[0].SourceBundle.RootRelativePath)
|
|
}
|
|
return OutputWarning{Message: fmt.Sprintf("pipeline=%s destination=%s path_mapping=fixed candidates=%d selected_bundle=%s destination_bundle=.", pipelineID, destinationID, candidateCount, selected)}
|
|
}
|
|
|
|
func isDestructiveFixedPathAction(action publish.Action) bool {
|
|
return action == publish.ActionReplaceOlder || action == publish.ActionReplaceTakeover || action == publish.ActionForceReplace
|
|
}
|
|
|
|
func fixedPathReplacementWarning(plan publish.Plan) OutputWarning {
|
|
if plan.Action == publish.ActionReplaceTakeover {
|
|
return OutputWarning{Message: fmt.Sprintf("pipeline=%s destination=%s path_mapping=fixed action=%s takeover_mode=%s replaces destination root for selected_bundle=%s reason=%q", plan.PipelineID, plan.DestinationID, plan.Action, plan.TakeoverMode, storage.DisplayPath(plan.BundlePath), plan.Reason)}
|
|
}
|
|
return OutputWarning{Message: fmt.Sprintf("pipeline=%s destination=%s path_mapping=fixed action=%s replaces destination root for selected_bundle=%s", plan.PipelineID, plan.DestinationID, plan.Action, storage.DisplayPath(plan.BundlePath))}
|
|
}
|
|
|
|
func destinationIDs(destinations []config.Destination) []string {
|
|
ids := make([]string, 0, len(destinations))
|
|
for _, destination := range destinations {
|
|
ids = append(ids, destination.ID)
|
|
}
|
|
return ids
|
|
}
|