-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
326 provide basic search functionality #459
base: master
Are you sure you want to change the base?
Changes from 13 commits
d1f09f1
7f0dddc
e38f98d
6bb9b2e
1771a0c
d29bf59
0b84acc
b5a73eb
7911016
ff264d5
53c97a1
f25fae6
cf77217
bba8e25
97e5dbb
4b4f008
605bc17
4febae6
768e433
ea14b10
92a5b38
547dc78
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'ostruct' | ||
|
||
# Returns a search result to the GraphQL API | ||
class SearchResolver | ||
def call(root, arguments, _context) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. C: Assignment Branch Condition size for call is too high. [17.26/15] |
||
query = root[:query] | ||
categories = arguments[:categories] | ||
if categories.blank? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. C: Method has too many lines. [17/10] |
||
indices = [::Index::RepositoryIndex::Repository, | ||
::Index::OrganizationIndex::Organization, | ||
::Index::UserIndex::User] | ||
else | ||
indices = categories.map do |category| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check out indices = categories.reduce([]) do |indices, category|
...
when 'repositories'
indices + [::Index::RepositoryIndex::Repository]
...
end that way you don't need to flatten the array at the end, which might make it faster. |
||
case category | ||
when 'organizationalUnits' | ||
[::Index::OrganizationIndex::Organization, ::Index::UserIndex::User] | ||
when 'repositories' | ||
[::Index::RepositoryIndex::Repository] | ||
else | ||
[] | ||
end | ||
end | ||
indices = indices.flatten | ||
end | ||
result = indices.map do |index| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could be done using reduce as well, but I think here flatten is fine. |
||
index.query(multi_match: {query: query, | ||
fuzziness: 'auto', | ||
fields: [ | ||
:display_name, | ||
:slug, | ||
:name, | ||
:description]}).entries | ||
end | ||
result = result.flatten | ||
graphQLResult = OpenStruct.new( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. W: Useless assignment to variable - |
||
entries: createEntries(result), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. W: Useless assignment to variable - |
||
count: OpenStruct.new( | ||
all: result.size, | ||
organizational_units: allOrganizationalUnits(result), | ||
repositories: allRepositories(result) | ||
) | ||
) | ||
end | ||
|
||
private | ||
def allOrganizationalUnits(result) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. C: Use snake_case for method names. |
||
search_result = 0 | ||
result.each do |element| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. C: Use snake_case for method names. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check out |
||
elem = element._data["_index"] | ||
if elem == 'user' || elem == 'organization' | ||
search_result+=1 | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. C: Avoid comparing a variable with multiple items in a conditional, use |
||
end | ||
search_result | ||
end | ||
|
||
private | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. W: Useless |
||
def allRepositories(result) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. C: Use snake_case for method names. |
||
search_result = 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. C: Use snake_case for method names. |
||
result.each do |element| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check out |
||
elem = element._data["_index"] | ||
if elem == 'repository' | ||
search_result+=1 | ||
end | ||
end | ||
search_result | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. C: Assignment Branch Condition size for create_entries is too high. [19.1/15] |
||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. E: unexpected token kEND (Using Ruby 2.5 parser; configure using |
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. W: Useless |
||
private | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. W: Useless |
||
def createEntries(result) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. C: Assignment Branch Condition size for createEntries is too high. [19.1/15] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. C: Assignment Branch Condition size for createEntries is too high. [19.1/15] |
||
result.map do |element| | ||
OpenStruct.new( | ||
ranking: element._data["_score"], | ||
entry: if element._data["_type"] == 'user' | ||
User.first(slug: element.attributes["slug"]) | ||
elsif element._data["_type"] == 'repository' | ||
Repository.first(slug: element.attributes["slug"]) | ||
else | ||
Organization.first(slug: element.attributes["slug"]) | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. W: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. W: |
||
) | ||
end | ||
end | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. E: unexpected token kEND (Using Ruby 2.5 parser; configure using |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,11 +15,26 @@ | |
end | ||
|
||
RSpec.describe 'Search query' do | ||
let(:user) { create :user } | ||
before do | ||
create :user | ||
3.times { create :organization } | ||
5.times { create :repository, owner: user } | ||
::Index::UserIndex.purge | ||
::Index::OrganizationIndex.purge | ||
::Index::RepositoryIndex.purge | ||
|
||
ada = create :user, display_name: 'Ada' | ||
::Index::UserIndex.import(ada) | ||
adc = create :user, display_name: 'Adc' | ||
::Index::UserIndex.import(adc) | ||
bob = create :user, display_name: 'Bob' | ||
::Index::UserIndex.import(bob) | ||
|
||
::Index::OrganizationIndex.import(create :organization, display_name: 'Ada') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. C: Line is too long. [81/80] |
||
::Index::OrganizationIndex.import(create :organization, display_name: 'Bda Organization') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. C: Line is too long. [94/80] |
||
::Index::OrganizationIndex.import(create :organization, display_name: 'Abc_Organization') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. C: Line is too long. [94/80] |
||
|
||
::Index::RepositoryIndex.import(create :repository, name: 'Ada/repository', owner: ada) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. C: Line is too long. [92/80] |
||
::Index::RepositoryIndex.import(create :repository, name: 'Bob/repository', owner: bob) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. C: Line is too long. [92/80] |
||
::Index::RepositoryIndex.import(create :repository, name: 'Adc/repository', owner: adc) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. C: Line is too long. [92/80] |
||
::Index::RepositoryIndex.import(create :repository, name: 'Bob/AdaRepository', owner: bob) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. C: Line is too long. [95/80] |
||
end | ||
|
||
let(:context) { {} } | ||
|
@@ -71,88 +86,87 @@ | |
context 'with global scope' do | ||
let(:scope) { 'global' } | ||
context 'no categories' do | ||
let(:variables) { {'query' => ''} } | ||
let(:expected_num_entries) { 10 } | ||
let(:expected_count_all) { 10 } | ||
let(:expected_count_organizational_units) { 5 } | ||
let(:expected_count_repositories) { 5 } | ||
let(:variables) { {'query' => 'Ada'} } | ||
let(:expected_num_entries) { 6 } | ||
let(:expected_count_all) { 6 } | ||
let(:expected_count_organizational_units) { 4 } | ||
let(:expected_count_repositories) { 2 } | ||
|
||
include_examples 'number of entries' | ||
|
||
it 'returns the repositories' do | ||
repositories = search_result['entries'].select do |e| | ||
e['entry']['__typename'] == 'Repository' | ||
end | ||
expect(repositories.length).to eq(5) | ||
expect(repositories.length).to eq(expected_count_repositories) | ||
end | ||
|
||
it 'returns the organizational units' do | ||
organizational_units = search_result['entries'].select do |e| | ||
%w(User Organization).include?(e['entry']['__typename']) | ||
end | ||
expect(organizational_units.length).to eq(5) | ||
expect(organizational_units.length).to eq(expected_count_organizational_units) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. C: Line is too long. [86/80] |
||
end | ||
end | ||
|
||
context 'category: all' do | ||
let(:variables) do | ||
{'query' => '', 'categories' => %w(repositories organizationalUnits)} | ||
{'query' => 'Ada', 'categories' => %w(repositories organizationalUnits)} | ||
end | ||
let(:expected_num_entries) { 10 } | ||
let(:expected_count_all) { 10 } | ||
let(:expected_count_organizational_units) { 5 } | ||
let(:expected_count_repositories) { 5 } | ||
let(:expected_num_entries) { 6 } | ||
let(:expected_count_all) { 6 } | ||
let(:expected_count_organizational_units) { 4 } | ||
let(:expected_count_repositories) { 2 } | ||
|
||
include_examples 'number of entries' | ||
|
||
it 'returns the repositories' do | ||
repositories = search_result['entries'].select do |e| | ||
e['entry']['__typename'] == 'Repository' | ||
end | ||
expect(repositories.length).to eq(5) | ||
expect(repositories.length).to eq(expected_count_repositories) | ||
end | ||
|
||
it 'returns the organizational units' do | ||
organizational_units = search_result['entries'].select do |e| | ||
%w(User Organization).include?(e['entry']['__typename']) | ||
end | ||
expect(organizational_units.length).to eq(5) | ||
expect(organizational_units.length).to eq(expected_count_organizational_units) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. C: Line is too long. [86/80] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. C: Line is too long. [86/80] |
||
end | ||
end | ||
|
||
context 'category: repositories' do | ||
let(:variables) { {'query' => '', 'categories' => %w(repositories)} } | ||
let(:expected_num_entries) { 5 } | ||
let(:expected_count_all) { 10 } | ||
let(:expected_count_organizational_units) { 5 } | ||
let(:expected_count_repositories) { 5 } | ||
let(:variables) { {'query' => 'Ada', 'categories' => %w(repositories)} } | ||
let(:expected_num_entries) { 2 } | ||
let(:expected_count_all) { 2 } | ||
let(:expected_count_organizational_units) { 0 } | ||
let(:expected_count_repositories) { 2 } | ||
|
||
include_examples 'number of entries' | ||
|
||
it 'returns the repositories' do | ||
repositories = search_result['entries'].select do |e| | ||
e['entry']['__typename'] == 'Repository' | ||
end | ||
expect(repositories.length).to eq(5) | ||
expect(repositories.length).to eq(expected_count_repositories) | ||
end | ||
end | ||
|
||
context 'category: organizationalUnits' do | ||
let(:variables) do | ||
{'query' => '', 'categories' => %w(organizationalUnits)} | ||
{'query' => 'Ada', 'categories' => %w(organizationalUnits)} | ||
end | ||
let(:expected_num_entries) { 5 } | ||
let(:expected_count_all) { 10 } | ||
let(:expected_count_organizational_units) { 5 } | ||
let(:expected_count_repositories) { 5 } | ||
let(:expected_num_entries) { 4 } | ||
let(:expected_count_all) { 4 } | ||
let(:expected_count_organizational_units) { 4 } | ||
let(:expected_count_repositories) { 0 } | ||
|
||
include_examples 'number of entries' | ||
|
||
it 'returns the organizational units' do | ||
organizational_units = search_result['entries'].select do |e| | ||
%w(User Organization).include?(e['entry']['__typename']) | ||
end | ||
expect(organizational_units.length).to eq(5) | ||
expect(organizational_units.length).to eq(expected_count_organizational_units) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. C: Line is too long. [86/80] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. C: Line is too long. [86/80] |
||
end | ||
end | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E: class definition in method body (Using Ruby 2.5 parser; configure using
TargetRubyVersion
parameter, underAllCops
)