Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TESTDONOTMERGE] #195

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 67 additions & 5 deletions src/main/java/jenkins/branch/OrganizationFolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@
import jenkins.scm.api.metadata.ObjectMetadataAction;
import jenkins.scm.impl.SingleSCMNavigator;
import jenkins.scm.impl.UncategorizedSCMSourceCategory;
import net.sf.json.JSONObject;

import org.acegisecurity.AccessDeniedException;
import org.acegisecurity.Authentication;
import org.apache.commons.io.Charsets;
Expand Down Expand Up @@ -143,6 +145,13 @@ public final class OrganizationFolder extends ComputedFolder<MultiBranchProject<
*/
private DescribableList<BranchBuildStrategy, BranchBuildStrategyDescriptor> buildStrategies = new DescribableList<>(this);

/**
* The branches properties.
*
* @since 2.5.6
*/
private BranchPropertyStrategy strategy;

/**
* The persisted state maintained outside of the config file.
*
Expand All @@ -164,6 +173,13 @@ public final class OrganizationFolder extends ComputedFolder<MultiBranchProject<
*/
private transient String facDigest;

/**
* The {@link #propertyStrategy} digest used to detect if we need to trigger a rescan on save.
*
* @since 2.5.6
*/
private transient String propsDigest;

/**
* The {@link #buildStrategies} digest used to detect if we need to trigger a rescan on save.
*
Expand Down Expand Up @@ -267,6 +283,11 @@ public void onLoad(ItemGroup<? extends Item> parent, String name) throws IOExcep
} catch (XStreamException e) {
facDigest = null;
}
try {
propsDigest = Util.getDigestOf(Items.XSTREAM2.toXML(strategy));
} catch (XStreamException e) {
propsDigest = null;
}
try {
bbsDigest = Util.getDigestOf(Items.XSTREAM2.toXML(buildStrategies));
} catch (XStreamException e) {
Expand Down Expand Up @@ -335,6 +356,26 @@ public DescribableList<MultiBranchProjectFactory,MultiBranchProjectFactoryDescri
return projectFactories;
}

/**
* Gets the strategy.
*
* @return the strategy.
* @since 2.5.6
*/
public BranchPropertyStrategy getStrategy() {
return strategy != null ? strategy : new DefaultBranchPropertyStrategy(new BranchProperty[0]);
}

/**
* Sets the branch property strategy.
*
* @param strategy chosen.
* @since 2.5.6
*/
public void setStrategy(BranchPropertyStrategy strategy) {
this.strategy = strategy;
}

/**
* The {@link BranchBuildStrategy}s to apply.
*
Expand All @@ -351,9 +392,13 @@ public DescribableList<BranchBuildStrategy, BranchBuildStrategyDescriptor> getBu
@Override
protected void submit(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException, Descriptor.FormException {
super.submit(req, rsp);
navigators.rebuildHetero(req, req.getSubmittedForm(), ExtensionList.lookup(SCMNavigatorDescriptor.class), "navigators");
projectFactories.rebuildHetero(req, req.getSubmittedForm(), ExtensionList.lookup(MultiBranchProjectFactoryDescriptor.class), "projectFactories");
buildStrategies.rebuildHetero(req, req.getSubmittedForm(), ExtensionList.lookup(BranchBuildStrategyDescriptor.class), "buildStrategies");

JSONObject json = req.getSubmittedForm();
navigators.rebuildHetero(req, json, ExtensionList.lookup(SCMNavigatorDescriptor.class), "navigators");
projectFactories.rebuildHetero(req, json, ExtensionList.lookup(MultiBranchProjectFactoryDescriptor.class), "projectFactories");
buildStrategies.rebuildHetero(req, json, ExtensionList.lookup(BranchBuildStrategyDescriptor.class), "buildStrategies");
strategy = req.bindJSON(BranchPropertyStrategy.class, json.getJSONObject("strategy"));

for (SCMNavigator n : navigators) {
n.afterSave(this);
}
Expand All @@ -369,6 +414,12 @@ protected void submit(StaplerRequest req, StaplerResponse rsp) throws IOExceptio
} catch (XStreamException e) {
facDigest = null;
}
String propsDigest;
try {
propsDigest = Util.getDigestOf(Items.XSTREAM2.toXML(strategy));
} catch (XStreamException e) {
propsDigest = null;
}
String bbsDigest;
try {
bbsDigest = Util.getDigestOf(Items.XSTREAM2.toXML(buildStrategies));
Expand All @@ -377,9 +428,11 @@ protected void submit(StaplerRequest req, StaplerResponse rsp) throws IOExceptio
}
recalculateAfterSubmitted(!StringUtils.equals(navDigest, this.navDigest));
recalculateAfterSubmitted(!StringUtils.equals(facDigest, this.facDigest));
recalculateAfterSubmitted(!StringUtils.equals(propsDigest, this.propsDigest));
recalculateAfterSubmitted(!StringUtils.equals(bbsDigest, this.bbsDigest));
this.navDigest = navDigest;
this.facDigest = facDigest;
this.propsDigest = propsDigest;
this.bbsDigest = bbsDigest;
}

Expand Down Expand Up @@ -731,6 +784,16 @@ public String getCategoryId() {
return "nested-projects";
}

/**
* Gets all the {@link BranchPropertyStrategyDescriptor} instances applicable to the specified project and source.
*
* @return all the {@link BranchPropertyStrategyDescriptor} instances applicable to the specified project and
* source.
*/
public List<BranchPropertyStrategyDescriptor> propertyStrategyDescriptors() {
return BranchPropertyStrategyDescriptor.all();
}

