Commit 389a22e7 authored by Mark Chao's avatar Mark Chao

Backport of EE changes in https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/6195

Refactor AutocompleteSources#label:

1. The return value was inconsistent, sometimes returning a hash and sometimes returning an ActiveModel.
2. It was inconsistent with respect to other methods in the class since they all return ActiveModels.
parent 83f79ced
...@@ -14,7 +14,7 @@ class Projects::AutocompleteSourcesController < Projects::ApplicationController ...@@ -14,7 +14,7 @@ class Projects::AutocompleteSourcesController < Projects::ApplicationController
end end
def labels def labels
render json: @autocomplete_service.labels(target) render json: @autocomplete_service.labels_as_hash(target)
end end
def milestones def milestones
......
...@@ -20,24 +20,28 @@ module Projects ...@@ -20,24 +20,28 @@ module Projects
MergeRequestsFinder.new(current_user, project_id: project.id, state: 'opened').execute.select([:iid, :title]) MergeRequestsFinder.new(current_user, project_id: project.id, state: 'opened').execute.select([:iid, :title])
end end
def labels(target = nil) def labels_as_hash(target = nil)
labels = LabelsFinder.new(current_user, project_id: project.id, include_ancestor_groups: true) available_labels = LabelsFinder.new(
.execute.select([:color, :title]) current_user,
project_id: project.id,
include_ancestor_groups: true
).execute
return labels unless target&.respond_to?(:labels) label_hashes = available_labels.as_json(only: [:title, :color])
issuable_label_titles = target.labels.pluck(:title) if target&.respond_to?(:labels)
already_set_labels = available_labels & target.labels
if issuable_label_titles if already_set_labels.present?
labels = labels.as_json(only: [:title, :color]) titles = already_set_labels.map(&:title)
label_hashes.each do |hash|
issuable_label_titles.each do |issuable_label_title| if titles.include?(hash['title'])
found_label = labels.find { |label| label['title'] == issuable_label_title } hash[:set] = true
found_label[:set] = true if found_label end
end
end end
end end
labels label_hashes
end end
def commands(noteable, type) def commands(noteable, type)
......
...@@ -131,4 +131,58 @@ describe Projects::AutocompleteService do ...@@ -131,4 +131,58 @@ describe Projects::AutocompleteService do
end end
end end
end end
describe '#labels_as_hash' do
def expect_labels_to_equal(labels, expected_labels)
expect(labels.size).to eq(expected_labels.size)
extract_title = lambda { |label| label['title'] }
expect(labels.map(&extract_title)).to eq(expected_labels.map(&extract_title))
end
let(:user) { create(:user) }
let(:group) { create(:group, :nested) }
let!(:sub_group) { create(:group, parent: group) }
let(:project) { create(:project, :public, group: group) }
let(:issue) { create(:issue, project: project) }
let!(:label1) { create(:label, project: project) }
let!(:label2) { create(:label, project: project) }
let!(:sub_group_label) { create(:group_label, group: sub_group) }
let!(:parent_group_label) { create(:group_label, group: group.parent) }
before do
create(:group_member, group: group, user: user)
end
it 'returns labels from project and ancestor groups' do
service = described_class.new(project, user)
results = service.labels_as_hash
expected_labels = [label1, label2, parent_group_label]
expect_labels_to_equal(results, expected_labels)
end
context 'some labels are already assigned' do
before do
issue.labels << label1
end
it 'marks already assigned as set' do
service = described_class.new(project, user)
results = service.labels_as_hash(issue)
expected_labels = [label1, label2, parent_group_label]
expect_labels_to_equal(results, expected_labels)
assigned_label_titles = issue.labels.map(&:title)
results.each do |hash|
if assigned_label_titles.include?(hash['title'])
expect(hash[:set]).to eq(true)
else
expect(hash.key?(:set)).to eq(false)
end
end
end
end
end
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