Skip to content
Permalink
Browse files

[JENKINS-47793] - Jenkins master and agent configuration pages do not…

… verify negative executor numbers (#3141)

* Fixed Regexps for number & positive-number

* Progress saving

* Fix for validation

* bug fixing

* bug fixing

* Address comments

* Address comments

* Address comments from @jglick

* Whitespace cleanup

* Address comment for @jglick
  • Loading branch information...
ksenia-nenasheva authored and oleg-nenashev committed Jan 30, 2018
1 parent e86e6f9 commit 2eda7816e8007851ced3d618cd0fe42c78a486e2
@@ -1472,6 +1472,11 @@ public void doConfigSubmit( StaplerRequest req, StaplerResponse rsp ) throws IOE
throw new FormException(Messages.ComputerSet_SlaveAlreadyExists(proposedName), "name");
}

String nExecutors = req.getSubmittedForm().getString("numExecutors");
if (Integer.parseInt(nExecutors)<=0) {
throw new FormException(Messages.Slave_InvalidConfig_Executors(nodeName), "numExecutors");
}

Node result = node.reconfigure(req, req.getSubmittedForm());
Jenkins.getInstance().getNodesObject().replaceNode(this.getNode(), result);

@@ -290,6 +290,7 @@
import hudson.init.Initializer;
import hudson.util.LogTaskListener;
import static java.util.logging.Level.*;
import javax.annotation.Nonnegative;
import static javax.servlet.http.HttpServletResponse.*;
import org.kohsuke.stapler.WebMethod;

@@ -2713,7 +2714,16 @@ public InitMilestone getInitLevel() {
return initLevel;
}

public void setNumExecutors(int n) throws IOException {
/**
* Sets a number of executors.
* @param n Number of executors
* @throws IOException Failed to save the configuration
* @throws IllegalArgumentException Negative value has been passed
*/
public void setNumExecutors(@Nonnegative int n) throws IOException, IllegalArgumentException {
if (n < 0) {
throw new IllegalArgumentException("Incorrect field \"# of executors\": " + n +". It should be a non-negative number.");
}
if (this.numExecutors != n) {
this.numExecutors = n;
updateComputerList();
@@ -51,6 +51,11 @@ public boolean configure(StaplerRequest req, JSONObject json) throws FormExcepti
Jenkins j = Jenkins.getInstance();
try {
// for compatibility reasons, this value is stored in Jenkins
String num = json.getString("numExecutors");
if (!num.matches("\\d+")) {
throw new FormException(Messages.Hudson_Computer_IncorrectNumberOfExecutors(),"numExecutors");
}

j.setNumExecutors(json.getInt("numExecutors"));
if (req.hasParameter("master.mode"))
j.setMode(Mode.valueOf(req.getParameter("master.mode")));
@@ -42,7 +42,7 @@ THE SOFTWARE.
-->

<f:entry title="${%# of executors}" field="numExecutors">
<f:number clazz="positive-number" min="0" step="1"/>
<f:number clazz="non-negative-number" min="0" step="1"/>
</f:entry>

<f:entry title="${%Labels}" field="labelString">
@@ -3,7 +3,7 @@ package jenkins.model.MasterBuildConfiguration
def f=namespace(lib.FormTagLib)

f.entry(title:_("# of executors"), field:"numExecutors") {
f.number(clazz:"number", min:0, step:1)
f.number(clazz:"non-negative-number", min:0, step:1)
}
f.entry(title:_("Labels"),field:"labelString") {
f.textbox()
@@ -26,6 +26,7 @@
Hudson.BadPortNumber=Bad port number {0}
Hudson.Computer.Caption=Master
Hudson.Computer.DisplayName=master
Hudson.Computer.IncorrectNumberOfExecutors=Incorrect field "# of executors". It should be a non-negative number.
Hudson.ControlCodeNotAllowed=No control code is allowed: {0}
Hudson.DisplayName=Jenkins
Hudson.JobAlreadyExists=A job already exists with the name \u2018{0}\u2019
@@ -694,9 +694,12 @@ var jenkinsRules = {
"INPUT.required" : function(e) { registerRegexpValidator(e,/./,"Field is required"); },

// validate form values to be an integer
"INPUT.number" : function(e) { registerRegexpValidator(e,/^(\d+|)$/,"Not an integer"); },
"INPUT.number" : function(e) { registerRegexpValidator(e,/^\-?(\d+)$/,"Not an integer"); },
"INPUT.non-negative-number" : function(e) {
registerRegexpValidator(e,/^\d+$/,"Not a non-negative number");
},
"INPUT.positive-number" : function(e) {
registerRegexpValidator(e,/^(\d*[1-9]\d*|)$/,"Not a positive integer");
registerRegexpValidator(e,/^[1-9]\d*$/,"Not a positive integer");
},

"INPUT.auto-complete": function(e) {// form field with auto-completion support

0 comments on commit 2eda781

Please sign in to comment.
You can’t perform that action at this time.