From f406928ad2d07be5588f7426f91cc07ce0227d09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Jervidalo?= Date: Tue, 21 Feb 2023 09:52:39 +0100 Subject: [PATCH] Implementation code to fix failing tests --- .../orca/pipeline/util/ArtifactUtils.java | 41 +++++++++++++++++-- .../front50/DependentPipelineStarter.groovy | 8 ++++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/orca-core/src/main/java/com/netflix/spinnaker/orca/pipeline/util/ArtifactUtils.java b/orca-core/src/main/java/com/netflix/spinnaker/orca/pipeline/util/ArtifactUtils.java index 4ae052f4e58..fa41626fc53 100644 --- a/orca-core/src/main/java/com/netflix/spinnaker/orca/pipeline/util/ArtifactUtils.java +++ b/orca-core/src/main/java/com/netflix/spinnaker/orca/pipeline/util/ArtifactUtils.java @@ -28,6 +28,7 @@ import com.netflix.spinnaker.kork.annotations.NonnullByDefault; import com.netflix.spinnaker.kork.artifacts.model.Artifact; import com.netflix.spinnaker.kork.artifacts.model.ExpectedArtifact; +import com.netflix.spinnaker.kork.web.exceptions.InvalidRequestException; import com.netflix.spinnaker.orca.api.pipeline.models.PipelineExecution; import com.netflix.spinnaker.orca.api.pipeline.models.StageExecution; import com.netflix.spinnaker.orca.pipeline.model.StageContext; @@ -35,13 +36,20 @@ import com.netflix.spinnaker.orca.pipeline.persistence.ExecutionRepository; import com.netflix.spinnaker.orca.pipeline.persistence.ExecutionRepository.ExecutionCriteria; import java.io.IOException; -import java.util.*; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.Stream; import javax.annotation.CheckReturnValue; import javax.annotation.Nonnull; import javax.annotation.Nullable; +import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -133,7 +141,30 @@ private List getAllArtifacts( contextParameterProcessor.process( boundArtifactMap, contextParameterProcessor.buildExecutionContext(stage), true); - return objectMapper.convertValue(evaluatedBoundArtifactMap, Artifact.class); + Artifact evaluatedArtifact = + objectMapper.convertValue(evaluatedBoundArtifactMap, Artifact.class); + return getBoundInlineArtifact(evaluatedArtifact, stage.getExecution()) + .orElse(evaluatedArtifact); + } + + private Optional getBoundInlineArtifact( + @Nullable Artifact artifact, PipelineExecution execution) { + if (ObjectUtils.anyNull( + artifact, execution.getTrigger(), execution.getTrigger().getArtifacts())) { + return Optional.empty(); + } + try { + ExpectedArtifact expectedArtifact = + ExpectedArtifact.builder().matchArtifact(artifact).build(); + return ArtifactResolver.getInstance(execution.getTrigger().getArtifacts(), true) + .resolveExpectedArtifacts(List.of(expectedArtifact)) + .getResolvedArtifacts() + .stream() + .findFirst(); + } catch (InvalidRequestException e) { + log.debug("Could not match inline artifact with trigger bound artifacts", e); + return Optional.empty(); + } } public @Nullable Artifact getBoundArtifactForId(StageExecution stage, @Nullable String id) { @@ -204,7 +235,11 @@ public void resolveArtifacts(Map pipeline) { .map(Collection::stream) .orElse(Stream.empty()) .map(it -> objectMapper.convertValue(it, ExpectedArtifact.class)) - .filter(artifact -> expectedArtifactIds.contains(artifact.getId())) + .filter( + artifact -> + expectedArtifactIds.contains(artifact.getId()) + || artifact.isUseDefaultArtifact() + || artifact.isUsePriorArtifact()) .collect(toImmutableList()); ImmutableSet receivedArtifacts = diff --git a/orca-front50/src/main/groovy/com/netflix/spinnaker/orca/front50/DependentPipelineStarter.groovy b/orca-front50/src/main/groovy/com/netflix/spinnaker/orca/front50/DependentPipelineStarter.groovy index 91e36beb8cf..a2b5d2a84a9 100644 --- a/orca-front50/src/main/groovy/com/netflix/spinnaker/orca/front50/DependentPipelineStarter.groovy +++ b/orca-front50/src/main/groovy/com/netflix/spinnaker/orca/front50/DependentPipelineStarter.groovy @@ -84,6 +84,11 @@ class DependentPipelineStarter implements ApplicationContextAware { } collectMany { it.expectedArtifactIds ?: [] } + if (!expectedArtifactIds && parentPipelineStageId) { + expectedArtifactIds = parentPipeline.trigger.resolvedExpectedArtifacts.collect { + it.id + } + } pipelineConfig.trigger = [ type : "pipeline", @@ -117,6 +122,9 @@ class DependentPipelineStarter implements ApplicationContextAware { if (parentPipelineStageId != null) { pipelineConfig.receivedArtifacts = artifactUtils?.getArtifacts(parentPipeline.stageById(parentPipelineStageId)) + if (!pipelineConfig.expectedArtifacts) { + pipelineConfig.expectedArtifacts = parentPipeline.trigger.getOther().getOrDefault("expectedArtifacts", []) + } } else { pipelineConfig.receivedArtifacts = artifactUtils?.getAllArtifacts(parentPipeline) }