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

process_alert_worker.rb « incident_management « workers « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5d5c10014f8a16e85f629e4f1b68d816b50c5f77 (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
# frozen_string_literal: true

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

    queue_namespace :incident_management
    feature_category :incident_management

    # `project_id` and `alert_payload` are deprecated and can be removed
    # starting from 14.0 release
    # https://gitlab.com/gitlab-org/gitlab/-/issues/224500
    def perform(_project_id = nil, _alert_payload = nil, alert_id = nil)
      return unless alert_id

      alert = find_alert(alert_id)
      return unless alert

      new_issue = create_issue_for(alert)
      return unless new_issue&.persisted?

      link_issue_with_alert(alert, new_issue.id)
    end

    private

    def find_alert(alert_id)
      AlertManagement::Alert.find_by_id(alert_id)
    end

    def parsed_payload(alert)
      if alert.prometheus?
        alert.payload
      else
        Gitlab::Alerting::NotificationPayloadParser.call(alert.payload.to_h, alert.project)
      end
    end

    def create_issue_for(alert)
      IncidentManagement::CreateIssueService
        .new(alert.project, parsed_payload(alert))
        .execute
        .dig(:issue)
    end

    def link_issue_with_alert(alert, issue_id)
      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