diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-07 00:08:48 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-07 00:08:48 +0300 |
commit | a89cb5cbdd832d4d9e80517973aceda6bc0a3856 (patch) | |
tree | 574475bd0901a2f8906d36a4728b8bbb95b41e1c /app/finders | |
parent | 0d6fa033121a9bef708b8f2de186c4034c61d4a3 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/finders')
-rw-r--r-- | app/finders/projects/prometheus/alerts_finder.rb | 71 | ||||
-rw-r--r-- | app/finders/protected_branches_finder.rb | 35 |
2 files changed, 106 insertions, 0 deletions
diff --git a/app/finders/projects/prometheus/alerts_finder.rb b/app/finders/projects/prometheus/alerts_finder.rb new file mode 100644 index 00000000000..3e3b72647c5 --- /dev/null +++ b/app/finders/projects/prometheus/alerts_finder.rb @@ -0,0 +1,71 @@ +# frozen_string_literal: true + +module Projects + module Prometheus + # Find Prometheus alerts by +project+, +environment+, +id+, + # or any combo thereof. + # + # Optionally filter by +metric+. + # + # Arguments: + # params: + # project: Project | integer + # environment: Environment | integer + # metric: PrometheusMetric | integer + class AlertsFinder + def initialize(params = {}) + unless params[:project] || params[:environment] || params[:id] + raise ArgumentError, + 'Please provide one or more of the following params: :project, :environment, :id' + end + + @params = params + end + + # Find all matching alerts + # + # @return [ActiveRecord::Relation<PrometheusAlert>] + def execute + relation = by_project(PrometheusAlert) + relation = by_environment(relation) + relation = by_metric(relation) + relation = by_id(relation) + relation = ordered(relation) + + relation + end + + private + + attr_reader :params + + def by_project(relation) + return relation unless params[:project] + + relation.for_project(params[:project]) + end + + def by_environment(relation) + return relation unless params[:environment] + + relation.for_environment(params[:environment]) + end + + def by_metric(relation) + return relation unless params[:metric] + + relation.for_metric(params[:metric]) + end + + def by_id(relation) + return relation unless params[:id] + + relation.id_in(params[:id]) + end + + def ordered(relation) + relation.order_by('id_asc') + end + end + end +end diff --git a/app/finders/protected_branches_finder.rb b/app/finders/protected_branches_finder.rb new file mode 100644 index 00000000000..68e8d2a9f54 --- /dev/null +++ b/app/finders/protected_branches_finder.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +# ProtectedBranchesFinder +# +# Used to filter protected branches by set of params +# +# Arguments: +# project - which project to scope to +# params: +# search: string +class ProtectedBranchesFinder + LIMIT = 100 + + attr_accessor :project, :params + + def initialize(project, params = {}) + @project = project + @params = params + end + + def execute + protected_branches = project.limited_protected_branches(LIMIT) + protected_branches = by_name(protected_branches) + + protected_branches + end + + private + + def by_name(protected_branches) + return protected_branches unless params[:search].present? + + protected_branches.by_name(params[:search]) + end +end |