summaryrefslogtreecommitdiff
path: root/app/models/incident_management/project_incident_management_setting.rb
blob: c79acdb685fe2835baf0e1f3bfdec0f0c4d47b15 (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
# frozen_string_literal: true

module IncidentManagement
  class ProjectIncidentManagementSetting < ApplicationRecord
    include Gitlab::Utils::StrongMemoize

    belongs_to :project

    validate :issue_template_exists, if: :create_issue?

    before_validation :ensure_pagerduty_token

    attr_encrypted :pagerduty_token,
      mode: :per_attribute_iv,
      key: ::Settings.attr_encrypted_db_key_base_truncated,
      algorithm: 'aes-256-gcm',
      encode: false, # No need to encode for binary column https://github.com/attr-encrypted/attr_encrypted#the-encode-encode_iv-encode_salt-and-default_encoding-options
      encode_iv: false

    def available_issue_templates
      Gitlab::Template::IssueTemplate.all(project)
    end

    def issue_template_content
      strong_memoize(:issue_template_content) do
        issue_template&.content if issue_template_key.present?
      end
    end

    private

    def issue_template_exists
      return unless issue_template_key.present?

      errors.add(:issue_template_key, 'not found') unless issue_template
    end

    def issue_template
      Gitlab::Template::IssueTemplate.find(issue_template_key, project)
    rescue Gitlab::Template::Finders::RepoTemplateFinder::FileNotFoundError
    end

    def ensure_pagerduty_token
      return unless pagerduty_active

      self.pagerduty_token ||= generate_pagerduty_token
    end

    def generate_pagerduty_token
      SecureRandom.hex
    end
  end
end