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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
# frozen_string_literal: true
module Integrations
module SlackInteractions
module IncidentManagement
class IncidentModalSubmitService
include GitlabRoutingHelper
include Gitlab::Routing
IssueCreateError = Class.new(StandardError)
def initialize(params)
@params = params
@values = params.dig(:view, :state, :values)
@team_id = params.dig(:team, :id)
@user_id = params.dig(:user, :id)
@additional_message = ''
end
def execute
create_response = Issues::CreateService.new(
container: project,
current_user: find_user.user,
params: incident_params,
spam_params: nil
).execute
raise IssueCreateError, create_response.errors.to_sentence if create_response.error?
incident = create_response.payload[:issue]
incident_link = incident_link_text(incident)
response = send_to_slack(incident_link)
return ServiceResponse.success(payload: { incident: incident }) if response['ok']
ServiceResponse.error(
message: _('Something went wrong when sending the incident link to Slack.'),
payload: response
).track_exception(
response: response.to_h,
slack_workspace_id: team_id,
slack_user_id: user_id
)
rescue StandardError => e
send_to_slack(_('There was a problem creating the incident. Please try again.'))
ServiceResponse
.error(
message: e.message
).track_exception(
slack_workspace_id: team_id,
slack_user_id: user_id,
as: e.class
)
end
private
attr_accessor :params, :values, :team_id, :user_id, :additional_message
def incident_params
{
title: values.dig(:title_input, :title, :value),
severity: severity,
confidential: confidential?,
description: description,
escalation_status: { status: status },
issue_type: "incident",
assignee_ids: [assignee],
label_ids: labels
}
end
def strip_markup(string)
SlackMarkdownSanitizer.sanitize(string)
end
def send_to_slack(text)
response_url = params.dig(:view, :private_metadata)
body = {
replace_original: 'true',
text: text
}
Gitlab::HTTP.post(
response_url,
body: Gitlab::Json.dump(body),
headers: { 'Content-Type' => 'application/json' }
)
end
def incident_link_text(incident)
"#{_('New incident has been created')}: " \
"<#{issue_url(incident)}|#{incident.to_reference} " \
"- #{strip_markup(incident.title)}>. #{@additional_message}"
end
def project
project_id = values.dig(
:project_and_severity_selector,
:incident_management_project,
:selected_option,
:value)
Project.find(project_id)
end
def find_user
ChatNames::FindUserService.new(team_id, user_id).execute
end
def description
description =
values.dig(:incident_description, :description, :value) ||
values.dig(project.id.to_s.to_sym, :description, :value)
zoom_link = values.dig(:zoom, :link, :value)
return description if zoom_link.blank?
"#{description} \n/zoom #{zoom_link}"
end
def confidential?
values.dig(:confidentiality, :confidential, :selected_options).present?
end
def severity
values.dig(:project_and_severity_selector, :severity, :selected_option, :value) || 'unknown'
end
def status
values.dig(:status_and_assignee_selector, :status, :selected_option, :value)
end
def assignee
assignee_id = values.dig(:status_and_assignee_selector, :assignee, :selected_option, :value)
return unless assignee_id
user = User.find_by_id(assignee_id)
member = project.member(user)
unless member
@additional_message =
"However, " \
"#{user.name} was not assigned to the incident as they are not a member in #{project.name}."
return
end
member.user_id
end
def labels
values.dig(:label_selector, :labels, :selected_options)&.pluck(:value)
end
end
end
end
end
|