Skip to content

Commit b120b1d

Browse files
committedNov 5, 2014
[FIX JENKINS-25449] Form validation for label expression
1 parent 1a1ae80 commit b120b1d

File tree

2 files changed

+108
-1
lines changed

2 files changed

+108
-1
lines changed
 

‎src/main/java/hudson/matrix/LabelExpAxis.java

+48-1
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,22 @@
2525

2626
import hudson.Extension;
2727
import hudson.Util;
28+
import hudson.model.AutoCompletionCandidates;
29+
import hudson.model.TopLevelItemDescriptor;
30+
import hudson.model.AbstractProject;
31+
import hudson.util.FormValidation;
2832
import jenkins.model.Jenkins;
2933

34+
import java.util.ArrayList;
3035
import java.util.LinkedList;
3136
import java.util.List;
3237

38+
import org.kohsuke.accmod.Restricted;
39+
import org.kohsuke.accmod.restrictions.DoNotUse;
40+
import org.kohsuke.accmod.restrictions.NoExternalUse;
41+
import org.kohsuke.stapler.AncestorInPath;
3342
import org.kohsuke.stapler.DataBoundConstructor;
43+
import org.kohsuke.stapler.QueryParameter;
3444

3545
/**
3646
* {@link Axis} that selects label expressions.
@@ -82,8 +92,45 @@ public boolean isInstantiable() {
8292
Jenkins h = Jenkins.getInstance();
8393
return !h.getNodes().isEmpty() || !h.clouds.isEmpty();
8494
}
95+
96+
@Restricted(NoExternalUse.class)
97+
public FormValidation doCheckLabelExpr(@AncestorInPath AbstractProject<?,?> project, @QueryParameter String value) {
98+
AbstractProject.AbstractProjectDescriptor desc = projectDescriptor();
99+
100+
if (Util.fixEmptyAndTrim(value) == null) return FormValidation.error("No expressions provided");
101+
102+
List<FormValidation> validations = new ArrayList<FormValidation>();
103+
for (String expr: getExprValues(value)) {
104+
validations.add(
105+
desc.doCheckAssignedLabelString(project, expr)
106+
);
107+
}
108+
109+
return aggregateValidations(validations);
110+
}
111+
112+
private final AbstractProject.AbstractProjectDescriptor projectDescriptor() {
113+
return Jenkins.getInstance().getDescriptorByType(MatrixProject.DescriptorImpl.class);
114+
}
85115
}
86-
116+
117+
// TODO move to hudson.util.FormValidation
118+
private static FormValidation aggregateValidations(List<FormValidation> validations) {
119+
120+
if (validations.size() == 1) return validations.get(0);
121+
122+
StringBuilder sb = new StringBuilder();
123+
sb.append("<ul style='list-style-type: none'>");
124+
for (FormValidation validation: validations) {
125+
sb.append("<li>").append(validation.renderHtml()).append("</li>");
126+
}
127+
sb.append("</ul>");
128+
129+
// Wrap into ok instead of worst of all results as class ok result
130+
// wrapped in warning or error overall result would inherit its color.
131+
return FormValidation.okWithMarkup(sb.toString());
132+
}
133+
87134
public static List<String> getExprValues(String valuesString){
88135
List<String> expressions = new LinkedList<String>();
89136
String[] exprs = valuesString.split("\n");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright (c) 2014 Red Hat, Inc.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
package hudson.matrix;
25+
26+
import static org.junit.Assert.*;
27+
import hudson.matrix.LabelExpAxis.DescriptorImpl;
28+
import hudson.util.FormValidation;
29+
30+
import org.junit.Rule;
31+
import org.junit.Test;
32+
import org.jvnet.hudson.test.JenkinsRule;
33+
34+
public class LabelExpAxisTest {
35+
36+
@Rule public JenkinsRule j = new JenkinsRule();
37+
38+
@Test
39+
public void test() throws Exception {
40+
j.jenkins.setLabelString("aaaa bbbb");
41+
42+
MatrixProject project = j.createMatrixProject("project");
43+
DescriptorImpl descriptor = new LabelExpAxis.DescriptorImpl();
44+
45+
assertEquals(FormValidation.Kind.ERROR, descriptor.doCheckLabelExpr(project, "").kind);
46+
47+
assertEquals(FormValidation.Kind.OK, descriptor.doCheckLabelExpr(project, "aaaa").kind);
48+
assertEquals(FormValidation.Kind.OK, descriptor.doCheckLabelExpr(project, "aaaa\nbbbb&&aaaa\n").kind);
49+
50+
assertEquals(FormValidation.Kind.WARNING, descriptor.doCheckLabelExpr(project, "no_such_slave").kind);
51+
52+
assertEquals(FormValidation.Kind.ERROR, descriptor.doCheckLabelExpr(project, "&&!||").kind);
53+
54+
// Use worse result - Not yet implemented
55+
//assertEquals(FormValidation.Kind.WARNING, descriptor.doCheckLabelExpr(project, "aaaa\nno_such_slave").kind);
56+
//assertEquals(FormValidation.Kind.ERROR, descriptor.doCheckLabelExpr(project, "aaaa\n&&").kind);
57+
//assertEquals(FormValidation.Kind.ERROR, descriptor.doCheckLabelExpr(project, "no_such_slave\n&&").kind);
58+
//assertEquals(FormValidation.Kind.ERROR, descriptor.doCheckLabelExpr(project, "no_such_slave\n&&\naaaa").kind);
59+
}
60+
}

0 commit comments

Comments
 (0)
Please sign in to comment.