Files
notarius/internal/framework/pipeline/layout.go

80 lines
2.9 KiB
Go

package pipeline
import (
"fmt"
"sort"
"strings"
)
// PipelineLayout is the catalog-independent ordered structure of a profile.
type PipelineLayout struct {
ID string
ExplicitSteps bool
Profile PipelineProfile
Steps []PipelineLayoutStep
ConfiguredLaneIDs []string
}
type PipelineLayoutStep struct {
ID string
References map[string]ReferenceSource
Artifacts map[string]ArtifactLaneProfile
LaneIDs []string
}
// LayoutPipeline copies and normalizes identities without selecting modules or
// interpreting references. Empty steps remain representable for config
// composition; complete resolution requires each selected step to have a lane.
func LayoutPipeline(profile PipelineProfile) (PipelineLayout, error) {
profile = ClonePipelineProfile(profile)
id := strings.TrimSpace(profile.ID)
if id == "" {
return PipelineLayout{}, fmt.Errorf("pipeline id must not be empty")
}
explicit := profile.Steps != nil
if explicit && len(profile.Artifacts) > 0 {
return PipelineLayout{}, fmt.Errorf("pipeline %q must not declare both artifacts and steps", id)
}
steps := profile.Steps
if !explicit {
steps = []PipelineStepProfile{{ID: "default", Artifacts: profile.Artifacts}}
}
if explicit && len(steps) == 0 {
return PipelineLayout{}, fmt.Errorf("pipeline %q must declare at least one ordered step", id)
}
layout := PipelineLayout{ID: id, ExplicitSteps: explicit, Profile: profile}
seenSteps := make(map[string]struct{}, len(steps))
seenLanes := make(map[string]string)
for index, step := range steps {
stepID := strings.TrimSpace(step.ID)
if stepID == "" {
return PipelineLayout{}, fmt.Errorf("pipeline %q step[%d] id must not be empty", id, index)
}
if _, exists := seenSteps[stepID]; exists {
return PipelineLayout{}, fmt.Errorf("pipeline %q step id %q is duplicated after trimming", id, stepID)
}
seenSteps[stepID] = struct{}{}
ordered := PipelineLayoutStep{ID: stepID, References: step.References, Artifacts: make(map[string]ArtifactLaneProfile, len(step.Artifacts))}
for rawLaneID, lane := range step.Artifacts {
laneID := strings.TrimSpace(rawLaneID)
if laneID == "" {
return PipelineLayout{}, fmt.Errorf("pipeline %q artifact lane id must not be empty", id)
}
if _, exists := ordered.Artifacts[laneID]; exists {
return PipelineLayout{}, fmt.Errorf("pipeline %q artifact lane id %q is duplicated after trimming", id, laneID)
}
if previous, exists := seenLanes[laneID]; exists {
return PipelineLayout{}, fmt.Errorf("pipeline %q artifact lane id %q is duplicated across steps %q and %q", id, laneID, previous, stepID)
}
seenLanes[laneID] = stepID
ordered.Artifacts[laneID] = lane
ordered.LaneIDs = append(ordered.LaneIDs, laneID)
layout.ConfiguredLaneIDs = append(layout.ConfiguredLaneIDs, laneID)
}
sort.Strings(ordered.LaneIDs)
layout.Steps = append(layout.Steps, ordered)
}
sort.Strings(layout.ConfiguredLaneIDs)
return layout, nil
}