Skip to content

Commit

Permalink
Fixes class cast exception with promoted builds plugin
Browse files Browse the repository at this point in the history
Manually cherry picked from commits 1ea3c76
and 5842185

JENKINS-40728 Invalid class cast in Disk Usage Plugin when ItemGroup is not a TopLevelItem
  • Loading branch information
froque committed Mar 2, 2023
1 parent c903885 commit f72bc23
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 17 deletions.
16 changes: 12 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@

<dependencies>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>mailer</artifactId>
<version>435.v79ef3972b_5c7</version>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>mailer</artifactId>
<version>435.v79ef3972b_5c7</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand All @@ -41,13 +41,21 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>junit</artifactId>
<version>1.0</version>
<version>1.20</version>
</dependency>
<dependency>
<groupId>io.jenkins.plugins</groupId>
<artifactId>jakarta-mail-api</artifactId>
<version>2.0.1-2</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>promoted-builds</artifactId>
<version>3.0</version>
<scope>test</scope>
</dependency>


</dependencies>

<build>
Expand Down
37 changes: 24 additions & 13 deletions src/main/java/hudson/plugins/disk_usage/DiskUsageProperty.java
Original file line number Diff line number Diff line change
Expand Up @@ -364,12 +364,14 @@ public Long getAllNonAgentOrCustomWorkspaceSize() {
}
Map<String, Long> paths = getAgentWorkspaceUsage().get(nodeName);
for(Entry<String, Long> 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())) {
Expand All @@ -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;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit f72bc23

Please sign in to comment.