Permalink
Browse files

[JENKINS-35459] - Prevent Duplicated elements with incorrect URL when…

… using the search on DashBoard View (#2994)

[JENKINS-35459] - Prevent Duplicated elements with incorrect URL when using the search on DashBoard View
  • Loading branch information...
alexanderrtaylor authored and oleg-nenashev committed Oct 16, 2017
1 parent 85d96df commit ead38e0d23c0a7d0d9416deffe08b8738df89bb1
Showing with 69 additions and 3 deletions.
  1. +30 −3 core/src/main/java/hudson/model/ListView.java
  2. +39 −0 test/src/test/java/hudson/search/SearchTest.java
@@ -29,6 +29,8 @@
import hudson.diagnosis.OldDataMonitor;
import hudson.model.Descriptor.FormException;
import hudson.model.listeners.ItemListener;
import hudson.search.CollectionSearchIndex;
import hudson.search.SearchIndexBuilder;
import hudson.security.ACL;
import hudson.security.ACLContext;
import hudson.util.CaseInsensitiveComparator;
@@ -168,16 +170,24 @@ public boolean hasJobFilterExtensions() {
public DescribableList<ListViewColumn, Descriptor<ListViewColumn>> getColumns() {
return columns;
}



public List<TopLevelItem> getItems() {
return getItems(this.recurse);
}


/**
* Returns a read-only view of all {@link Job}s in this view.
*
*
* <p>
* This method returns a separate copy each time to avoid
* concurrent modification issue.
* @param recurse {@code false} not to recurse in ItemGroups
* true to recurse in ItemGroups
*/
@Override
public List<TopLevelItem> getItems() {
private List<TopLevelItem> getItems(boolean recurse) {
SortedSet<String> names;
List<TopLevelItem> items = new ArrayList<TopLevelItem>();

@@ -217,6 +227,23 @@ public boolean hasJobFilterExtensions() {
return items;
}

@Override
public SearchIndexBuilder makeSearchIndex() {
SearchIndexBuilder sib = new SearchIndexBuilder().addAllAnnotations(this);
sib.add(new CollectionSearchIndex<TopLevelItem>() {// for jobs in the view
protected TopLevelItem get(String key) { return getItem(key); }
protected Collection<TopLevelItem> all() { return getItems(); }
@Override
protected String getName(TopLevelItem o) {
// return the name instead of the display for suggestion searching
return o.getName();
}
});
// add the display name for each item in the search index
addDisplayNamesToSearchIndex(sib, getItems(true));
return sib;
}

private List<TopLevelItem> expand(Collection<TopLevelItem> items, List<TopLevelItem> allItems) {
for (TopLevelItem item : items) {
if (item instanceof ItemGroup) {
@@ -454,4 +454,43 @@ public void adminOnlyLinksNotShownToRegularUser() {
index.suggest(term, result);
return result;
}

@Issue("JENKINS-35459")
@Test
public void testProjectNameInAListView() throws Exception {
MockFolder myMockFolder = j.createFolder("folder");
FreeStyleProject freeStyleProject = myMockFolder.createProject(FreeStyleProject.class, "myJob");

ListView listView = new ListView("ListView", j.jenkins);
listView.setRecurse(true);
listView.add(myMockFolder);
listView.add(freeStyleProject);

j.jenkins.addView(listView);
j.jenkins.setPrimaryView(listView);

assertEquals(2, j.jenkins.getPrimaryView().getAllItems().size());

WebClient wc = j.createWebClient();
Page result = wc.goTo("search/suggest?query=" + freeStyleProject.getName(), "application/json");

assertNotNull(result);
j.assertGoodStatus(result);

String content = result.getWebResponse().getContentAsString();
JSONObject jsonContent = (JSONObject)JSONSerializer.toJSON(content);
assertNotNull(jsonContent);
JSONArray jsonArray = jsonContent.getJSONArray("suggestions");
assertNotNull(jsonArray);

assertEquals(2, jsonArray.size());

Page searchResult = wc.goTo("search?q=" + myMockFolder.getName() + "%2F" + freeStyleProject.getName());

assertNotNull(searchResult);
j.assertGoodStatus(searchResult);

URL resultUrl = searchResult.getUrl();
assertTrue(resultUrl.toString().equals(j.getInstance().getRootUrl() + freeStyleProject.getUrl()));
}
}

0 comments on commit ead38e0

Please sign in to comment.