From 4579c0c723c41f92838a65c93e647de60d991969 Mon Sep 17 00:00:00 2001 From: Mark Waite Date: Mon, 4 Sep 2023 04:59:44 -0600 Subject: [PATCH] Convert empty or blank name to non-blank in readResolve Jenkins 2.403 improves the cloud management user interface. Each cloud is now managed from a separate page. That separate page requires a non-blank name for the cloud. Jenkins 2.402 and later allowed a blank name. When a blank name is detected, convert it to a non-blank name based on the hashCode of the DockerAPI and templates. --- .../jenkins/plugins/docker/DockerCloud.java | 11 ++++++++++- .../jenkins/plugins/docker/DockerCloudTest.java | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/nirima/jenkins/plugins/docker/DockerCloud.java b/src/main/java/com/nirima/jenkins/plugins/docker/DockerCloud.java index ef479b8a..38805850 100644 --- a/src/main/java/com/nirima/jenkins/plugins/docker/DockerCloud.java +++ b/src/main/java/com/nirima/jenkins/plugins/docker/DockerCloud.java @@ -143,6 +143,13 @@ public DockerCloud(String name, DockerAPI dockerApi, List templa } } + /* Constructor to create a new DockerCloud based on an existing DockerCloud */ + public DockerCloud(@NonNull String name, @NonNull DockerCloud source) { + super(name); + this.dockerApi = source.dockerApi; + this.templates = source.templates; + } + @Deprecated public DockerCloud( String name, @@ -731,7 +738,9 @@ protected Object readResolve() { } if (name == null || name.isBlank()) { - LOGGER.warn("Docker cloud requires a non-blank name after Jenkins 2.402"); + String newName = "docker-cloud-" + Integer.toHexString(hashCode()); + LOGGER.warn("Docker cloud requires a non-blank name after Jenkins 2.402, using '" + newName + "'"); + return new DockerCloud(newName, this); } return this; diff --git a/src/test/java/com/nirima/jenkins/plugins/docker/DockerCloudTest.java b/src/test/java/com/nirima/jenkins/plugins/docker/DockerCloudTest.java index 64a7a761..b1db16be 100644 --- a/src/test/java/com/nirima/jenkins/plugins/docker/DockerCloudTest.java +++ b/src/test/java/com/nirima/jenkins/plugins/docker/DockerCloudTest.java @@ -78,6 +78,21 @@ public void testConstructorWithNullName() { MatcherAssert.assertThat(lr.getMessages(), IsIterableContaining.hasItem(LOG_MESSAGE)); } + @Issue("JENKINS-70729") // Handle null or empty cloud name + @Test + public void testCopyConstructor() { + lr.record(DockerCloud.class.getName(), Level.ALL).capture(16); + DockerCloud cloud = + new DockerCloud(null, new DockerAPI(new DockerServerEndpoint("uri", "credentialsId")), List.of()); + Assert.assertEquals(cloud.getDisplayName(), null); + MatcherAssert.assertThat(lr.getMessages(), IsIterableContaining.hasItem(LOG_MESSAGE)); + String newName = "docker-cloud-" + Integer.toHexString(cloud.hashCode()); + DockerCloud copy = new DockerCloud(newName, cloud); + Assert.assertEquals(cloud.getDockerApi(), copy.getDockerApi()); + Assert.assertEquals(cloud.getTemplates().hashCode(), copy.getTemplates().hashCode()); + Assert.assertEquals(newName, copy.getDisplayName()); + } + @Test public void globalConfigRoundtrip() throws Exception {