Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Demonstrating that API in JENKINS-44892 can be used as a replacement for the special methods in StepDescriptor #170

Merged
merged 10 commits into from
Dec 2, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import hudson.model.Run;
import hudson.model.TaskListener;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
Expand All @@ -44,6 +43,7 @@
import org.jenkinsci.plugins.docker.commons.credentials.DockerRegistryEndpoint;
import org.jenkinsci.plugins.docker.commons.credentials.KeyMaterialFactory;
import org.jenkinsci.plugins.docker.commons.tools.DockerTool;
import org.jenkinsci.plugins.structs.describable.CustomDescribableModel;
import org.jenkinsci.plugins.structs.describable.UninstantiatedDescribable;
import org.jenkinsci.plugins.workflow.steps.Step;
import org.jenkinsci.plugins.workflow.steps.StepContext;
Expand Down Expand Up @@ -107,7 +107,7 @@ public static class Execution extends AbstractEndpointStepExecution {

}

@Extension public static class DescriptorImpl extends StepDescriptor {
@Extension public static class DescriptorImpl extends StepDescriptor implements CustomDescribableModel {

@Override public String getFunctionName() {
return "withDockerRegistry";
Expand All @@ -126,17 +126,7 @@ public static class Execution extends AbstractEndpointStepExecution {
return true;
}

@Override public UninstantiatedDescribable uninstantiate(Step step) throws UnsupportedOperationException {
RegistryEndpointStep s = (RegistryEndpointStep) step;
Map<String, Object> args = new TreeMap<>();
args.put("url", s.registry.getUrl());
args.put("credentialsId", s.registry.getCredentialsId());
args.put("toolName", s.toolName);
args.values().removeAll(Collections.singleton(null));
return new UninstantiatedDescribable(args);
}

@Override public Step newInstance(Map<String, Object> arguments) throws Exception {
@Override public Map<String, Object> customInstantiate(Map<String, Object> arguments) {
arguments = new HashMap<>(arguments);
if (arguments.containsKey("url") || arguments.containsKey("credentialsId")) {
if (arguments.containsKey("registry")) {
Expand All @@ -146,7 +136,18 @@ public static class Execution extends AbstractEndpointStepExecution {
} else if (!arguments.containsKey("registry")) {
throw new IllegalArgumentException("must specify url/credentialsId (or registry)");
}
return super.newInstance(arguments);
return arguments;
}

@Override public UninstantiatedDescribable customUninstantiate(UninstantiatedDescribable ud) {
Object registry = ud.getArguments().get("registry");
if (registry instanceof UninstantiatedDescribable) {
Map<String, Object> arguments = new TreeMap<>(ud.getArguments());
arguments.remove("registry");
arguments.putAll(((UninstantiatedDescribable) registry).getArguments());
return ud.withArguments(arguments);
}
return ud;
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,18 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import org.jenkinsci.plugins.structs.describable.DescribableModel;
import org.jvnet.hudson.test.LoggerRule;

public class RegistryEndpointStepTest {

@Rule public JenkinsRule r = new JenkinsRule();
@Rule public LoggerRule logging = new LoggerRule();

@Issue("JENKINS-51395")
@Test public void configRoundTrip() throws Exception {
logging.record(DescribableModel.class, Level.FINE);
{ // Recommended syntax.
SnippetizerTester st = new SnippetizerTester(r);
RegistryEndpointStep step = new RegistryEndpointStep(new DockerRegistryEndpoint("https://myreg/", null));
Expand Down