Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[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 ead38e0
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
33 changes: 30 additions & 3 deletions core/src/main/java/hudson/model/ListView.java
Expand Up @@ -29,6 +29,8 @@
import hudson.diagnosis.OldDataMonitor; import hudson.diagnosis.OldDataMonitor;
import hudson.model.Descriptor.FormException; import hudson.model.Descriptor.FormException;
import hudson.model.listeners.ItemListener; import hudson.model.listeners.ItemListener;
import hudson.search.CollectionSearchIndex;
import hudson.search.SearchIndexBuilder;
import hudson.security.ACL; import hudson.security.ACL;
import hudson.security.ACLContext; import hudson.security.ACLContext;
import hudson.util.CaseInsensitiveComparator; import hudson.util.CaseInsensitiveComparator;
Expand Down Expand Up @@ -168,16 +170,24 @@ public DescribableList<ViewJobFilter, Descriptor<ViewJobFilter>> getJobFilters()
public DescribableList<ListViewColumn, Descriptor<ListViewColumn>> getColumns() { public DescribableList<ListViewColumn, Descriptor<ListViewColumn>> getColumns() {
return columns; return columns;
} }



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


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


Expand Down Expand Up @@ -217,6 +227,23 @@ public List<TopLevelItem> getItems() {
return items; 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) { private List<TopLevelItem> expand(Collection<TopLevelItem> items, List<TopLevelItem> allItems) {
for (TopLevelItem item : items) { for (TopLevelItem item : items) {
if (item instanceof ItemGroup) { if (item instanceof ItemGroup) {
Expand Down
39 changes: 39 additions & 0 deletions test/src/test/java/hudson/search/SearchTest.java
Expand Up @@ -454,4 +454,43 @@ private List<SearchItem> suggest(SearchIndex index, String term) {
index.suggest(term, result); index.suggest(term, result);
return 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.