Share artifact dependency queries across consumers
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifactdeps"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifactpolicy"
|
||||
)
|
||||
|
||||
@@ -263,76 +264,51 @@ func validateScriptoriumInputSource(
|
||||
// graph independently of execution selection. Disabled artifacts remain valid
|
||||
// prerequisites for explicit selections and therefore participate in cycles.
|
||||
func ValidateScriptoriumArtifactDependencies(artifacts map[string]ScriptoriumArtifactConfig) error {
|
||||
if len(artifacts) == 0 {
|
||||
return nil
|
||||
}
|
||||
_, err := BuildArtifactGraph(artifacts)
|
||||
return err
|
||||
}
|
||||
|
||||
// BuildArtifactGraph validates definitions and builds their immutable dependency view.
|
||||
func BuildArtifactGraph(artifacts map[string]ScriptoriumArtifactConfig) (*artifactdeps.Graph, error) {
|
||||
nodes := make([]string, 0, len(artifacts))
|
||||
for name := range artifacts {
|
||||
nodes = append(nodes, name)
|
||||
}
|
||||
sort.Strings(nodes)
|
||||
|
||||
graph := make(map[string][]string, len(artifacts))
|
||||
edges := make(map[string][]string, len(artifacts))
|
||||
for _, name := range nodes {
|
||||
cfg := artifacts[name]
|
||||
if !artifactpolicy.IsConfiguredKey(name) {
|
||||
return fmt.Errorf("pipeline.scriptorium.artifacts keys must match ^[a-z][a-z0-9_]*$")
|
||||
return nil, fmt.Errorf("pipeline.scriptorium.artifacts keys must match ^[a-z][a-z0-9_]*$")
|
||||
}
|
||||
seen := make(map[string]struct{}, len(cfg.DependsOn))
|
||||
for _, dep := range cfg.DependsOn {
|
||||
trimmedDep := strings.TrimSpace(dep)
|
||||
if trimmedDep == "" {
|
||||
field := dependencyOriginField(name, trimmedDep, cfg)
|
||||
if strings.TrimSpace(dep) == "" {
|
||||
field := dependencyOriginField(name, "", cfg)
|
||||
if field == fmt.Sprintf("pipeline.scriptorium.artifacts.%s.depends_on", name) {
|
||||
return fmt.Errorf("%s entries must be non-empty", field)
|
||||
return nil, fmt.Errorf("%s entries must be non-empty", field)
|
||||
}
|
||||
return fmt.Errorf("%s must be non-empty", field)
|
||||
return nil, fmt.Errorf("%s must be non-empty", field)
|
||||
}
|
||||
if _, ok := artifacts[trimmedDep]; !ok {
|
||||
return fmt.Errorf("%s dependency %q is not configured", dependencyOriginField(name, trimmedDep, cfg), dep)
|
||||
}
|
||||
if trimmedDep == name {
|
||||
return fmt.Errorf("%s must not include itself", dependencyOriginField(name, trimmedDep, cfg))
|
||||
}
|
||||
if _, duplicate := seen[trimmedDep]; duplicate {
|
||||
continue
|
||||
}
|
||||
seen[trimmedDep] = struct{}{}
|
||||
graph[name] = append(graph[name], trimmedDep)
|
||||
}
|
||||
sort.Strings(graph[name])
|
||||
edges[name] = cfg.DependsOn
|
||||
}
|
||||
|
||||
visiting := make(map[string]bool, len(artifacts))
|
||||
visited := make(map[string]bool, len(artifacts))
|
||||
|
||||
var visit func(node string) error
|
||||
visit = func(node string) error {
|
||||
if visited[node] {
|
||||
return nil
|
||||
}
|
||||
visiting[node] = true
|
||||
for _, dep := range graph[node] {
|
||||
if visiting[dep] {
|
||||
return fmt.Errorf(
|
||||
"%s creates a cycle; pipeline.scriptorium.artifacts dependencies must not contain cycles",
|
||||
dependencyOriginField(node, dep, artifacts[node]),
|
||||
)
|
||||
}
|
||||
if err := visit(dep); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
visiting[node] = false
|
||||
visited[node] = true
|
||||
return nil
|
||||
graph, err := artifactdeps.New(edges)
|
||||
if err == nil {
|
||||
return graph, nil
|
||||
}
|
||||
|
||||
for _, node := range nodes {
|
||||
if err := visit(node); err != nil {
|
||||
return err
|
||||
}
|
||||
var graphErr *artifactdeps.Error
|
||||
if !errors.As(err, &graphErr) {
|
||||
return nil, err
|
||||
}
|
||||
field := dependencyOriginField(graphErr.Node, graphErr.Dependency, artifacts[graphErr.Node])
|
||||
switch graphErr.Kind {
|
||||
case artifactdeps.UnknownDependency:
|
||||
return nil, fmt.Errorf("%s dependency %q is not configured", field, graphErr.Dependency)
|
||||
case artifactdeps.SelfDependency:
|
||||
return nil, fmt.Errorf("%s must not include itself", field)
|
||||
case artifactdeps.Cycle:
|
||||
return nil, fmt.Errorf("%s creates a cycle; pipeline.scriptorium.artifacts dependencies must not contain cycles", field)
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user