/**
* A description of this {@link OrganizationFolder}.
*
Expand Down Expand Up @@ -1389,10 +1452,9 @@ private List<BranchSource> createBranchSources() {
for (SCMSource source : sources) {
BranchSource branchSource = new BranchSource(source);
branchSource.setBuildStrategies(buildStrategies);
// TODO do we want/need a more general BranchPropertyStrategyFactory?
branchSource.setStrategy(strategy);
branchSources.add(branchSource);
}
sources = null; // make sure complete gets called just once
return branchSources;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
~ THE SOFTWARE.
-->
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form" xmlns:st="jelly:stapler">
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form" xmlns:st="jelly:stapler" xmlns:branch="/lib/branch-api">
<f:section title="${%Projects}">
<j:choose>
<j:when test="${instance.singleOrigin}">
Expand All @@ -48,6 +48,12 @@
<f:entry field="projectFactories" title="${%Project Recognizers}">
<f:repeatableHeteroProperty field="projectFactories" hasHeader="true" />
</f:entry>
<j:set var="descriptors" value="${descriptor.propertyStrategyDescriptors()}"/>
<j:if test="${!descriptors.isEmpty()}">
<f:block>
<branch:dropdownDescriptorSelector field="strategy" title="Property strategy" descriptors="${descriptors}"/>
</f:block>
</j:if>
<j:set var="descriptors" value="${descriptor.getPropertyType(instance,'buildStrategies').applicableItemDescriptors}"/>
<j:if test="${!descriptors.isEmpty()}">
<f:entry title="${%Build strategies}" help="${descriptor.getHelpFile('buildStrategies')}">
Expand Down
66 changes: 66 additions & 0 deletions src/test/java/jenkins/branch/OrganizationFolderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,19 @@

import com.cloudbees.hudson.plugins.folder.computed.ChildObserver;
import com.cloudbees.hudson.plugins.folder.computed.ComputedFolder;

import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.ExtensionList;
import hudson.model.Item;
import hudson.model.ItemGroup;
import hudson.model.ParameterDefinition;
import hudson.model.Result;
import hudson.model.StringParameterDefinition;
import hudson.model.TaskListener;
import hudson.model.View;
import hudson.scm.NullSCM;
import hudson.security.Permission;
import integration.harness.BasicMultiBranchProject;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -62,13 +67,33 @@
import org.jvnet.hudson.test.TestExtension;
import org.kohsuke.stapler.DataBoundConstructor;

import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.*;

public class OrganizationFolderTest {

public static class OrganizationFolderBranchProperty extends ParameterDefinitionBranchProperty {

@DataBoundConstructor
public OrganizationFolderBranchProperty() {
super();
}

@TestExtension
public static class DescriptorImpl extends BranchPropertyDescriptor {
@Override
protected boolean isApplicable(@NonNull MultiBranchProjectDescriptor projectDescriptor) {
return projectDescriptor instanceof BasicMultiBranchProject.DescriptorImpl;
}
}
}

@Rule
public JenkinsRule r = new JenkinsRule();

Expand Down Expand Up @@ -97,6 +122,47 @@ public void configRoundTrip() throws Exception {
}
}

@Issue("JENKINS-48837")
@Test
public void verifyBranchPropertiesAppliedOnNewProjects() throws Exception {
try (MockSCMController c = MockSCMController.create()) {
c.createRepository("stuff");
OrganizationFolder top = r.jenkins.createProject(OrganizationFolder.class, "top");
List<MultiBranchProjectFactory> projectFactories = top.getProjectFactories();
assertEquals(1, projectFactories.size());
assertEquals(MockFactory.class, projectFactories.get(0).getClass());
top.getNavigators().add(new SingleSCMNavigator("stuff",
Collections.<SCMSource>singletonList(new SingleSCMSource("stuffy",
new MockSCM(c, "stuff", new MockSCMHead("master"), null))))
);
OrganizationFolderBranchProperty instance = new OrganizationFolderBranchProperty();
instance.setParameterDefinitions(Collections.<ParameterDefinition>singletonList(
new StringParameterDefinition("PARAM_STR", "PARAM_DEFAULT_0812673", "The param")
));
top.setStrategy(new DefaultBranchPropertyStrategy(new BranchProperty[] { instance }));
top = r.configRoundtrip(top);

top.scheduleBuild(0);
r.waitUntilNoActivity();

// get the child project produced by the factory after scan
MultiBranchImpl prj = (MultiBranchImpl) top.getItem("stuff");
// verify new multibranch project have branch properties inherited from folder
assertThat(prj.getSources().get(0).getStrategy(), instanceOf(DefaultBranchPropertyStrategy.class));
DefaultBranchPropertyStrategy strategy = (DefaultBranchPropertyStrategy) prj.getSources().get(0).getStrategy();
assertThat(strategy.getProps().get(0), instanceOf(OrganizationFolderBranchProperty.class));
OrganizationFolderBranchProperty property = (OrganizationFolderBranchProperty) strategy.getProps().get(0);
assertThat(property.getParameterDefinitions(), contains(
allOf(
instanceOf(StringParameterDefinition.class),
hasProperty("name", is("PARAM_STR")),
hasProperty("defaultValue", is("PARAM_DEFAULT_0812673")),
hasProperty("description", is("The param"))
)
));
}
}

@Test
@Issue("JENKINS-31516")
public void indexChildrenOnOrganizationFolderIndex() throws Exception {
Expand Down