diff --git a/core/src/main/java/hudson/search/Search.java b/core/src/main/java/hudson/search/Search.java index 92939e727239..3ff1aae7161e 100644 --- a/core/src/main/java/hudson/search/Search.java +++ b/core/src/main/java/hudson/search/Search.java @@ -129,11 +129,18 @@ private void doIndexImpl(StaplerRequest2 req, StaplerResponse2 rsp) throws IOExc rsp.sendRedirect2(req.getContextPath() + target.getUrl()); return; } + + List suggestions = suggest(index, query, smo); + + if (suggestions.size() == 1) { + rsp.sendRedirect2(req.getContextPath() + suggestions.get(0).getUrl()); + return; + } } } } - // no exact match. show the suggestions + // No exact match and no single suggestion. rsp.setStatus(SC_NOT_FOUND); req.getView(this, "search-failed.jelly").forward(req, rsp); } diff --git a/test/src/test/java/hudson/search/SearchTest.java b/test/src/test/java/hudson/search/SearchTest.java index 5ded36f56f29..89bf2254465e 100644 --- a/test/src/test/java/hudson/search/SearchTest.java +++ b/test/src/test/java/hudson/search/SearchTest.java @@ -125,6 +125,21 @@ void testSearchByProjectName() throws Exception { assertTrue(contents.contains(String.format("%s - Jenkins", projectName))); } + @Test + void shouldRedirectToSingleSuggestedItem() throws Exception { + FreeStyleProject project = j.createFreeStyleProject("job-config-scroll-test"); + + Page result = j.search("job-config-scroll"); + + assertNotNull(result); + j.assertGoodStatus(result); + + assertEquals( + j.getURL().toString() + project.getUrl(), + result.getUrl().toString() + ); + } + @Issue("JENKINS-24433") @Test void testSearchByProjectNameBehindAFolder() throws Exception {