diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-11-19 11:27:35 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-11-19 11:27:35 +0300 |
commit | 7e9c479f7de77702622631cff2628a9c8dcbc627 (patch) | |
tree | c8f718a08e110ad7e1894510980d2155a6549197 /app/models/alert_management | |
parent | e852b0ae16db4052c1c567d9efa4facc81146e88 (diff) |
Add latest changes from gitlab-org/gitlab@13-6-stable-eev13.6.0-rc42
Diffstat (limited to 'app/models/alert_management')
-rw-r--r-- | app/models/alert_management/alert.rb | 2 | ||||
-rw-r--r-- | app/models/alert_management/http_integration.rb | 44 |
2 files changed, 39 insertions, 7 deletions
diff --git a/app/models/alert_management/alert.rb b/app/models/alert_management/alert.rb index 61cc15a522e..7ce7f40b6a8 100644 --- a/app/models/alert_management/alert.rb +++ b/app/models/alert_management/alert.rb @@ -34,7 +34,7 @@ module AlertManagement has_many :ordered_notes, -> { fresh }, as: :noteable, class_name: 'Note' has_many :user_mentions, class_name: 'AlertManagement::AlertUserMention', foreign_key: :alert_management_alert_id - has_internal_id :iid, scope: :project, init: ->(s) { s.project.alert_management_alerts.maximum(:iid) } + has_internal_id :iid, scope: :project sha_attribute :fingerprint diff --git a/app/models/alert_management/http_integration.rb b/app/models/alert_management/http_integration.rb index 7f954e1d384..ae5170867c3 100644 --- a/app/models/alert_management/http_integration.rb +++ b/app/models/alert_management/http_integration.rb @@ -2,6 +2,10 @@ module AlertManagement class HttpIntegration < ApplicationRecord + include ::Gitlab::Routing + LEGACY_IDENTIFIER = 'legacy' + DEFAULT_NAME_SLUG = 'http-endpoint' + belongs_to :project, inverse_of: :alert_management_http_integrations attr_encrypted :token, @@ -9,19 +13,45 @@ module AlertManagement key: Settings.attr_encrypted_db_key_base_truncated, algorithm: 'aes-256-gcm' + default_value_for(:endpoint_identifier, allows_nil: false) { SecureRandom.hex(8) } + default_value_for(:token) { generate_token } + validates :project, presence: true validates :active, inclusion: { in: [true, false] } - - validates :token, presence: true + validates :token, presence: true, format: { with: /\A\h{32}\z/ } validates :name, presence: true, length: { maximum: 255 } - validates :endpoint_identifier, presence: true, length: { maximum: 255 } + validates :endpoint_identifier, presence: true, length: { maximum: 255 }, format: { with: /\A[A-Za-z0-9]+\z/ } validates :endpoint_identifier, uniqueness: { scope: [:project_id, :active] }, if: :active? before_validation :prevent_token_assignment + before_validation :prevent_endpoint_identifier_assignment before_validation :ensure_token + scope :for_endpoint_identifier, -> (endpoint_identifier) { where(endpoint_identifier: endpoint_identifier) } + scope :active, -> { where(active: true) } + scope :ordered_by_id, -> { order(:id) } + + def url + return project_alerts_notify_url(project, format: :json) if legacy? + + project_alert_http_integration_url(project, name_slug, endpoint_identifier, format: :json) + end + private + def self.generate_token + SecureRandom.hex + end + + def name_slug + (name && Gitlab::Utils.slugify(name)) || DEFAULT_NAME_SLUG + end + + def legacy? + endpoint_identifier == LEGACY_IDENTIFIER + end + + # Blank token assignment triggers token reset def prevent_token_assignment if token.present? && token_changed? self.token = nil @@ -31,11 +61,13 @@ module AlertManagement end def ensure_token - self.token = generate_token if token.blank? + self.token = self.class.generate_token if token.blank? end - def generate_token - SecureRandom.hex + def prevent_endpoint_identifier_assignment + if endpoint_identifier_changed? && endpoint_identifier_was.present? + self.endpoint_identifier = endpoint_identifier_was + end end end end |