-
Notifications
You must be signed in to change notification settings - Fork 1
/
epic_links.rb
128 lines (104 loc) · 3.87 KB
/
epic_links.rb
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# frozen_string_literal: true
module API
class EpicLinks < Grape::API::Instance
include ::Gitlab::Utils::StrongMemoize
before do
authenticate!
end
helpers ::API::Helpers::EpicsHelpers
helpers do
def child_epic
strong_memoize(:child_epic) do
find_epics(finder_params: { group_id: user_group.id })
.find_by_id(declared_params[:child_epic_id])
end
end
def child_epics
EpicsFinder.new(current_user, {
parent_id: epic.id,
group_id: user_group.id,
sort: 'relative_position'
}).execute
end
params :child_epic_id do
# Unique ID should be used because epics from other groups can be assigned as child.
requires :child_epic_id, type: Integer, desc: 'The global ID of the epic that will be assigned as child'
end
end
params do
requires :id, type: String, desc: 'The ID of a group'
requires :epic_iid, type: Integer, desc: 'The internal ID of an epic'
end
resource :groups, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
desc 'Get related epics' do
success EE::API::Entities::Epic
end
get ':id/(-/)epics/:epic_iid/epics' do
authorize_epics_feature!
authorize_can_read!
present child_epics, with: EE::API::Entities::Epic
end
desc 'Relate epics' do
success EE::API::Entities::Epic
end
params do
use :child_epic_id
end
post ':id/(-/)epics/:epic_iid/epics/:child_epic_id' do
authorize_subepics_feature!
authorize_can_admin_epic_link!
create_params = { target_issuable: child_epic }
result = ::EpicLinks::CreateService.new(epic, current_user, create_params).execute
if result[:status] == :success
present child_epic, with: EE::API::Entities::Epic
else
render_api_error!(result[:message], result[:http_status])
end
end
desc 'Create and relate epic to a parent' do
success EE::API::Entities::Epic
end
params do
requires :title, type: String, desc: 'The title of a child epic'
end
post ':id/(-/)epics/:epic_iid/epics' do
authorize_subepics_feature!
authorize_can_admin_epic_link!
create_params = { parent_id: epic.id, title: params[:title] }
child_epic = ::Epics::CreateService.new(user_group, current_user, create_params).execute
if child_epic.valid?
present child_epic, with: EE::API::Entities::LinkedEpic, user: current_user
else
render_validation_error!(child_epic)
end
end
desc 'Remove epics relation'
params do
use :child_epic_id
end
delete ':id/(-/)epics/:epic_iid/epics/:child_epic_id' do
authorize_subepics_feature!
authorize_can_admin_epic_link!
updated_epic = ::Epics::UpdateService.new(user_group, current_user, { parent: nil }).execute(child_epic)
present updated_epic, with: EE::API::Entities::Epic
end
desc 'Reorder child epics'
params do
use :child_epic_id
optional :move_before_id, type: Integer, desc: 'The id of the epic that should be positioned before the child epic'
optional :move_after_id, type: Integer, desc: 'The id of the epic that should be positioned after the child epic'
end
put ':id/(-/)epics/:epic_iid/epics/:child_epic_id' do
authorize_subepics_feature!
authorize_can_admin_epic_link!
update_params = params.slice(:move_before_id, :move_after_id)
result = ::EpicLinks::UpdateService.new(child_epic, current_user, update_params).execute
if result[:status] == :success
present child_epics, with: EE::API::Entities::Epic
else
render_api_error!(result[:message], result[:http_status])
end
end
end
end
end