public void setContext(@Nonnull Map<String, Object> context) { if (context instanceof StageContext) { this.context = context; } else { this.context = new StageContext(this, context); } }
public @Nonnull List<Artifact> getArtifacts(@Nonnull Stage stage) { if (stage.getContext() instanceof StageContext) { return (List<Artifact>) Optional.ofNullable((List) ((StageContext) stage.getContext()).getAll("artifacts")) .map(list -> list.stream() .filter(Objects::nonNull) .flatMap(it -> ((List) it).stream()) .map(a -> a instanceof Map ? objectMapper.convertValue(a, Artifact.class) : a) .collect(Collectors.toList())) .orElse(emptyList()); } else { log.warn("Unable to read artifacts from unknown context type: {} ({})", stage.getContext().getClass(), stage.getExecution().getId()); return emptyList(); } }
@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; } }
public @Nullable Artifact getBoundArtifactForId( @Nonnull Stage stage, @Nullable String id) { if (isEmpty(id)) { return null; } List<ExpectedArtifact> expectedArtifacts; if (stage.getContext() instanceof StageContext) { expectedArtifacts = (List<ExpectedArtifact>) Optional.ofNullable((List) ((StageContext) stage.getContext()).getAll("resolvedExpectedArtifacts")) .map(list -> list.stream() .filter(Objects::nonNull) .flatMap(it -> ((List) it).stream()) .map(a -> a instanceof Map ? objectMapper.convertValue(a, ExpectedArtifact.class) : a) .collect(Collectors.toList())) .orElse(emptyList()); } else { log.warn("Unable to read resolved expected artifacts from unknown context type: {} ({})", stage.getContext().getClass(), stage.getExecution().getId()); expectedArtifacts = new ArrayList<>(); } return expectedArtifacts .stream() .filter(e -> e.getId().equals(id)) .findFirst() .map(ExpectedArtifact::getBoundArtifact) .orElse(null); }