Commit ea7ee60a authored by Changzheng Liu's avatar Changzheng Liu Committed by Enrique Alcántara

Set the scope in search context from group issue and MR pages

parent 1d49a777
......@@ -25,7 +25,7 @@
= hidden_field_tag :group_id, search_context.for_group? ? search_context.group.id : '', class: 'js-search-group-options', data: search_context.group_metadata
= hidden_field_tag :project_id, search_context.for_project? ? search_context.project.id : '', id: 'search_project_id', class: 'js-search-project-options', data: search_context.project_metadata
- if search_context.for_project?
- if search_context.for_project? || search_context.for_group?
= hidden_field_tag :scope, search_context.scope
= hidden_field_tag :search_code, search_context.code_search?
......
---
title: Set the scope in search context from group issue and MR pages
merge_request: 56383
author:
type: other
......@@ -129,7 +129,10 @@ module Gitlab
'wiki_blobs'
elsif view_context.current_controller?(:commits)
'commits'
else nil
elsif view_context.current_controller?(:groups)
if %w(issues merge_requests).include?(view_context.controller.action_name)
view_context.controller.action_name
end
end
end
end
......
......@@ -127,6 +127,35 @@ RSpec.describe Gitlab::SearchContext::Builder, type: :controller do
it { is_expected.to be_for_group }
it { is_expected.to be_search_context(group: group) }
context 'with group scope' do
let(:action_name) { '' }
before do
allow(controller).to receive(:controller_name).and_return('groups')
allow(controller).to receive(:action_name).and_return(action_name)
end
it 'returns nil without groups controller action' do
expect(subject.scope).to be(nil)
end
context 'when on issues scope' do
let(:action_name) { 'issues' }
it 'search context returns issues scope' do
expect(subject.scope).to be('issues')
end
end
context 'when on merge requests scope' do
let(:action_name) { 'merge_requests' }
it 'search context returns issues scope' do
expect(subject.scope).to be('merge_requests')
end
end
end
end
end
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'layouts/_search' do
let(:group) { nil }
let(:project) { nil }
let(:scope) { 'issues' }
let(:search_context) do
instance_double(Gitlab::SearchContext,
project: project,
group: group,
scope: scope,
ref: nil,
snippets: [],
search_url: '/search',
project_metadata: {},
group_metadata: {})
end
before do
allow(view).to receive(:search_context).and_return(search_context)
allow(search_context).to receive(:code_search?).and_return(false)
allow(search_context).to receive(:for_snippets?).and_return(false)
end
shared_examples 'search context scope is set' do
context 'when on issues' do
it 'sets scope to issues' do
render
expect(rendered).to have_css("input[name='scope'][value='issues']", count: 1, visible: false)
end
end
context 'when on merge requests' do
let(:scope) { 'merge_requests' }
it 'sets scope to merge_requests' do
render
expect(rendered).to have_css("input[name='scope'][value='merge_requests']", count: 1, visible: false)
end
end
end
context 'when doing project level search' do
let(:project) { create(:project) }
before do
allow(search_context).to receive(:for_project?).and_return(true)
allow(search_context).to receive(:for_group?).and_return(false)
end
it_behaves_like 'search context scope is set'
end
context 'when doing group level search' do
let(:group) { create(:group) }
before do
allow(search_context).to receive(:for_project?).and_return(false)
allow(search_context).to receive(:for_group?).and_return(true)
end
it_behaves_like 'search context scope is set'
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment