Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

[26684] Redirect to WP from search only with #ID match on enter

Oliver Günthercf4975cad3e2
diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb
index b6effe1cd3c..5116f60d85c 100644
--- a/app/controllers/search_controller.rb
+++ b/app/controllers/search_controller.rb
@@ -31,8 +31,7 @@ class SearchController < ApplicationController
include Layout
before_action :find_optional_project,
- :prepare_tokens,
- :quick_wp_id_redirect
+ :prepare_tokens
LIMIT = 10
@@ -64,12 +63,6 @@ class SearchController < ApplicationController
end
end
- def quick_wp_id_redirect
- scan_work_package_reference @question do |id|
- redirect_to work_package_path(id: id) if WorkPackage.visible.find_by(id: id)
- end
- end
-
def find_optional_project
return true unless params[:project_id]
@@ -108,10 +101,6 @@ class SearchController < ApplicationController
tokens
end
- def scan_work_package_reference(query, &blk)
- query.match(/\A#?(\d+)\z/) && ((blk&.call($1.to_i)) || true)
- end
-
def search_params
@search_params ||= permitted_params.search
end
diff --git a/frontend/src/app/modules/global_search/input/global-search-input.component.ts b/frontend/src/app/modules/global_search/input/global-search-input.component.ts
index 36308a1257f..3afa36808e7 100644
--- a/frontend/src/app/modules/global_search/input/global-search-input.component.ts
+++ b/frontend/src/app/modules/global_search/input/global-search-input.component.ts
@@ -39,7 +39,6 @@ import {
} from '@angular/core';
import {ContainHelpers} from 'core-app/modules/common/focus/contain-helpers';
import {I18nService} from 'core-app/modules/common/i18n/i18n.service';
-import {DynamicBootstrapper} from "core-app/globals/dynamic-bootstrapper";
import {PathHelperService} from "core-app/modules/common/path-helper/path-helper.service";
import {HalResourceService} from "core-app/modules/hal/services/hal-resource.service";
import {WorkPackageResource} from "core-app/modules/hal/resources/work-package-resource";
@@ -89,7 +88,10 @@ export class GlobalSearchInputComponent implements OnInit, OnDestroy {
/** Remember the current value */
public currentValue:string = '';
- private unregisterGlobalListener:Function | undefined;
+ /** Remember if we have found an exact ID match */
+ public exactMatchedWpId:string|null = null
+
+ private unregisterGlobalListener:Function|undefined;
public text:{ [key:string]:string } = {
all_projects: this.I18n.t('js.global_search.all_projects'),
@@ -209,6 +211,11 @@ export class GlobalSearchInputComponent implements OnInit, OnDestroy {
public onEnterBeforeResultsLoaded() {
if (!this.requests.hasResults) {
this.searchInScope(this.currentScope);
+ return;
+ }
+
+ if (this.exactMatchedWpId) {
+ window.location.href = this.wpPath(this.exactMatchedWpId);
}
}
@@ -226,14 +233,15 @@ export class GlobalSearchInputComponent implements OnInit, OnDestroy {
return of([]);
}
- let idOnly:boolean = false;
-
- if (query.match(/^#\d+$/)) {
- query = query.replace(/^#/, '');
- idOnly = true;
+ // Remove ID marker # when searching for #<number>
+ if (query.match(/^#(\d+)/)) {
+ query = query.substr(1);
}
- let href:string = this.PathHelperService.api.v3.wpBySubjectOrId(query, idOnly);
+ // Test for exact ID matches
+ this.exactMatchedWpId = null;
+
+ let href:string = this.PathHelperService.api.v3.wpBySubjectOrId(query);
this.addSuggestions();
@@ -242,6 +250,10 @@ export class GlobalSearchInputComponent implements OnInit, OnDestroy {
.pipe(
map((collection) => {
return this.suggestions.concat(collection.elements.map((wp) => {
+ if (query === wp.id!.toString()) {
+ this.exactMatchedWpId = query;
+ }
+
return {
id: wp.id!,
subject: wp.subject,
@@ -304,8 +316,8 @@ export class GlobalSearchInputComponent implements OnInit, OnDestroy {
this.ngSelectComponent.close();
// Work package results can update without page reload.
if (!forcePageLoad &&
- this.globalSearchService.isAfterSearch() &&
- this.globalSearchService.currentTab === 'work_packages') {
+ this.globalSearchService.isAfterSearch() &&
+ this.globalSearchService.currentTab === 'work_packages') {
window.history
.replaceState({},
`${I18n.t('global_search.search')}: ${this.ngSelectComponent.searchTerm}`,
@@ -319,7 +331,7 @@ export class GlobalSearchInputComponent implements OnInit, OnDestroy {
public blur() {
this.ngSelectComponent.searchTerm = '';
- (<HTMLInputElement> document.activeElement).blur();
+ (<HTMLInputElement>document.activeElement).blur();
}
private get currentScope():string {
diff --git a/spec/controllers/search_controller_spec.rb b/spec/controllers/search_controller_spec.rb
index 8446803ebc6..2ed643b0a8e 100644
--- a/spec/controllers/search_controller_spec.rb
+++ b/spec/controllers/search_controller_spec.rb
@@ -29,58 +29,58 @@
require 'spec_helper'
describe SearchController, type: :controller do
- let!(:project) do
+ shared_let(:project) do
FactoryBot.create(:project,
name: 'eCookbook')
end
- let!(:other_project) do
+ shared_let(:other_project) do
FactoryBot.create(:project,
name: 'Other project')
end
- let!(:subproject) do
+ shared_let(:subproject) do
FactoryBot.create(:project,
name: 'Child project',
parent: project)
end
- let(:role) do
+ shared_let(:role) do
FactoryBot.create(:role, permissions: %i[view_wiki_pages view_work_packages])
end
- let(:user) do
+ shared_let(:user) do
FactoryBot.create(:user,
member_in_projects: [project, subproject],
member_through_role: role)
end
- let!(:wiki_page) do
+ shared_let(:wiki_page) do
FactoryBot.create(:wiki_page,
title: "How to solve an issue",
wiki: project.wiki)
end
- let!(:work_package_1) do
+ shared_let(:work_package_1) do
FactoryBot.create(:work_package,
subject: 'This is a test issue',
project: project)
end
- let!(:work_package_2) do
+ shared_let(:work_package_2) do
FactoryBot.create(:work_package,
subject: 'Issue test 2',
project: project,
status: FactoryBot.create(:closed_status))
end
- let!(:work_package_3) do
+ shared_let(:work_package_3) do
FactoryBot.create(:work_package,
subject: 'Issue test 3',
project: subproject)
end
- let!(:work_package_4) do
+ shared_let(:work_package_4) do
FactoryBot.create(:work_package,
subject: 'Issue test 4',
project: other_project)
@@ -108,14 +108,6 @@ describe SearchController, type: :controller do
it_behaves_like 'successful search'
end
-
- context 'is a work package reference' do
- let!(:work_package) { FactoryBot.create :work_package, project: project }
-
- subject { get :index, params: { q: "##{work_package.id}" } }
-
- it { is_expected.to redirect_to work_package }
- end
end
end
@@ -271,45 +263,5 @@ describe SearchController, type: :controller do
end
end
end
-
- describe '#scan_work_package_reference' do
- subject { @controller.send(:scan_work_package_reference, query) }
-
- context 'with normal query' do
- let(:query) { 'lorem' }
-
- it { is_expected.to be nil }
- end
-
- context 'with work package reference' do
- let(:query) { '#4123' }
-
- it { is_expected.not_to be nil }
-
- describe 'captures' do
- let!(:check_block) { Proc.new { |id| @work_package_id = id } }
-
- it 'block gets called' do
- # NOTE: this is how it is favored to do in RSpec3
- # expect(check_block).to receive :call
- # but we have only RSpec2 here, so:
- expect(check_block).to receive :call
- @controller.send(:scan_work_package_reference, query, &check_block)
- end
-
- it 'id inside of block is work package id' do
- @controller.send(:scan_work_package_reference, query, &check_block)
-
- expect(@work_package_id.to_i).to eq 4123
- end
- end
-
- context 'and with additional text' do
- let(:query) { '#4123 and some text' }
-
- it { is_expected.to be nil }
- end
- end
- end
end
end