Commit a0b21f15 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Merge branch 'bw-backlog-create_service' into 'master'

Add ability to board list create service to create backlog list

See merge request gitlab-org/gitlab!38648
parents 18e224eb 71dac72c
......@@ -7,34 +7,39 @@ module Boards
def execute(board)
List.transaction do
case type
when :backlog
create_backlog(board)
else
target = target(board)
position = next_position(board)
create_list(board, type, target, position)
end
end
end
private
def type
# We don't ever expect to have more than one list
# type param at once.
if params.key?('backlog')
:backlog
else
:label
end
end
def target(board)
strong_memoize(:target) do
available_labels_for(board).find(params[:label_id])
end
available_labels.find(params[:label_id])
end
def available_labels_for(board)
options = { include_ancestor_groups: true }
if board.group_board?
options.merge!(group_id: parent.id, only_group_labels: true)
else
options[:project_id] = parent.id
end
LabelsFinder.new(current_user, options).execute
def available_labels
::Labels::AvailableLabelsService.new(current_user, parent, {})
.available_labels
end
def next_position(board)
......@@ -49,6 +54,12 @@ module Boards
def create_list_attributes(type, target, position)
{ type => target, list_type: type, position: position }
end
def create_backlog(board)
return board.lists.backlog.first if board.lists.backlog.exists?
board.lists.create(list_type: :backlog, position: nil)
end
end
end
end
......
......@@ -39,12 +39,12 @@ module Labels
ids.map(&:to_i) & existing_ids
end
private
def available_labels
@available_labels ||= LabelsFinder.new(current_user, finder_params).execute
end
private
def finder_params
params = { include_ancestor_groups: true }
......
......@@ -59,6 +59,21 @@ RSpec.describe Boards::Lists::CreateService do
expect { service.execute(board) }.to raise_error(ActiveRecord::RecordNotFound)
end
end
context 'when backlog param is sent' do
it 'creates one and only one backlog list' do
service = described_class.new(parent, user, 'backlog' => true)
list = service.execute(board)
expect(list.list_type).to eq('backlog')
expect(list.position).to be_nil
expect(list).to be_valid
another_backlog = service.execute(board)
expect(another_backlog).to eq list
end
end
end
context 'when board parent is a project' do
......
......@@ -10,7 +10,7 @@ RSpec.describe Labels::AvailableLabelsService do
let(:other_project_label) { create(:label) }
let(:group_label) { create(:group_label, group: group) }
let(:other_group_label) { create(:group_label) }
let(:labels) { [project_label, other_project_label, group_label, other_group_label] }
let!(:labels) { [project_label, other_project_label, group_label, other_group_label] }
describe '#find_or_create_by_titles' do
let(:label_titles) { labels.map(&:title).push('non existing title') }
......@@ -89,4 +89,26 @@ RSpec.describe Labels::AvailableLabelsService do
end
end
end
describe '#available_labels' do
context 'when parent is a project' do
it 'returns only relevant labels' do
result = described_class.new(user, project, {}).available_labels
expect(result.count).to eq(2)
expect(result).to include(project_label, group_label)
expect(result).not_to include(other_project_label, other_group_label)
end
end
context 'when parent is a group' do
it 'returns only relevant labels' do
result = described_class.new(user, group, {}).available_labels
expect(result.count).to eq(1)
expect(result).to include(group_label)
expect(result).not_to include(project_label, other_project_label, other_group_label)
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