From 5eb9dc7dd2e6875a0ad0304e01310c2ec6512106 Mon Sep 17 00:00:00 2001 From: Filipe Roque Date: Mon, 1 Apr 2024 18:19:40 +0100 Subject: [PATCH] Adds checks for user defined crontab entries --- .../DiskUsageProjectActionFactory.java | 28 +++++++++++++++++++ .../global.jelly | 6 ++-- .../plugins/disk_usage/Messages.properties | 1 + 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/main/java/hudson/plugins/disk_usage/DiskUsageProjectActionFactory.java b/src/main/java/hudson/plugins/disk_usage/DiskUsageProjectActionFactory.java index 2a440a51..633ab127 100644 --- a/src/main/java/hudson/plugins/disk_usage/DiskUsageProjectActionFactory.java +++ b/src/main/java/hudson/plugins/disk_usage/DiskUsageProjectActionFactory.java @@ -8,6 +8,7 @@ import hudson.model.Describable; import hudson.model.Descriptor; import hudson.model.TransientProjectActionFactory; +import hudson.scheduler.CronTab; import hudson.security.Permission; import java.util.ArrayList; import java.util.Collection; @@ -17,8 +18,11 @@ import java.util.logging.Level; import java.util.logging.Logger; import javax.servlet.ServletException; + +import hudson.util.FormValidation; import jenkins.model.Jenkins; import net.sf.json.JSONObject; +import org.kohsuke.stapler.QueryParameter; import org.kohsuke.stapler.StaplerRequest; /** @@ -378,6 +382,30 @@ private void configureBuildsCalculation(JSONObject form) { } } + + private FormValidation checkCrons(String cron){ + try { + final CronTab cronTab = new CronTab(cron); + final String sanity = cronTab.checkSanity(); + if (sanity == null){ + return FormValidation.ok(); + } else { + return FormValidation.warning(sanity); + } + } catch (IllegalArgumentException e){ + return FormValidation.error(Messages.InvalidCrontab(cron)); + } + } + public FormValidation doCheckCountIntervalBuilds(@QueryParameter String value){ + return checkCrons(value); + } + public FormValidation doCheckCountIntervalJobs(@QueryParameter String value){ + return checkCrons(value); + } + public FormValidation doCheckCountIntervalWorkspace(@QueryParameter String value){ + return checkCrons(value); + } + private void configureJobsCalculation(JSONObject form) { boolean oldCalculationJobs = calculationJobs; String oldcountIntervalJobs = countIntervalJobs; diff --git a/src/main/resources/hudson/plugins/disk_usage/DiskUsageProjectActionFactory/global.jelly b/src/main/resources/hudson/plugins/disk_usage/DiskUsageProjectActionFactory/global.jelly index d96042bc..69f63648 100644 --- a/src/main/resources/hudson/plugins/disk_usage/DiskUsageProjectActionFactory/global.jelly +++ b/src/main/resources/hudson/plugins/disk_usage/DiskUsageProjectActionFactory/global.jelly @@ -10,21 +10,21 @@ - + - + - + diff --git a/src/main/resources/hudson/plugins/disk_usage/Messages.properties b/src/main/resources/hudson/plugins/disk_usage/Messages.properties index 949a88b9..d1e194a5 100755 --- a/src/main/resources/hudson/plugins/disk_usage/Messages.properties +++ b/src/main/resources/hudson/plugins/disk_usage/Messages.properties @@ -8,3 +8,4 @@ DiskUsage.Graph.JobDirectory=job DiskUsage.Graph.BuildDirectory=builds DiskUsage.Graph.AgentWorkspaces=agent workspaces DiskUsage.Graph.NonAgentWorkspaces=non-agent workspaces +InvalidCrontab=Invalid cron {0}