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

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

module IncidentManagement
  class CreateIssueService < BaseService
    include Gitlab::Utils::StrongMemoize

    def initialize(project, params)
      super(project, User.alert_bot, params)
    end

    def execute
      return error_with('setting disabled') unless incident_management_setting.create_issue?
      return error_with('invalid alert') unless alert.valid?

      issue = create_issue
      return error_with(issue_errors(issue)) unless issue.valid?

      success(issue: issue)
    end

    private

    def create_issue
      label_result = find_or_create_incident_label

      # 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] } : {}

      Issues::CreateService.new(
        project,
        current_user,
        title: issue_title,
        description: issue_description,
        **extra_params
      ).execute
    end

    def issue_title
      alert.full_title
    end

    def issue_description
      horizontal_line = "\n\n---\n\n"

      [
        alert_summary,
        alert_markdown,
        issue_template_content
      ].compact.join(horizontal_line)
    end

    def find_or_create_incident_label
      IncidentManagement::CreateIncidentLabelService.new(project, current_user).execute
    end

    def alert_summary
      alert.issue_summary_markdown
    end

    def alert_markdown
      alert.alert_markdown
    end

    def alert
      strong_memoize(:alert) do
        Gitlab::Alerting::Alert.new(project: project, payload: params).present
      end
    end

    def issue_template_content
      incident_management_setting.issue_template_content
    end

    def incident_management_setting
      strong_memoize(:incident_management_setting) do
        project.incident_management_setting ||
          project.build_incident_management_setting
      end
    end

    def issue_errors(issue)
      issue.errors.full_messages.to_sentence
    end

    def error_with(message)
      log_error(%{Cannot create incident issue for "#{project.full_name}": #{message}})

      error(message)
    end
  end
end