Skip to content

Commit 0e281f0

Browse files
authored
Merge pull request #170 from jglick/CustomDescribableModel-JENKINS-44892
Demonstrating that API in JENKINS-44892 can be used as a replacement for the special methods in StepDescriptor
2 parents 4e3b1e5 + f558f83 commit 0e281f0

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

src/main/java/org/jenkinsci/plugins/docker/workflow/RegistryEndpointStep.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import hudson.model.Run;
3636
import hudson.model.TaskListener;
3737
import java.io.IOException;
38-
import java.util.Collections;
3938
import java.util.HashMap;
4039
import java.util.Map;
4140
import java.util.Objects;
@@ -44,6 +43,7 @@
4443
import org.jenkinsci.plugins.docker.commons.credentials.DockerRegistryEndpoint;
4544
import org.jenkinsci.plugins.docker.commons.credentials.KeyMaterialFactory;
4645
import org.jenkinsci.plugins.docker.commons.tools.DockerTool;
46+
import org.jenkinsci.plugins.structs.describable.CustomDescribableModel;
4747
import org.jenkinsci.plugins.structs.describable.UninstantiatedDescribable;
4848
import org.jenkinsci.plugins.workflow.steps.Step;
4949
import org.jenkinsci.plugins.workflow.steps.StepContext;
@@ -107,7 +107,7 @@ public static class Execution extends AbstractEndpointStepExecution {
107107

108108
}
109109

110-
@Extension public static class DescriptorImpl extends StepDescriptor {
110+
@Extension public static class DescriptorImpl extends StepDescriptor implements CustomDescribableModel {
111111

112112
@Override public String getFunctionName() {
113113
return "withDockerRegistry";
@@ -126,17 +126,7 @@ public static class Execution extends AbstractEndpointStepExecution {
126126
return true;
127127
}
128128

129-
@Override public UninstantiatedDescribable uninstantiate(Step step) throws UnsupportedOperationException {
130-
RegistryEndpointStep s = (RegistryEndpointStep) step;
131-
Map<String, Object> args = new TreeMap<>();
132-
args.put("url", s.registry.getUrl());
133-
args.put("credentialsId", s.registry.getCredentialsId());
134-
args.put("toolName", s.toolName);
135-
args.values().removeAll(Collections.singleton(null));
136-
return new UninstantiatedDescribable(args);
137-
}
138-
139-
@Override public Step newInstance(Map<String, Object> arguments) throws Exception {
129+
@Override public Map<String, Object> customInstantiate(Map<String, Object> arguments) {
140130
arguments = new HashMap<>(arguments);
141131
if (arguments.containsKey("url") || arguments.containsKey("credentialsId")) {
142132
if (arguments.containsKey("registry")) {
@@ -146,7 +136,18 @@ public static class Execution extends AbstractEndpointStepExecution {
146136
} else if (!arguments.containsKey("registry")) {
147137
throw new IllegalArgumentException("must specify url/credentialsId (or registry)");
148138
}
149-
return super.newInstance(arguments);
139+
return arguments;
140+
}
141+
142+
@Override public UninstantiatedDescribable customUninstantiate(UninstantiatedDescribable ud) {
143+
Object registry = ud.getArguments().get("registry");
144+
if (registry instanceof UninstantiatedDescribable) {
145+
Map<String, Object> arguments = new TreeMap<>(ud.getArguments());
146+
arguments.remove("registry");
147+
arguments.putAll(((UninstantiatedDescribable) registry).getArguments());
148+
return ud.withArguments(arguments);
149+
}
150+
return ud;
150151
}
151152

152153
@SuppressWarnings("unchecked")

src/test/java/org/jenkinsci/plugins/docker/workflow/RegistryEndpointStepTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,18 @@
7070
import java.util.HashMap;
7171
import java.util.Map;
7272
import java.util.Set;
73+
import java.util.logging.Level;
74+
import org.jenkinsci.plugins.structs.describable.DescribableModel;
75+
import org.jvnet.hudson.test.LoggerRule;
7376

7477
public class RegistryEndpointStepTest {
7578

7679
@Rule public JenkinsRule r = new JenkinsRule();
80+
@Rule public LoggerRule logging = new LoggerRule();
7781

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

0 commit comments

Comments
 (0)