blob: 085d6c6b0e736d0dbac48580f7a3a1a60760ca16 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# frozen_string_literal: true
module WorkItems
module Widgets
module HierarchyService
class BaseService < WorkItems::Widgets::BaseService
private
def handle_hierarchy_changes(params)
return feature_flag_error unless feature_flag_enabled?
return incompatible_args_error if incompatible_args?(params)
if params.key?(:parent)
update_work_item_parent(params.delete(:parent))
elsif params.key?(:children)
update_work_item_children(params.delete(:children))
else
invalid_args_error
end
end
def update_work_item_parent(parent)
if parent.nil?
remove_parent
else
set_parent(parent)
end
end
def set_parent(parent)
::WorkItems::ParentLinks::CreateService
.new(parent, current_user, { target_issuable: widget.work_item })
.execute
end
# rubocop: disable CodeReuse/ActiveRecord
def remove_parent
link = ::WorkItems::ParentLink.find_by(work_item: widget.work_item)
return success unless link.present?
::WorkItems::ParentLinks::DestroyService.new(link, current_user).execute
end
# rubocop: enable CodeReuse/ActiveRecord
def update_work_item_children(children)
::WorkItems::ParentLinks::CreateService
.new(widget.work_item, current_user, { issuable_references: children })
.execute
end
def feature_flag_enabled?
Feature.enabled?(:work_items_hierarchy, widget.work_item&.project)
end
def incompatible_args?(params)
params[:children] && params[:parent]
end
def feature_flag_error
error(_('`work_items_hierarchy` feature flag disabled for this project'))
end
def incompatible_args_error
error(_('A Work Item can be a parent or a child, but not both.'))
end
def invalid_args_error
error(_("One or more arguments are invalid: %{args}." % { args: params.keys.to_sentence } ))
end
def service_response!(result)
return result unless result[:status] == :error
raise WidgetError, result[:message]
end
end
end
end
end
|