/** * As per `Stage.ancestors` except this method returns tuples of the stages * and their `StageDefinitionBuilder`. */ public List<Result> ancestors(Stage startingStage) { return startingStage .ancestors() .stream() .map(it -> new Result(it, stageDefinitionBuilders.get(it.getType())) ) .collect(toList()); }
private static Map<String, Object> findBuildInfoInUpstreamStage(Stage currentStage, List<Pattern> packageFilePatterns) { Stage upstreamStage = currentStage .ancestors() .stream() .filter(it -> { Map<String, Object> buildInfo = (Map<String, Object>) it.getOutputs().get("buildInfo"); return buildInfo != null && artifactMatch((List<Map<String, String>>) buildInfo.get("artifacts"), packageFilePatterns); }) .findFirst() .orElse(null); return upstreamStage != null ? (Map<String, Object>) upstreamStage.getOutputs().get("buildInfo") : emptyMap(); }
@Override public Object get(@Nullable Object key) { if (delegate().containsKey(key)) { return super.get(key); } else { return stage .ancestors() .stream() .filter(it -> it.getOutputs().containsKey(key)) .findFirst() .map(it -> it.getOutputs().get(key)) .orElse(null); } }
@SuppressWarnings("unchecked") public <E> List<E> getAll(Object key) { List<E> result = (List<E>) stage .ancestors() .stream() .filter(it -> it.getOutputs().containsKey(key)) .map(it -> it.getOutputs().get(key)) .collect(toList()); if (delegate.containsKey(key)) { result.add(0, (E) delegate.get(key)); } if (key.equals("artifacts")) { result.add((E) getTrigger().getArtifacts()); } else if (key.equals("resolvedExpectedArtifacts")) { result.add((E) getTrigger().getResolvedExpectedArtifacts()); } return result; } }
@VisibleForTesting Collection<String> upstreamImageIds(Stage sourceStage, Collection<String> consideredStageRefIds, String cloudProviderType) { List<Stage> ancestors = sourceStage.ancestors(); List<Stage> imageProvidingAncestorStages = ancestors.stream() .filter(stage -> { String cloudProvider = (String) stage.getContext().getOrDefault("cloudProvider", stage.getContext().get("cloudProviderType")); boolean consideredStageRefIdMatches = consideredStageRefIds == null || consideredStageRefIds.isEmpty() || consideredStageRefIds.contains(stage.getRefId()) || (stage.getParent() != null && consideredStageRefIds.contains(stage.getParent().getRefId())); return consideredStageRefIdMatches && (stage.getContext().containsKey("imageId") || stage.getContext().containsKey("amiDetails")) && cloudProviderType.equals(cloudProvider); }).collect(toList()); return imageProvidingAncestorStages.stream().map(it -> { if (it.getContext().containsKey("imageId")) { return (String) it.getContext().get("imageId"); } else { return (String) ((List<Map>) it.getContext().get("amiDetails")).get(0).get("imageId"); } }).collect(toList()); }