diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-06-18 14:18:50 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-06-18 14:18:50 +0300 |
commit | 8c7f4e9d5f36cff46365a7f8c4b9c21578c1e781 (patch) | |
tree | a77e7fe7a93de11213032ed4ab1f33a3db51b738 /app/models/alert_management | |
parent | 00b35af3db1abfe813a778f643dad221aad51fca (diff) |
Add latest changes from gitlab-org/gitlab@13-1-stable-ee
Diffstat (limited to 'app/models/alert_management')
-rw-r--r-- | app/models/alert_management/alert.rb | 48 | ||||
-rw-r--r-- | app/models/alert_management/alert_assignee.rb | 11 | ||||
-rw-r--r-- | app/models/alert_management/alert_user_mention.rb | 8 |
3 files changed, 58 insertions, 9 deletions
diff --git a/app/models/alert_management/alert.rb b/app/models/alert_management/alert.rb index acaf474ecc2..af60ddd6f9a 100644 --- a/app/models/alert_management/alert.rb +++ b/app/models/alert_management/alert.rb @@ -1,10 +1,14 @@ # frozen_string_literal: true +require_dependency 'alert_management' + module AlertManagement class Alert < ApplicationRecord + include IidRoutes include AtomicInternalId include ShaAttribute include Sortable + include Noteable include Gitlab::SQL::Pattern STATUSES = { @@ -23,9 +27,15 @@ module AlertManagement belongs_to :project belongs_to :issue, optional: true - has_internal_id :iid, scope: :project, init: ->(s) { s.project.alert_management_alerts.maximum(:iid) } - self.table_name = 'alert_management_alerts' + has_many :alert_assignees, inverse_of: :alert + has_many :assignees, through: :alert_assignees + + has_many :notes, as: :noteable, inverse_of: :noteable, dependent: :delete_all # rubocop:disable Cop/ActiveRecordDependent + 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) } sha_attribute :fingerprint @@ -102,7 +112,7 @@ module AlertManagement scope :order_start_time, -> (sort_order) { order(started_at: sort_order) } scope :order_end_time, -> (sort_order) { order(ended_at: sort_order) } - scope :order_events_count, -> (sort_order) { order(events: sort_order) } + scope :order_event_count, -> (sort_order) { order(events: sort_order) } scope :order_severity, -> (sort_order) { order(severity: sort_order) } scope :order_status, -> (sort_order) { order(status: sort_order) } @@ -110,12 +120,12 @@ module AlertManagement def self.sort_by_attribute(method) case method.to_s - when 'start_time_asc' then order_start_time(:asc) - when 'start_time_desc' then order_start_time(:desc) - when 'end_time_asc' then order_end_time(:asc) - when 'end_time_desc' then order_end_time(:desc) - when 'events_count_asc' then order_events_count(:asc) - when 'events_count_desc' then order_events_count(:desc) + when 'started_at_asc' then order_start_time(:asc) + when 'started_at_desc' then order_start_time(:desc) + when 'ended_at_asc' then order_end_time(:asc) + when 'ended_at_desc' then order_end_time(:desc) + when 'event_count_asc' then order_event_count(:asc) + when 'event_count_desc' then order_event_count(:desc) when 'severity_asc' then order_severity(:asc) when 'severity_desc' then order_severity(:desc) when 'status_asc' then order_status(:asc) @@ -135,8 +145,28 @@ module AlertManagement monitoring_tool == Gitlab::AlertManagement::AlertParams::MONITORING_TOOLS[:prometheus] end + def register_new_event! + increment!(:events) + end + + # required for todos (typically contains an identifier like issue iid) + # no-op; we could use iid, but we don't have a reference prefix + def to_reference(_from = nil, full: false) + '' + end + + def execute_services + return unless project.has_active_services?(:alert_hooks) + + project.execute_services(hook_data, :alert_hooks) + end + private + def hook_data + Gitlab::DataBuilder::Alert.build(self) + end + def hosts_length return unless hosts diff --git a/app/models/alert_management/alert_assignee.rb b/app/models/alert_management/alert_assignee.rb new file mode 100644 index 00000000000..c74b2699182 --- /dev/null +++ b/app/models/alert_management/alert_assignee.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +module AlertManagement + class AlertAssignee < ApplicationRecord + belongs_to :alert, inverse_of: :alert_assignees + belongs_to :assignee, class_name: 'User', foreign_key: :user_id + + validates :alert, presence: true + validates :assignee, presence: true, uniqueness: { scope: :alert_id } + end +end diff --git a/app/models/alert_management/alert_user_mention.rb b/app/models/alert_management/alert_user_mention.rb new file mode 100644 index 00000000000..d36aa80ee05 --- /dev/null +++ b/app/models/alert_management/alert_user_mention.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +module AlertManagement + class AlertUserMention < UserMention + belongs_to :alert_management_alert, class_name: '::AlertManagement::Alert' + belongs_to :note + end +end |