Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

git-task: default variables #76

Merged
merged 2 commits into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ public GitTask(GitSecretService secretService, Path processWorkDir) {
this.processWorkDir = processWorkDir;
}

public Map<String, Object> execute(Map<String, Object> in) throws Exception {
public Map<String, Object> execute(Map<String, Object> in, Map<String, Object> defaults) throws Exception {
in = merge(in, defaults);

Action action = getAction(in);
log.info("Starting '{}' action...", action);

Expand Down Expand Up @@ -201,7 +203,7 @@ private Map<String, Object> doPull(Map<String, Object> in) throws Exception {
log.info("Fetch result: '{}'", fetchResult);
}

log.info("Merge result: '{}'", mergeResult.toString());
log.info("Merge result: '{}'", mergeResult);
log.info("Merge status: '{}'", mergeStatus.toString().toUpperCase());

switch (mergeStatus) {
Expand All @@ -227,7 +229,7 @@ private Map<String, Object> doPull(Map<String, Object> in) throws Exception {
if (!fetchResult.isEmpty()) {
log.error("Fetch result: '{}'", result.getFetchResult().getMessages());
}
log.error("Merge result: '{}'", mergeResult.toString());
log.error("Merge result: '{}'", mergeResult);
log.error("Merge status: '{}'", mergeStatus.toString().toUpperCase());
throw new IllegalArgumentException("Git pull action failed. Please fix the above errors before retrying...");
}
Expand Down Expand Up @@ -613,6 +615,12 @@ private static String getHeadSHA(Path dstDir) throws RefNotFoundException {
}
}

private static Map<String, Object> merge(Map<String, Object> in, Map<String, Object> defaults) {
Map<String, Object> result = new HashMap<>(defaults != null ? defaults : Collections.emptyMap());
result.putAll(in);
return result;
}

private static Action getAction(Map<String, Object> in) {
String v = MapUtils.assertString(in, ACTION_KEY);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@

import com.walmartlabs.concord.plugins.git.GitSecretService;
import com.walmartlabs.concord.plugins.git.GitTask;
import com.walmartlabs.concord.sdk.Context;
import com.walmartlabs.concord.sdk.ContextUtils;
import com.walmartlabs.concord.sdk.SecretService;
import com.walmartlabs.concord.sdk.Task;
import com.walmartlabs.concord.sdk.*;

import javax.inject.Inject;
import javax.inject.Named;
Expand All @@ -41,6 +38,9 @@ public class GitTaskV1 implements Task {
private static final String OUT_KEY = "out";
private static final String DEFAULT_OUT_VAR_KEY = "result";

@InjectVariable("gitParams")
private Map<String, Object> defaults;

private final SecretService secretService;

@Inject
Expand All @@ -51,7 +51,7 @@ public GitTaskV1(SecretService secretService) {
@Override
public void execute(Context ctx) throws Exception {
Map<String, Object> result = new GitTask(new SecretServiceV1(secretService, ctx), ContextUtils.getWorkDir(ctx))
.execute(ctx.toMap());
.execute(ctx.toMap(), defaults);

String out = ContextUtils.getString(ctx, OUT_KEY, DEFAULT_OUT_VAR_KEY);
ctx.setVariable(out, result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,18 @@
@Named("git")
public class GitTaskV2 implements Task {

private final Context context;
private final GitTask delegate;

@Inject
public GitTaskV2(SecretService secretService, WorkingDirectory workDir) {
this.delegate = new GitTask(new SecretServiceV2(secretService), workDir.getValue());
public GitTaskV2(Context context) {
this.context = context;
this.delegate = new GitTask(new SecretServiceV2(context.secretService()), context.workingDirectory());
}

@Override
public TaskResult.SimpleResult execute(Variables input) throws Exception {
Map<String, Object> result = delegate.execute(input.toMap());
Map<String, Object> result = delegate.execute(input.toMap(), context.defaultVariables().toMap());

// we can't change the delegate's return type w/o breaking compatibility with Concord < 1.62.0
// and we can't break that right now as we don't have a way to properly communicate this breakage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,20 @@
import com.walmartlabs.concord.plugins.git.GitTask;
import com.walmartlabs.concord.plugins.git.TokenSecret;
import com.walmartlabs.concord.plugins.git.Utils;
import com.walmartlabs.concord.runtime.v2.sdk.Context;
import com.walmartlabs.concord.runtime.v2.sdk.MapBackedVariables;
import com.walmartlabs.concord.runtime.v2.sdk.SecretService;
import com.walmartlabs.concord.runtime.v2.sdk.TaskResult;
import com.walmartlabs.concord.runtime.v2.sdk.WorkingDirectory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class GitTaskV2Test {

Expand All @@ -63,7 +61,12 @@ public void test() throws Exception {
input.put(GitTask.ACTION_KEY, GitTask.Action.CLONE.name());
input.put(GitTask.GIT_URL, "https://github.com/walmartlabs/concord-plugins.git");

GitTaskV2 task = new GitTaskV2(mock(SecretService.class), new WorkingDirectory(workDir));
Context context = mock(Context.class);
when(context.secretService()).thenReturn(mock(SecretService.class));
when(context.workingDirectory()).thenReturn(workDir);
when(context.defaultVariables()).thenReturn(new MapBackedVariables(Collections.emptyMap()));

GitTaskV2 task = new GitTaskV2(context);
TaskResult.SimpleResult result = task.execute(new MapBackedVariables(input));
assertTrue(result.ok());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,6 @@ private static Variables merge(Variables variables, Map<String, Object> defaults
return new MapBackedVariables(variablesMap);
}


public static <E extends Enum<E>> E assertEnum(Variables variables, String name, Class<E> enumData) {
E result = getEnum(variables, name, enumData, null);
if (result != null) {
Expand Down