Skip to content

Commit

Permalink
[JENKINS-47793] - Jenkins master and agent configuration pages do not…
Browse files Browse the repository at this point in the history
… 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 2eda781
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 5 deletions.
5 changes: 5 additions & 0 deletions core/src/main/java/hudson/model/Computer.java
Expand Up @@ -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);

Expand Down
12 changes: 11 additions & 1 deletion core/src/main/java/jenkins/model/Jenkins.java
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down
Expand Up @@ -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")));
Expand Down
Expand Up @@ -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">
Expand Down
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions core/src/main/resources/jenkins/model/Messages.properties
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions war/src/main/webapp/scripts/hudson-behavior.js
Expand Up @@ -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
Expand Down

0 comments on commit 2eda781

Please sign in to comment.