Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds checks for user defined crontab entries #96

Merged
merged 1 commit into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@

<f:optionalBlock name="calculationBuilds" title="${%Enable calculation of builds}"
checked="${descriptor.isCalculationBuildsEnabled()}">
<f:entry title="${%Time interval for calculation}">
<f:entry title="${%Time interval for calculation}" field="countIntervalBuilds">
<f:textbox name="countIntervalBuilds" value="${descriptor.getCountIntervalForBuilds()}"/>
</f:entry>
</f:optionalBlock>

<f:optionalBlock name="calculationJobs" title="${%Enable calculation of jobs}"
checked="${descriptor.isCalculationJobsEnabled()}">
<f:entry title="${%Time interval for calculation}">
<f:entry title="${%Time interval for calculation}" field="countIntervalJobs">
<f:textbox name="countIntervalJobs" value="${descriptor.getCountIntervalForJobs()}"/>
</f:entry>
</f:optionalBlock>

<f:optionalBlock name="calculationWorkspace" title="${%Enable calculation of workspace}"
checked="${descriptor.isCalculationWorkspaceEnabled()}">
<f:entry title="${%Time interval for calculation}">
<f:entry title="${%Time interval for calculation}" field="countIntervalWorkspace">
<f:textbox name="countIntervalWorkspace" value="${descriptor.getCountIntervalForWorkspaces()}"/>
</f:entry>
</f:optionalBlock>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}