Skip to content

Commit

Permalink
Merge pull request #32 from jglick/branch-property-filtering-JENKINS-…
Browse files Browse the repository at this point in the history
…32670

[JENKINS-32670] Multibranch Pipelines have no applicable branch properties
  • Loading branch information
jglick committed Mar 15, 2016
2 parents feae9aa + 84e998a commit f6db79c
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 12 deletions.
3 changes: 0 additions & 3 deletions src/main/java/jenkins/branch/BranchPropertyDescriptor.java
Expand Up @@ -89,9 +89,6 @@ public static List<BranchPropertyDescriptor> all(@NonNull MultiBranchProject pro

/**
* Ensures that the configuration screen of (for example) {@link DefaultBranchPropertyStrategy} shows only appropriate descriptors.
* TODO this trick does not work for {@link NamedExceptionsBranchPropertyStrategy} on initial configuration (i.e., when {@link DefaultBranchPropertyStrategy} was initially selected);
* seems {@code filterDescriptors} is not called when {@code instance == null} perhaps?
* Or perhaps {@code it == null} when {@code l:renderOnDemand} is in use (supposed to work due to {@code capture} attribute, but…)?
*/
@Restricted(DoNotUse.class)
@Extension
Expand Down
Expand Up @@ -26,6 +26,7 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.ExtensionList;
import hudson.model.Descriptor;
import hudson.model.DescriptorVisibilityFilter;
import jenkins.scm.api.SCMSourceDescriptor;

import java.util.ArrayList;
Expand All @@ -46,21 +47,24 @@ public abstract class BranchPropertyStrategyDescriptor extends Descriptor<Branch
* @param sourceDescriptor the source descriptor.
* @return {@code} true iff this property strategy is relevant with this source.
*/
@SuppressWarnings("unused") // by stapler
public boolean isApplicable(@NonNull SCMSourceDescriptor sourceDescriptor) {
return true;
}

/**
* A branch property strategy may not be appropriate for every project, this method lets a strategy
* opt out of being selectable for a specific project.
*
* <p>By default it checks {@link #isApplicable(MultiBranchProjectDescriptor)},
* and whether {@link BranchPropertyDescriptor#all(MultiBranchProject)} is nonempty
* when filtered by {@link DescriptorVisibilityFilter} on the project,
* which due to {@link jenkins.branch.BranchPropertyDescriptor.Visibility}
* also calls {@link BranchPropertyDescriptor#isApplicable(MultiBranchProject)}.
* @param project the project.
* @return {@code} true iff this property strategy is relevant with this project instance.
*/
@SuppressWarnings("unused") // by stapler
public boolean isApplicable(@NonNull MultiBranchProject project) {
return isApplicable(project.getDescriptor());
return isApplicable(project.getDescriptor()) &&
!DescriptorVisibilityFilter.apply(project, BranchPropertyDescriptor.all()).isEmpty();
}

/**
Expand Down
12 changes: 7 additions & 5 deletions src/main/resources/jenkins/branch/BranchSource/config.jelly
Expand Up @@ -22,7 +22,7 @@
~ THE SOFTWARE.
-->
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form" xmlns:branch="/lib/branch-api">
<tr name="source">
<td colspan="3">
<input type="hidden" name="stapler-class" value="${sourceDescriptor.clazz.name}"/>
Expand All @@ -35,8 +35,10 @@
</table>
</td>
</tr>
<f:block>
<f:dropdownDescriptorSelector field="strategy" title="Property strategy"
descriptors="${descriptor.propertyStrategyDescriptors(it,sourceDescriptor)}"/>
</f:block>
<j:set var="descriptors" value="${descriptor.propertyStrategyDescriptors(it,sourceDescriptor)}"/>
<j:if test="${!descriptors.isEmpty()}">
<f:block>
<branch:dropdownDescriptorSelector field="strategy" title="Property strategy" descriptors="${descriptors}"/>
</f:block>
</j:if>
</j:jelly>
72 changes: 72 additions & 0 deletions src/main/resources/lib/branch-api/dropdownDescriptorSelector.jelly
@@ -0,0 +1,72 @@
<!--
The MIT License
Copyright (c) 2010, InfraDNA, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<!-- TODO 1.645+ use standard /lib/form version -->
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<st:documentation>
Renders a single &lt;select> control for choosing a Describable.
Depending on the currently selected value, its config.jelly will be
rendered below &lt;select, allowing the user to configure Describable.

<st:attribute name="field" use="required">
form field name. Used for databinding.
</st:attribute>
<st:attribute name="title" use="required">
Human readable title of this control.
</st:attribute>
<st:attribute name="descriptors">
Collection that lists up all the valid candidate descriptors.
If unspecified, inferred from the type of the field.
</st:attribute>
<st:attribute name="default">
If specified, this will be chosen as the default value in case the current selection is null. The default can be an specific instance or a descriptor e.g.
${descriptor.defaultSettingsProvider} or ${descriptor.defaultSettingsProvider.descriptor}. In the later case, the from input fields will be empty.
</st:attribute>
<st:attribute name="capture">
Config fragments from descriptors are rendered lazily by default, which means
variables seen in the caller aren't visible to them. This attribute allows you
to nominate additional variables and their values to be captured for descriptors.
</st:attribute>
</st:documentation>
<f:prepareDatabinding />
<j:set target="${attrs}" property="descriptors" value="${attrs.descriptors ?: descriptor.getPropertyType(instance,attrs.field).getApplicableDescriptors()}" />

<f:dropdownList name="${attrs.field}" title="${attrs.title}" help="${descriptor.getHelpFile(attrs.field)}">
<d:invokeBody />

<j:set var="current" value="${instance[attrs.field]}"/>
<j:set var="current" value="${current!=null ? current : (default.descriptor!=null ? default : null)}"/>
<j:set var="capture" value="${attrs.capture?:''}" />
<j:forEach var="descriptor" items="${attrs.descriptors}" varStatus="loop">
<f:dropdownListBlock value="${loop.index}" title="${descriptor.displayName}"
selected="${current.descriptor==descriptor || (current==null and descriptor==attrs.default)}" staplerClass="${descriptor.clazz.name}"
lazy="descriptor,it,${capture}">
<l:ajax>
<j:set var="instance" value="${current.descriptor==descriptor ? current : null}" />
<st:include from="${descriptor}" page="${descriptor.configPage}" optional="true" />
</l:ajax>
</f:dropdownListBlock>
</j:forEach>
</f:dropdownList>
</j:jelly>

0 comments on commit f6db79c

Please sign in to comment.