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

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

require 'securerandom'

module Alerting
  class ProjectAlertingSetting < ApplicationRecord
    belongs_to :project

    validates :token, presence: true

    attr_encrypted :token,
      mode: :per_attribute_iv,
      key: Settings.attr_encrypted_db_key_base_32,
      algorithm: 'aes-256-gcm'

    before_validation :ensure_token
    after_create :create_http_integration
    after_update :sync_http_integration

    private

    def ensure_token
      self.token ||= generate_token
    end

    def generate_token
      SecureRandom.hex
    end

    # Remove in next required stop after %16.4
    # https://gitlab.com/gitlab-org/gitlab/-/issues/338838
    def sync_http_integration
      project.alert_management_http_integrations
        .for_endpoint_identifier('legacy-prometheus')
        .take
        &.update_columns(
          encrypted_token: encrypted_token,
          encrypted_token_iv: encrypted_token_iv
        )
    end

    # Remove in next required stop after %16.4
    # https://gitlab.com/gitlab-org/gitlab/-/issues/338838
    def create_http_integration
      AlertManagement::HttpIntegration.insert({
        project_id: project_id,
        encrypted_token: encrypted_token,
        encrypted_token_iv: encrypted_token_iv,
        active: true,
        name: 'Prometheus',
        endpoint_identifier: 'legacy-prometheus',
        type_identifier: :prometheus
      })
    end
  end
end