Skip to content

Commit

Permalink
Merge pull request #170 from jglick/CustomDescribableModel-JENKINS-44892
Browse files Browse the repository at this point in the history
Demonstrating that API in JENKINS-44892 can be used as a replacement for the special methods in StepDescriptor
  • Loading branch information
jglick committed Dec 2, 2022
2 parents 4e3b1e5 + f558f83 commit 0e281f0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
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

0 comments on commit 0e281f0

Please sign in to comment.