Skip to content

Commit

Permalink
Merge pull request #20 from amuniz/schedule-build-on-indexing
Browse files Browse the repository at this point in the history
[JENKINS-31516] Run re-index on children on organization re-index
  • Loading branch information
jglick committed Dec 9, 2015
2 parents 214823f + 3be438b commit e1cf1f4
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/main/java/jenkins/branch/OrganizationFolder.java
Expand Up @@ -162,6 +162,7 @@ public void complete() throws IllegalStateException, InterruptedException {
PersistedList<BranchSource> sourcesList = existing.getSourcesList();
sourcesList.clear();
sourcesList.addAll(createBranchSources());
existing.scheduleBuild();
return;
}
if (!observer.mayCreate(projectName)) {
Expand Down
58 changes: 56 additions & 2 deletions src/test/java/jenkins/branch/OrganizationFolderTest.java
Expand Up @@ -26,18 +26,29 @@

import hudson.model.ItemGroup;
import hudson.model.TaskListener;
import jenkins.branch.harness.MultiBranchImpl;
import jenkins.scm.api.SCMNavigator;
import jenkins.scm.impl.SingleSCMNavigator;
import hudson.scm.NullSCM;
import hudson.util.RingBufferLogHandler;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import jenkins.scm.api.SCMSource;
import jenkins.scm.impl.SingleSCMSource;
import org.junit.Test;
import static org.junit.Assert.*;

import org.junit.Assert;
import org.junit.Rule;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.TestExtension;
import org.kohsuke.stapler.DataBoundConstructor;
Expand Down Expand Up @@ -65,15 +76,32 @@ public void configRoundTrip() throws Exception {
assertEquals(MockFactory.class, projectFactories.get(0).getClass());
assertEquals(MockFactory.class, projectFactories.get(1).getClass());
}
@TestExtension("configRoundTrip")

@Test
@Issue("JENKINS-31516")
public void indexChildrenOnOrganizationFolderIndex() throws Exception {
OrganizationFolder top = r.jenkins.createProject(OrganizationFolder.class, "top");
List<MultiBranchProjectFactory> projectFactories = top.getProjectFactories();
projectFactories.add(new MockFactory());
top.getNavigators().add(new SingleSCMNavigator("stuff", Collections.<SCMSource>singletonList(new SingleSCMSource("id", "stuffy", new NullSCM()))));
top = r.configRoundtrip(top);

RingBufferLogHandler logs = createJULTestHandler(); // switch to the mock log handler

top.setDescription("Org folder test");
top = r.configRoundtrip(top);
waitForLogFileMessage("Indexing multibranch project: stuff", logs);
}

@TestExtension
public static class ConfigRoundTripDescriptor extends MockFactoryDescriptor {}

public static class MockFactory extends MultiBranchProjectFactory {
@DataBoundConstructor
public MockFactory() {}
@Override
public MultiBranchProject<?, ?> createProject(ItemGroup<?> parent, String name, List<? extends SCMSource> scmSources, Map<String,Object> attributes, TaskListener listener) throws IOException, InterruptedException {
return null;
return new MultiBranchImpl(parent, name);
}
}
static abstract class MockFactoryDescriptor extends MultiBranchProjectFactoryDescriptor {
Expand All @@ -90,4 +118,30 @@ public String getDisplayName() {
}
}

private RingBufferLogHandler createJULTestHandler() throws SecurityException, IOException {
RingBufferLogHandler handler = new RingBufferLogHandler();
SimpleFormatter formatter = new SimpleFormatter();
handler.setFormatter(formatter);
Logger logger = Logger.getLogger(MultiBranchImpl.class.getName());
logger.addHandler(handler);
return handler;
}

private void waitForLogFileMessage(String string, RingBufferLogHandler logs) throws IOException, InterruptedException {
File rootDir = r.jenkins.getRootDir();
synchronized (rootDir) {
int limit = 0;
while (limit < 5) {
rootDir.wait(1000);
for (LogRecord r : logs.getView()) {
if (r.getMessage().contains(string)) {
return;
}
}
limit++;
}
}
Assert.assertTrue("Expected log not found: " + string, false);
}

}
17 changes: 11 additions & 6 deletions src/test/java/jenkins/branch/harness/MultiBranchImpl.java
Expand Up @@ -29,6 +29,7 @@

import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;

import hudson.Extension;
import hudson.model.FreeStyleBuild;
Expand All @@ -45,8 +46,9 @@

public class MultiBranchImpl extends MultiBranchProject<FreeStyleProject, FreeStyleBuild> {

private static final Logger LOGGER = Logger.getLogger(MultiBranchImpl.class.getName());

protected MultiBranchImpl(ItemGroup parent, String name) {
public MultiBranchImpl(ItemGroup parent, String name) {
super(parent, name);
}

Expand All @@ -55,16 +57,19 @@ protected BranchProjectFactory<FreeStyleProject, FreeStyleBuild> newProjectFacto
return new BranchProjectFactoryImpl();
}

@Override
public boolean scheduleBuild() {
LOGGER.info("Indexing multibranch project: " + getDisplayName());
return super.scheduleBuild();
}

public static class BranchProjectFactoryImpl extends BranchProjectFactory<FreeStyleProject, FreeStyleBuild> {

@Override
public FreeStyleProject newInstance(Branch branch) {
FreeStyleProject job = new FreeStyleProject(getOwner(), branch.getName());
FreeStyleProject spied = spy(job);
// Do nothing.. Running the actual build is not desired/required (and not possible) in this tests.
when(spied.scheduleBuild()).thenReturn(false);
setBranch(spied, branch);
return spied;
setBranch(job, branch);
return job;
}

@Override
Expand Down

0 comments on commit e1cf1f4

Please sign in to comment.