From f72bc23b0c304f8b453107a42b7cfc2988ac1499 Mon Sep 17 00:00:00 2001 From: Filipe Roque Date: Thu, 2 Mar 2023 01:52:26 +0000 Subject: [PATCH] Fixes class cast exception with promoted builds plugin Manually cherry picked from commits 1ea3c76902f09a8e7fe27a570aad5db83318bbd3 and 5842185ddfcbfe9cec035dba3ed20a623ef1d60b JENKINS-40728 Invalid class cast in Disk Usage Plugin when ItemGroup is not a TopLevelItem --- pom.xml | 16 ++++++-- .../plugins/disk_usage/DiskUsageProperty.java | 37 ++++++++++++------- .../integration/DiskUsagePropertyTest.java | 19 ++++++++++ 3 files changed, 55 insertions(+), 17 deletions(-) diff --git a/pom.xml b/pom.xml index d56f3e6d..560470c1 100644 --- a/pom.xml +++ b/pom.xml @@ -29,9 +29,9 @@ - org.jenkins-ci.plugins - mailer - 435.v79ef3972b_5c7 + org.jenkins-ci.plugins + mailer + 435.v79ef3972b_5c7 org.jenkins-ci.plugins @@ -41,13 +41,21 @@ org.jenkins-ci.plugins junit - 1.0 + 1.20 io.jenkins.plugins jakarta-mail-api 2.0.1-2 + + org.jenkins-ci.plugins + promoted-builds + 3.0 + test + + + diff --git a/src/main/java/hudson/plugins/disk_usage/DiskUsageProperty.java b/src/main/java/hudson/plugins/disk_usage/DiskUsageProperty.java index 9170c660..6136a48b 100644 --- a/src/main/java/hudson/plugins/disk_usage/DiskUsageProperty.java +++ b/src/main/java/hudson/plugins/disk_usage/DiskUsageProperty.java @@ -364,12 +364,14 @@ public Long getAllNonAgentOrCustomWorkspaceSize() { } Map paths = getAgentWorkspaceUsage().get(nodeName); for(Entry entry : paths.entrySet()) { - TopLevelItem item = null; + Item item = null; if(owner instanceof TopLevelItem) { - item = (TopLevelItem) owner; + item = owner; } else { - item = (TopLevelItem) owner.getParent(); + if (owner.getParent() instanceof TopLevelItem){ + item = (TopLevelItem) owner.getParent(); + } } try { if(!isContainedInWorkspace(item, node, entry.getKey())) { @@ -384,24 +386,33 @@ public Long getAllNonAgentOrCustomWorkspaceSize() { return size; } - private boolean isContainedInWorkspace(TopLevelItem item, Node node, String path) { + private boolean isContainedInWorkspace(Item item, Node node, String path) { if(node instanceof Slave) { Slave agent = (Slave) node; return path.contains(agent.getRemoteFS()); } else { - if(node instanceof Jenkins) { - FilePath file = Jenkins.getInstance().getWorkspaceFor(item); - return path.contains(file.getRemote()); - } - else { - try { - return path.contains(node.getWorkspaceFor(item).getRemote()); + if (item instanceof TopLevelItem){ + TopLevelItem topLevelItem = (TopLevelItem) item; + if(node instanceof Jenkins) { + FilePath file = Jenkins.getInstance().getWorkspaceFor(topLevelItem); + if (file != null){ + return path.contains(file.getRemote()); + } } - catch (Exception e) { - return false; + else { + try { + final var file = node.getWorkspaceFor(topLevelItem); + if (file != null){ + return path.contains(file.getRemote()); + } + } + catch (Exception e) { + return false; + } } } + return false; } } diff --git a/src/test/java/hudson/plugins/disk_usage/integration/DiskUsagePropertyTest.java b/src/test/java/hudson/plugins/disk_usage/integration/DiskUsagePropertyTest.java index ba87c69d..3b9f4815 100644 --- a/src/test/java/hudson/plugins/disk_usage/integration/DiskUsagePropertyTest.java +++ b/src/test/java/hudson/plugins/disk_usage/integration/DiskUsagePropertyTest.java @@ -2,6 +2,11 @@ import hudson.Functions; +import hudson.model.FreeStyleBuild; +import hudson.model.Project; +import hudson.plugins.promoted_builds.JobPropertyImpl; +import hudson.plugins.promoted_builds.PromotionProcess; +import hudson.plugins.promoted_builds.conditions.SelfPromotionCondition; import hudson.tasks.BatchFile; import java.util.ConcurrentModificationException; import java.util.GregorianCalendar; @@ -51,6 +56,20 @@ public class DiskUsagePropertyTest { @Rule public JenkinsRule j = new JenkinsRule(); + @Issue("JENKINS-40728") + @Test + public void testCalculationWorkspaceForItemInNonTopLeverGroupItem() throws Exception { + final var project = j.createFreeStyleProject("some-project"); + JobPropertyImpl property = new JobPropertyImpl(project); + project.addProperty(property); + PromotionProcess process = property.addProcess("Simple-process"); + process.conditions.add(new SelfPromotionCondition(true)); + process.getBuildSteps().add(new Shell("echo hello > log.log")); + j.buildAndAssertSuccess(project); + DiskUsageProperty p = process.getProperty(DiskUsageProperty.class); + Thread.sleep(1000); + p.getAllNonSlaveOrCustomWorkspaceSize(); + } @Test public void testGetAllDiskUsageWithoutBuilds() throws Exception {