Skip to content

Commit 6b2df22

Browse files
committed
Demonstrating that API in JENKINS-44892 can be used as a replacement for the special methods in StepDescriptor.
1 parent 4e6026a commit 6b2df22

File tree

3 files changed

+32
-16
lines changed

3 files changed

+32
-16
lines changed

pom.xml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@
3131
<properties>
3232
<revision>1.19</revision>
3333
<changelist>-SNAPSHOT</changelist>
34-
<jenkins.version>2.60.3</jenkins.version>
34+
<jenkins.version>2.121.1</jenkins.version>
3535
<java.level>8</java.level>
36-
<workflow-step-api-plugin.version>2.18</workflow-step-api-plugin.version>
36+
<useBeta>true</useBeta>
37+
<workflow-step-api-plugin.version>2.20-rc517.251dd3bdb066</workflow-step-api-plugin.version> <!-- TODO https://github.com/jenkinsci/workflow-step-api-plugin/pull/43 -->
3738
<workflow-support-plugin.version>2.12</workflow-support-plugin.version>
3839
<workflow-cps-plugin.version>2.25</workflow-cps-plugin.version>
3940
</properties>
@@ -130,4 +131,13 @@
130131
<scope>test</scope>
131132
</dependency>
132133
</dependencies>
134+
<dependencyManagement>
135+
<dependencies>
136+
<dependency>
137+
<groupId>org.jenkins-ci.plugins</groupId>
138+
<artifactId>structs</artifactId>
139+
<version>1.18-rc266.794506c68411</version> <!-- TODO https://github.com/jenkinsci/structs-plugin/pull/43 -->
140+
</dependency>
141+
</dependencies>
142+
</dependencyManagement>
133143
</project>

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

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import hudson.model.Run;
3434
import hudson.model.TaskListener;
3535
import java.io.IOException;
36-
import java.util.Collections;
3736
import java.util.HashMap;
3837
import java.util.Map;
3938
import java.util.Set;
@@ -43,6 +42,7 @@
4342
import org.jenkinsci.plugins.docker.commons.credentials.DockerRegistryEndpoint;
4443
import org.jenkinsci.plugins.docker.commons.credentials.KeyMaterialFactory;
4544
import org.jenkinsci.plugins.docker.commons.tools.DockerTool;
45+
import org.jenkinsci.plugins.structs.describable.CustomDescribableModel;
4646
import org.jenkinsci.plugins.structs.describable.UninstantiatedDescribable;
4747
import org.jenkinsci.plugins.workflow.steps.Step;
4848
import org.jenkinsci.plugins.workflow.steps.StepContext;
@@ -105,7 +105,7 @@ public static class Execution extends AbstractEndpointStepExecution {
105105

106106
}
107107

108-
@Extension public static class DescriptorImpl extends StepDescriptor {
108+
@Extension public static class DescriptorImpl extends StepDescriptor implements CustomDescribableModel {
109109

110110
@Override public String getFunctionName() {
111111
return "withDockerRegistry";
@@ -123,17 +123,7 @@ public static class Execution extends AbstractEndpointStepExecution {
123123
return true;
124124
}
125125

126-
@Override public UninstantiatedDescribable uninstantiate(Step step) throws UnsupportedOperationException {
127-
RegistryEndpointStep s = (RegistryEndpointStep) step;
128-
Map<String, Object> args = new TreeMap<>();
129-
args.put("url", s.registry.getUrl());
130-
args.put("credentialsId", s.registry.getCredentialsId());
131-
args.put("toolName", s.toolName);
132-
args.values().removeAll(Collections.singleton(null));
133-
return new UninstantiatedDescribable(args);
134-
}
135-
136-
@Override public Step newInstance(Map<String, Object> arguments) throws Exception {
126+
@Override public Map<String, Object> customInstantiate(Map<String, Object> arguments) {
137127
arguments = new HashMap<>(arguments);
138128
if (arguments.containsKey("url") || arguments.containsKey("credentialsId")) {
139129
if (arguments.containsKey("registry")) {
@@ -143,9 +133,20 @@ public static class Execution extends AbstractEndpointStepExecution {
143133
} else if (!arguments.containsKey("registry")) {
144134
throw new IllegalArgumentException("must specify url/credentialsId (or registry)");
145135
}
146-
return super.newInstance(arguments);
136+
return arguments;
147137
}
148138

139+
@Override public UninstantiatedDescribable customUninstantiate(UninstantiatedDescribable ud) {
140+
Object registry = ud.getArguments().get("registry");
141+
if (registry instanceof UninstantiatedDescribable) {
142+
Map<String, Object> arguments = new TreeMap<>(ud.getArguments());
143+
arguments.remove("registry");
144+
arguments.putAll(((UninstantiatedDescribable) registry).getArguments());
145+
return ud.withArguments(arguments);
146+
}
147+
return ud;
148+
}
149+
149150
@SuppressWarnings("unchecked")
150151
@Override public Set<? extends Class<?>> getRequiredContext() {
151152
return ImmutableSet.of(TaskListener.class, EnvVars.class, Node.class, Run.class, FilePath.class, Launcher.class);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,18 @@
7272
import java.util.HashMap;
7373
import java.util.Map;
7474
import java.util.Set;
75+
import java.util.logging.Level;
76+
import org.jenkinsci.plugins.structs.describable.DescribableModel;
77+
import org.jvnet.hudson.test.LoggerRule;
7578

7679
public class RegistryEndpointStepTest {
7780

7881
@Rule public JenkinsRule r = new JenkinsRule();
82+
@Rule public LoggerRule logging = new LoggerRule();
7983

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

0 commit comments

Comments
 (0)