Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'app/services/alert_management/create_alert_issue_service.rb')
-rw-r--r--app/services/alert_management/create_alert_issue_service.rb65
1 files changed, 45 insertions, 20 deletions
diff --git a/app/services/alert_management/create_alert_issue_service.rb b/app/services/alert_management/create_alert_issue_service.rb
index beacd240b08..6ea3fd867ef 100644
--- a/app/services/alert_management/create_alert_issue_service.rb
+++ b/app/services/alert_management/create_alert_issue_service.rb
@@ -2,6 +2,8 @@
module AlertManagement
class CreateAlertIssueService
+ include Gitlab::Utils::StrongMemoize
+
# @param alert [AlertManagement::Alert]
# @param user [User]
def initialize(alert, user)
@@ -13,18 +15,20 @@ module AlertManagement
return error_no_permissions unless allowed?
return error_issue_already_exists if alert.issue
- result = create_issue(alert, user, alert_payload)
- @issue = result[:issue]
+ result = create_issue
+ issue = result.payload[:issue]
+
+ return error(result.message, issue) if result.error?
+ return error(object_errors(alert), issue) unless associate_alert_with_issue(issue)
- return error(result[:message]) if result[:status] == :error
- return error(alert.errors.full_messages.to_sentence) unless update_alert_issue_id
+ SystemNoteService.new_alert_issue(alert, issue, user)
- success
+ result
end
private
- attr_reader :alert, :user, :issue
+ attr_reader :alert, :user
delegate :project, to: :alert
@@ -32,29 +36,36 @@ module AlertManagement
user.can?(:create_issue, project)
end
- def create_issue(alert, user, alert_payload)
- ::IncidentManagement::CreateIssueService
- .new(project, alert_payload, user)
- .execute(skip_settings_check: true)
- end
+ def create_issue
+ label_result = find_or_create_incident_label
- def alert_payload
- if alert.prometheus?
- alert.payload
- else
- Gitlab::Alerting::NotificationPayloadParser.call(alert.payload.to_h)
- end
+ # Create an unlabelled issue if we couldn't create the label
+ # due to a race condition.
+ # See https://gitlab.com/gitlab-org/gitlab-foss/issues/65042
+ extra_params = label_result.success? ? { label_ids: [label_result.payload[:label].id] } : {}
+
+ issue = Issues::CreateService.new(
+ project,
+ user,
+ title: alert_presenter.title,
+ description: alert_presenter.issue_description,
+ **extra_params
+ ).execute
+
+ return error(object_errors(issue), issue) unless issue.valid?
+
+ success(issue)
end
- def update_alert_issue_id
+ def associate_alert_with_issue(issue)
alert.update(issue_id: issue.id)
end
- def success
+ def success(issue)
ServiceResponse.success(payload: { issue: issue })
end
- def error(message)
+ def error(message, issue = nil)
ServiceResponse.error(payload: { issue: issue }, message: message)
end
@@ -65,5 +76,19 @@ module AlertManagement
def error_no_permissions
error(_('You have no permissions'))
end
+
+ def alert_presenter
+ strong_memoize(:alert_presenter) do
+ alert.present
+ end
+ end
+
+ def find_or_create_incident_label
+ IncidentManagement::CreateIncidentLabelService.new(project, user).execute
+ end
+
+ def object_errors(object)
+ object.errors.full_messages.to_sentence
+ end
end
end