Skip to content

Commit

Permalink
Taking advantage of APIs available with newer Jenkins cores.
Browse files Browse the repository at this point in the history
· [JENKINS-18224] TransientActionFactory simplifies adding the Move action and makes it available more broadly.
  Can therefore deprecate TransientFolderActionFactory.
· Expanding the search index to look for jobs in subfolders (matches #848 in Jenkins root).
· testReloadJenkinsAndFindBuildInProgress can now pass unconditionally.
· ListView.setIncludeRegex avoids the need for reflection.
· f:bottomButtonBar convenient in Jelly.
As yet unimplemented:
· AutoCompletionCandidates.ofJobNames could be used, though it does not filter by permitted children.
  (Nor, generally, should it, since this method is typically used for *existing* items, which might violate this restriction.)
  Might be feasible to call ofJobNames and then filter the result; or perhaps this method should accept an Item filter.
  • Loading branch information
jglick committed Jan 9, 2014
1 parent efac1d2 commit c006194
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 62 deletions.
26 changes: 10 additions & 16 deletions src/main/java/com/cloudbees/hudson/plugins/folder/Folder.java
Expand Up @@ -82,7 +82,6 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.text.ParseException;
import java.util.ArrayList;
Expand All @@ -97,7 +96,6 @@
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;

import static hudson.Util.fixEmpty;
import hudson.init.InitMilestone;
Expand Down Expand Up @@ -282,16 +280,7 @@ private void init() {
try {
lv.getColumns().replaceBy(columns.toList());
lv.getJobFilters().replaceBy(filters.toList());

try { // TODO use setIncludeRegex as of 1.526
Field f = lv.getClass().getDeclaredField("includeRegex");
f.setAccessible(true);
f.set(lv, ".*");
f = lv.getClass().getDeclaredField("includePattern");
f.setAccessible(true);
f.set(lv, Pattern.compile(".*"));
} catch (Throwable e) {
}
lv.setIncludeRegex(".*");
lv.save();
} catch (IOException e) {
LOGGER.log(Level.WARNING, "Failed to set up the initial view", e);
Expand Down Expand Up @@ -393,6 +382,7 @@ public String getPronoun() {
*
* @see TransientProjectActionFactory
*/
@SuppressWarnings("deprecation")
@Override
public synchronized List<Action> getActions() {
// add all the transient actions, too
Expand Down Expand Up @@ -806,13 +796,17 @@ public View getStaplerFallback() {
@Override protected SearchIndexBuilder makeSearchIndex() {
return super.makeSearchIndex().add(new CollectionSearchIndex<TopLevelItem>() {
@Override protected SearchItem get(String key) {
return items.get(key);
return Jenkins.getInstance().getItem(key, grp());
}
@Override protected Collection<TopLevelItem> all() {
return items.values();
return Items.getAllItems(grp(), TopLevelItem.class);
}
@Override protected String getName(TopLevelItem j) {
return j.getName();
return j.getRelativeNameFrom(grp());
}
/** Disambiguates calls that otherwise would match {@link Item} too. */
private ItemGroup<?> grp() {
return Folder.this;
}
});
}
Expand Down Expand Up @@ -937,7 +931,7 @@ public List<FolderHealthMetricDescriptor> getHealthMetricDescriptors() {
*/
public AutoCompletionCandidates doAutoCompleteCopyNewItemFrom(@AncestorInPath final Folder f,
@QueryParameter final String value) {
// TODO use ofJobNames in 1.489+
// TODO use ofJobNames but filter by isAllowedChild
final AutoCompletionCandidates r = new AutoCompletionCandidates();

abstract class VisitorImpl extends ItemVisitor {
Expand Down
Expand Up @@ -27,12 +27,11 @@
import hudson.Extension;
import hudson.ExtensionList;
import hudson.ExtensionPoint;
import hudson.model.AbstractProject;
import hudson.model.Action;
import hudson.model.Hudson;
import hudson.tasks.BuildStep;

import java.util.Collection;
import jenkins.model.Jenkins;
import jenkins.model.TransientActionFactory;

/**
* Extension point for inserting transient {@link Action}s into {@link Folder}s.
Expand All @@ -46,7 +45,9 @@
* To register your implementation, put {@link Extension} on your subtype.
*
* @see Action
* @deprecated Use {@link TransientActionFactory} on {@link Folder} instead.
*/
@Deprecated
public abstract class TransientFolderActionFactory implements ExtensionPoint {
/**
* Creates actions for the given project.
Expand All @@ -62,7 +63,7 @@ public abstract class TransientFolderActionFactory implements ExtensionPoint {
* Returns all the registered {@link TransientFolderActionFactory}s.
*/
public static ExtensionList<TransientFolderActionFactory> all() {
return Hudson.getInstance().getExtensionList(TransientFolderActionFactory.class);
return Jenkins.getInstance().getExtensionList(TransientFolderActionFactory.class);
}
}

Expand Up @@ -24,16 +24,12 @@

package com.cloudbees.hudson.plugins.folder.relocate;

import com.cloudbees.hudson.plugins.folder.Folder;
import com.cloudbees.hudson.plugins.folder.Messages;
import com.cloudbees.hudson.plugins.folder.TransientFolderActionFactory;
import hudson.Extension;
import hudson.model.AbstractProject;
import hudson.model.Action;
import hudson.model.Failure;
import hudson.model.Item;
import hudson.model.ItemGroup;
import hudson.model.TransientProjectActionFactory;
import hudson.security.Permission;
import hudson.security.PermissionScope;
import hudson.util.HttpResponses;
Expand All @@ -45,6 +41,7 @@
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import jenkins.model.Jenkins;
import jenkins.model.TransientActionFactory;
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
Expand Down Expand Up @@ -161,33 +158,24 @@ public HttpResponse doMove(StaplerRequest req, @QueryParameter String destinatio
}
}

// TODO JENKINS-18224 replace with a single TransientActionFactory

/**
* Makes sure that {@link AbstractProject}s have the action.
* Makes sure that {@link Item}s have the action.
*/
@Extension
public static class TransientProjectActionFactoryImpl extends TransientProjectActionFactory {
public static class TransientActionFactoryImpl extends TransientActionFactory<Item> {

static {
RELOCATE.getId(); // ensure loaded eagerly
}

@Override
public Collection<? extends Action> createFor(AbstractProject target) {
return Collections.singleton(new RelocateAction(target));
@Override public Class<Item> type() {
return Item.class;
}
}

/**
* Makes sure that {@link Folder}s have the action.
*/
@Extension
public static class TransientFolderActionFactoryImpl extends TransientFolderActionFactory {

@Override
public Collection<? extends Action> createFor(Folder target) {
public Collection<? extends Action> createFor(Item target) {
return Collections.singleton(new RelocateAction(target));
}
}

}
Expand Up @@ -60,15 +60,10 @@ THE SOFTWARE.
<f:descriptorList descriptors="${descriptor.getPropertyDescriptors()}" instances="${it.properties}" forceRowSet="true" />

<j:if test="${h.hasPermission(it,it.CONFIGURE)}">
<!-- TODO <f:bottomButtonBar> in 1.499+ -->
<f:block>
<div id="bottom-sticker" >
<div class="bottom-sticker-inner">
<f:bottomButtonBar>
<f:submit value="${%Save}" />
<f:apply/>
</div>
</div>
</f:block>
</f:bottomButtonBar>
</j:if>
</f:form>
</l:main-panel>
Expand Down
21 changes: 5 additions & 16 deletions src/test/java/com/cloudbees/hudson/plugins/folder/FolderTest.java
Expand Up @@ -28,26 +28,21 @@
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlRadioButtonInput;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.Job;
import hudson.model.ListView;
import hudson.model.TopLevelItem;
import hudson.search.SearchItem;
import hudson.tasks.BuildTrigger;
import hudson.tasks.Shell;
import hudson.util.VersionNumber;
import hudson.views.BuildButtonColumn;
import hudson.views.JobColumn;
import jenkins.model.Jenkins;
import org.jvnet.hudson.test.Bug;
import org.jvnet.hudson.test.SleepBuilder;
import org.jvnet.hudson.test.TestBuilder;
import org.jvnet.hudson.test.recipes.LocalData;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

public class FolderTest extends AbstractFolderTest {
/**
Expand Down Expand Up @@ -192,7 +187,6 @@ public void testDataCompatibility() throws Exception {
assertTrue(2<new ListView("test").getColumns().size());
}

/* TODO 1.512 adjust Folder.makeSearchIndex to use Items.getAllItems (and check against 1.527/#848):
public void testSearch() throws Exception {
FreeStyleProject topJob = jenkins.createProject(FreeStyleProject.class, "top job");
Folder f1 = jenkins.createProject(Folder.class, "f1");
Expand All @@ -203,13 +197,8 @@ public void testSearch() throws Exception {
f1.getSearchIndex().suggest("job", items);
assertEquals(new HashSet<SearchItem>(Arrays.asList(middleJob, bottomJob)), new HashSet<SearchItem>(items));
}
*/

// TODO: define @ZenDesk(13067)
public void testReloadJenkinsAndFindBuildInProgress() throws Exception {
if (new VersionNumber("1.500").compareTo(Jenkins.getVersion())>0)
return; // this test only works with 1.509 LTS and onward

Folder f1 = jenkins.createProject(Folder.class, "f");
FreeStyleProject p1 = f1.createProject(FreeStyleProject.class, "test1");

Expand Down

0 comments on commit c006194

Please sign in to comment.