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

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

module IncidentManagement
  module LinkAlerts
    class CreateService < BaseService
      # @param incident [Issue] an incident to link alerts
      # @param current_user [User]
      # @param alert_references [[String]] a list of alert references. Can be either a short reference or URL
      #   Examples:
      #     "^alert#IID"
      #     "https://gitlab.com/company/project/-/alert_management/IID/details"
      def initialize(incident, current_user, alert_references)
        @incident = incident
        @current_user = current_user
        @alert_references = alert_references

        super(project: incident.project, current_user: current_user)
      end

      def execute
        return error_no_permissions unless allowed?

        references = extract_alerts_from_references
        incident.alert_management_alerts << references if references.present?

        success
      end

      private

      attr_reader :alert_references

      def extract_alerts_from_references
        text = alert_references.join(' ')
        extractor = Gitlab::ReferenceExtractor.new(project, current_user)
        extractor.analyze(text, {})

        extractor.alerts
      end
    end
  end
end