提交 ead38e0d 编写于 作者: A Alex Taylor 提交者: Oleg Nenashev

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

[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
上级 85d96df6
......@@ -29,6 +29,8 @@ import hudson.Util;
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 class ListView extends View implements DirectlyModifiableView {
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 class ListView extends View implements DirectlyModifiableView {
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 class SearchTest {
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()));
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册