/*
 * Copyright 2020 Google, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.netflix.spinnaker.orca.clouddriver.tasks.manifest;

import com.google.common.collect.ImmutableMap;
import com.netflix.spinnaker.kork.annotations.NonnullByDefault;
import com.netflix.spinnaker.orca.api.pipeline.Task;
import com.netflix.spinnaker.orca.api.pipeline.TaskResult;
import com.netflix.spinnaker.orca.api.pipeline.models.ExecutionStatus;
import com.netflix.spinnaker.orca.api.pipeline.models.StageExecution;
import javax.annotation.Nonnull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@NonnullByDefault
public final class ResolvePatchSourceManifestTask implements Task {
  public static final String TASK_NAME = "resolvePatchSourceManifest";

  private final ManifestEvaluator manifestEvaluator;

  @Autowired
  public ResolvePatchSourceManifestTask(ManifestEvaluator manifestEvaluator) {
    this.manifestEvaluator = manifestEvaluator;
  }

  @Nonnull
  @Override
  public TaskResult execute(@Nonnull StageExecution stage) {
    PatchManifestContext context = stage.mapTo(PatchManifestContext.class);
    ManifestEvaluator.Result result = manifestEvaluator.evaluate(stage, context);
    ImmutableMap<String, Object> outputs = getOutputs(result);
    return TaskResult.builder(ExecutionStatus.SUCCEEDED).context(outputs).outputs(outputs).build();
  }

  private ImmutableMap<String, Object> getOutputs(ManifestEvaluator.Result result) {
    return new ImmutableMap.Builder<String, Object>()
        .put("manifests", result.getManifests())
        .put("requiredArtifacts", result.getRequiredArtifacts())
        .put("allArtifacts", result.getOptionalArtifacts())
        .build();
  }
}

@@ -652,16 +652,64 @@
 ifest;%0A%0A
+import com.google.common.collect.ImmutableList;%0A
 import c
@@ -2254,16 +2254,29 @@
    .put(
+%0A            
 %22allArti
@@ -2274,33 +2274,148 @@
  %22allArtifacts%22,
- 
+%0A            ImmutableList.builder()%0A                .addAll(result.getRequiredArtifacts())%0A                .addAll(
 result.getOption
@@ -2421,32 +2421,58 @@
 nalArtifacts())%0A
+                .build())%0A
         .build()

Fix artifact binding in patch manifest stage (#3605)