|
52 | 52 | import javax.annotation.CheckReturnValue;
|
53 | 53 | import javax.annotation.Nonnull;
|
54 | 54 | import javax.annotation.Nullable;
|
55 |
| -import org.apache.commons.lang3.ObjectUtils; |
56 | 55 | import org.apache.commons.lang3.StringUtils;
|
57 | 56 | import org.slf4j.Logger;
|
58 | 57 | import org.slf4j.LoggerFactory;
|
@@ -144,31 +143,7 @@ private List<Artifact> getAllArtifacts(
|
144 | 143 | contextParameterProcessor.process(
|
145 | 144 | boundArtifactMap, contextParameterProcessor.buildExecutionContext(stage), true);
|
146 | 145 |
|
147 |
| - Artifact evaluatedArtifact = |
148 |
| - objectMapper.convertValue(evaluatedBoundArtifactMap, Artifact.class); |
149 |
| - return getBoundInlineArtifact(evaluatedArtifact, stage.getExecution()) |
150 |
| - .orElse(evaluatedArtifact); |
151 |
| - } |
152 |
| - |
153 |
| - private Optional<Artifact> getBoundInlineArtifact( |
154 |
| - @Nullable Artifact artifact, PipelineExecution execution) { |
155 |
| - if (ObjectUtils.anyNull( |
156 |
| - artifact, execution.getTrigger(), execution.getTrigger().getArtifacts())) { |
157 |
| - return Optional.empty(); |
158 |
| - } |
159 |
| - try { |
160 |
| - ExpectedArtifact expectedArtifact = |
161 |
| - ExpectedArtifact.builder().matchArtifact(artifact).build(); |
162 |
| - return ArtifactResolver.getInstance(execution.getTrigger().getArtifacts(), true) |
163 |
| - .resolveExpectedArtifacts(List.of(expectedArtifact)) |
164 |
| - .getResolvedExpectedArtifacts() |
165 |
| - .stream() |
166 |
| - .findFirst() |
167 |
| - .flatMap(this::getBoundArtifact); |
168 |
| - } catch (InvalidRequestException e) { |
169 |
| - log.debug("Could not match inline artifact with trigger bound artifacts", e); |
170 |
| - return Optional.empty(); |
171 |
| - } |
| 146 | + return objectMapper.convertValue(evaluatedBoundArtifactMap, Artifact.class); |
172 | 147 | }
|
173 | 148 |
|
174 | 149 | public @Nullable Artifact getBoundArtifactForId(StageExecution stage, @Nullable String id) {
|
@@ -230,6 +205,107 @@ public List<Artifact> getArtifactsForPipelineIdWithoutStageRef(
|
230 | 205 | .orElse(Collections.emptyList());
|
231 | 206 | }
|
232 | 207 |
|
| 208 | + /** |
| 209 | + * The intention is to throw an exception if all the expected artifacts are not provided in the |
| 210 | + * trigger <br> |
| 211 | + * For now this is only for triggers of type pipeline |
| 212 | + * |
| 213 | + * @param pipeline |
| 214 | + * @throws InvalidRequestException if the trigger does not provide all the expected artifacts |
| 215 | + */ |
| 216 | + public void validateArtifactConstraintsFromTrigger(Map<String, Object> pipeline) { |
| 217 | + Map<String, Object> trigger = (Map<String, Object>) pipeline.get("trigger"); |
| 218 | + List<?> expectedArtifactIds = |
| 219 | + (List<?>) trigger.getOrDefault("expectedArtifactIds", emptyList()); |
| 220 | + |
| 221 | + ImmutableList<ExpectedArtifact> expectedArtifacts = |
| 222 | + Optional.ofNullable((List<?>) pipeline.get("expectedArtifacts")) |
| 223 | + .map(Collection::stream) |
| 224 | + .orElse(Stream.empty()) |
| 225 | + .map(it -> objectMapper.convertValue(it, ExpectedArtifact.class)) |
| 226 | + .filter( |
| 227 | + artifact -> |
| 228 | + expectedArtifactIds.contains(artifact.getId()) |
| 229 | + || artifact.isUseDefaultArtifact() |
| 230 | + || artifact.isUsePriorArtifact()) |
| 231 | + .collect(toImmutableList()); |
| 232 | + |
| 233 | + ImmutableSet<Artifact> receivedArtifacts = concatReceivedArtifacts(pipeline, trigger); |
| 234 | + |
| 235 | + ArtifactResolver.getInstance( |
| 236 | + receivedArtifacts, () -> getPriorArtifacts(pipeline), /* requireUniqueMatches= */ true) |
| 237 | + .resolveExpectedArtifacts(expectedArtifacts); |
| 238 | + } |
| 239 | + |
| 240 | + private ImmutableSet<Artifact> concatReceivedArtifacts( |
| 241 | + Map<String, Object> pipeline, Map<String, Object> trigger) { |
| 242 | + return Stream.concat( |
| 243 | + Optional.ofNullable((List<?>) pipeline.get("receivedArtifacts")) |
| 244 | + .map(Collection::stream) |
| 245 | + .orElse(Stream.empty()), |
| 246 | + Optional.ofNullable((List<?>) trigger.get("artifacts")) |
| 247 | + .map(Collection::stream) |
| 248 | + .orElse(Stream.empty())) |
| 249 | + .map(it -> objectMapper.convertValue(it, Artifact.class)) |
| 250 | + .collect(toImmutableSet()); |
| 251 | + } |
| 252 | + |
| 253 | + /** |
| 254 | + * This will only resolve the expected artifacts that match received artifacts. <br> |
| 255 | + * If the expected artifact is not present in received artifacts, it is not resolved. <br> |
| 256 | + * This resolves as many artifacts as possible, because there is no standard way to include all |
| 257 | + * the stages using expected artifacts. <br> |
| 258 | + * The best scenario is: the trigger pipeline provides all the expected artifacts. <br> |
| 259 | + * The worst scenario is: the pipeline will fail until the stage using the expected artifact is |
| 260 | + * executed. <br> |
| 261 | + * For now this is only for triggers of type pipeline |
| 262 | + * |
| 263 | + * @param pipeline |
| 264 | + */ |
| 265 | + public void resolveArtifactsFromTrigger(Map pipeline) { |
| 266 | + Map<String, Object> trigger = (Map<String, Object>) pipeline.get("trigger"); |
| 267 | + ImmutableList<ExpectedArtifact> expectedArtifacts = |
| 268 | + Optional.ofNullable((List<?>) pipeline.get("expectedArtifacts")) |
| 269 | + .map(Collection::stream) |
| 270 | + .orElse(Stream.empty()) |
| 271 | + .map(it -> objectMapper.convertValue(it, ExpectedArtifact.class)) |
| 272 | + .collect(toImmutableList()); |
| 273 | + |
| 274 | + ImmutableSet<Artifact> receivedArtifacts = concatReceivedArtifacts(pipeline, trigger); |
| 275 | + |
| 276 | + ArtifactResolver.ResolveResult resolveResult = |
| 277 | + ArtifactResolver.getInstance( |
| 278 | + receivedArtifacts, |
| 279 | + () -> getPriorArtifacts(pipeline), |
| 280 | + /* requireUniqueMatches= */ true) |
| 281 | + .resolveExpectedArtifacts(expectedArtifacts, false); |
| 282 | + |
| 283 | + ImmutableSet<Artifact> allArtifacts = |
| 284 | + ImmutableSet.<Artifact>builder() |
| 285 | + .addAll(receivedArtifacts) |
| 286 | + .addAll(resolveResult.getResolvedArtifacts()) |
| 287 | + .build(); |
| 288 | + |
| 289 | + try { |
| 290 | + trigger.put( |
| 291 | + "artifacts", |
| 292 | + objectMapper.readValue(objectMapper.writeValueAsString(allArtifacts), List.class)); |
| 293 | + trigger.put( |
| 294 | + "expectedArtifacts", |
| 295 | + objectMapper.readValue( |
| 296 | + objectMapper.writeValueAsString(resolveResult.getResolvedExpectedArtifacts()), |
| 297 | + List.class)); |
| 298 | + trigger.put( |
| 299 | + "resolvedExpectedArtifacts", |
| 300 | + objectMapper.readValue( |
| 301 | + objectMapper.writeValueAsString(resolveResult.getResolvedExpectedArtifacts()), |
| 302 | + List.class)); // Add the actual expectedArtifacts we included in the ids. |
| 303 | + } catch (IOException e) { |
| 304 | + throw new ArtifactResolutionException( |
| 305 | + "Failed to store artifacts in trigger: " + e.getMessage(), e); |
| 306 | + } |
| 307 | + } |
| 308 | + |
233 | 309 | private List<String> getExpectedArtifactIdsFromMap(Map<String, Object> trigger) {
|
234 | 310 | List<String> expectedArtifactIds = (List<String>) trigger.get("expectedArtifactIds");
|
235 | 311 | return (expectedArtifactIds != null) ? expectedArtifactIds : emptyList();
|
|
0 commit comments