diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-05-20 17:34:42 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-05-20 17:34:42 +0300 |
commit | 9f46488805e86b1bc341ea1620b866016c2ce5ed (patch) | |
tree | f9748c7e287041e37d6da49e0a29c9511dc34768 /app/finders/alert_management/alerts_finder.rb | |
parent | dfc92d081ea0332d69c8aca2f0e745cb48ae5e6d (diff) |
Add latest changes from gitlab-org/gitlab@13-0-stable-ee
Diffstat (limited to 'app/finders/alert_management/alerts_finder.rb')
-rw-r--r-- | app/finders/alert_management/alerts_finder.rb | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/app/finders/alert_management/alerts_finder.rb b/app/finders/alert_management/alerts_finder.rb new file mode 100644 index 00000000000..cb35be43c15 --- /dev/null +++ b/app/finders/alert_management/alerts_finder.rb @@ -0,0 +1,55 @@ +# frozen_string_literal: true + +module AlertManagement + class AlertsFinder + # @return [Hash<Integer,Integer>] Mapping of status id to count + # ex) { 0: 6, ...etc } + def self.counts_by_status(current_user, project, params = {}) + new(current_user, project, params).execute.counts_by_status + end + + def initialize(current_user, project, params) + @current_user = current_user + @project = project + @params = params + end + + def execute + return AlertManagement::Alert.none unless authorized? + + collection = project.alert_management_alerts + collection = by_status(collection) + collection = by_search(collection) + collection = by_iid(collection) + sort(collection) + end + + private + + attr_reader :current_user, :project, :params + + def by_iid(collection) + return collection unless params[:iid] + + collection.for_iid(params[:iid]) + end + + def by_status(collection) + values = AlertManagement::Alert::STATUSES.values & Array(params[:status]) + + values.present? ? collection.for_status(values) : collection + end + + def by_search(collection) + params[:search].present? ? collection.search(params[:search]) : collection + end + + def sort(collection) + params[:sort] ? collection.sort_by_attribute(params[:sort]) : collection + end + + def authorized? + Ability.allowed?(current_user, :read_alert_management_alert, project) + end + end +end |