summaryrefslogtreecommitdiff
path: root/app/workers/incident_management/process_alert_worker.rb
blob: 0af34fa35d589b7275fbda468a70c343c9581435 (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
# frozen_string_literal: true

module IncidentManagement
  class ProcessAlertWorker # rubocop:disable Scalability/IdempotentWorker
    include ApplicationWorker

    queue_namespace :incident_management
    feature_category :incident_management

    def perform(project_id, alert_payload, am_alert_id = nil)
      project = find_project(project_id)
      return unless project

      new_issue = create_issue(project, alert_payload)
      return unless am_alert_id && new_issue&.persisted?

      link_issue_with_alert(am_alert_id, new_issue.id)
    end

    private

    def find_project(project_id)
      Project.find_by_id(project_id)
    end

    def create_issue(project, alert_payload)
      IncidentManagement::CreateIssueService
        .new(project, alert_payload)
        .execute
        .dig(:issue)
    end

    def link_issue_with_alert(alert_id, issue_id)
      alert = AlertManagement::Alert.find_by_id(alert_id)
      return unless alert

      return if alert.update(issue_id: issue_id)

      Gitlab::AppLogger.warn(
        message: 'Cannot link an Issue with Alert',
        issue_id: issue_id,
        alert_id: alert_id,
        alert_errors: alert.errors.messages
      )
    end
  end
end