Skip to content

Commit

Permalink
Merge pull request #1009 from stephenc/master
Browse files Browse the repository at this point in the history
[FIXED JENKINS-20514] Add a core extension point to allow plugins to con...
  • Loading branch information
olivergondza committed Nov 11, 2013
2 parents 634d30e + 53f754e commit a6914c5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
4 changes: 4 additions & 0 deletions changelog.html
Expand Up @@ -84,6 +84,10 @@
<li class=bug>
Global search box now remembers entered text
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-18192">issue 18192</a>)
<li class=rfe>
Add extension point to allow plugins to contribute to the checking of
assigned labels.
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-20514">issue 20514</a>)
</ul>
</div><!--=TRUNK-END=-->

Expand Down
34 changes: 32 additions & 2 deletions core/src/main/java/hudson/model/AbstractProject.java
Expand Up @@ -29,6 +29,7 @@

import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
import hudson.EnvVars;
import hudson.ExtensionPoint;
import hudson.Functions;
import antlr.ANTLRException;
import hudson.AbortException;
Expand Down Expand Up @@ -103,6 +104,7 @@
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.stapler.Ancestor;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.ForwardToView;
import org.kohsuke.stapler.HttpRedirect;
import org.kohsuke.stapler.HttpResponse;
Expand Down Expand Up @@ -2209,7 +2211,8 @@ public boolean isApplicable(Descriptor descriptor) {
return true;
}

public FormValidation doCheckAssignedLabelString(@QueryParameter String value) {
public FormValidation doCheckAssignedLabelString(@AncestorInPath AbstractProject<?,?> project,
@QueryParameter String value) {
if (Util.fixEmpty(value)==null)
return FormValidation.ok(); // nothing typed yet
try {
Expand All @@ -2228,6 +2231,15 @@ public FormValidation doCheckAssignedLabelString(@QueryParameter String value) {
}
return FormValidation.warning(Messages.AbstractProject_AssignedLabelString_NoMatch());
}
if (project != null) {
for (AbstractProject.LabelValidator v : Jenkins.getInstance()
.getExtensionList(AbstractProject.LabelValidator.class)) {
FormValidation result = v.check(project, l);
if (!FormValidation.Kind.OK.equals(result.kind)) {
return result;
}
}
}
return FormValidation.ok();
}

Expand Down Expand Up @@ -2384,5 +2396,23 @@ public void setCustomWorkspace(String customWorkspace) throws IOException {
this.customWorkspace= Util.fixEmptyAndTrim(customWorkspace);
save();
}


/**
* Plugins may want to contribute additional restrictions on the use of specific labels for specific projects.
* This extension point allows such restrictions.
*
* @since 1.540
*/
public static abstract class LabelValidator implements ExtensionPoint {
/**
* Check the use of the label within the specified context.
*
* @param project the project that wants to restrict itself to the specified label.
* @param label the label that the project wants to restrict itself to.
* @return the {@link FormValidation} result.
*/
@Nonnull
public abstract FormValidation check(@Nonnull AbstractProject<?, ?> project, @Nonnull Label label);
}

}
Expand Up @@ -233,11 +233,11 @@ public Object call() throws Exception {

Label l = jenkins.getLabel("foo");
DumbSlave s = createSlave(l);
String msg = d.doCheckAssignedLabelString("goo").renderHtml();
String msg = d.doCheckAssignedLabelString(null, "goo").renderHtml();
assertTrue(msg.contains("foo"));
assertTrue(msg.contains("goo"));

msg = d.doCheckAssignedLabelString("master && goo").renderHtml();
msg = d.doCheckAssignedLabelString(null, "master && goo").renderHtml();
assertTrue(msg.contains("foo"));
assertTrue(msg.contains("goo"));
return null;
Expand Down

0 comments on commit a6914c5

Please sign in to comment.