diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-14 00:09:30 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-14 00:09:30 +0000 |
commit | 8957ace3159e5369a700a77614493ed6a8a98f93 (patch) | |
tree | 98ff5be0caa30cfebb4e0cd0ae2ceaf21ce92eb4 /app/finders | |
parent | 232e0a31f1e5d5b3a788dfc3dba8f8d41df36bf9 (diff) | |
download | gitlab-ce-8957ace3159e5369a700a77614493ed6a8a98f93.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/finders')
-rw-r--r-- | app/finders/ci/jobs_finder.rb | 53 | ||||
-rw-r--r-- | app/finders/ci/pipeline_schedules_finder.rb | 28 | ||||
-rw-r--r-- | app/finders/ci/pipelines_finder.rb | 158 | ||||
-rw-r--r-- | app/finders/ci/runner_jobs_finder.rb | 45 | ||||
-rw-r--r-- | app/finders/jobs_finder.rb | 51 | ||||
-rw-r--r-- | app/finders/pipeline_schedules_finder.rb | 26 | ||||
-rw-r--r-- | app/finders/pipelines_finder.rb | 156 | ||||
-rw-r--r-- | app/finders/runner_jobs_finder.rb | 43 |
8 files changed, 284 insertions, 276 deletions
diff --git a/app/finders/ci/jobs_finder.rb b/app/finders/ci/jobs_finder.rb new file mode 100644 index 00000000000..2169bf8c53e --- /dev/null +++ b/app/finders/ci/jobs_finder.rb @@ -0,0 +1,53 @@ +# frozen_string_literal: true + +module Ci + class JobsFinder + include Gitlab::Allowable + + def initialize(current_user:, project: nil, params: {}) + @current_user = current_user + @project = project + @params = params + end + + def execute + builds = init_collection.order_id_desc + filter_by_scope(builds) + rescue Gitlab::Access::AccessDeniedError + Ci::Build.none + end + + private + + attr_reader :current_user, :project, :params + + def init_collection + project ? project_builds : all_builds + end + + def all_builds + raise Gitlab::Access::AccessDeniedError unless current_user&.admin? + + Ci::Build.all + end + + def project_builds + raise Gitlab::Access::AccessDeniedError unless can?(current_user, :read_build, project) + + project.builds.relevant + end + + def filter_by_scope(builds) + case params[:scope] + when 'pending' + builds.pending.reverse_order + when 'running' + builds.running.reverse_order + when 'finished' + builds.finished + else + builds + end + end + end +end diff --git a/app/finders/ci/pipeline_schedules_finder.rb b/app/finders/ci/pipeline_schedules_finder.rb new file mode 100644 index 00000000000..2544c8c3254 --- /dev/null +++ b/app/finders/ci/pipeline_schedules_finder.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +module Ci + class PipelineSchedulesFinder + attr_reader :project, :pipeline_schedules + + def initialize(project) + @project = project + @pipeline_schedules = project.pipeline_schedules + end + + # rubocop: disable CodeReuse/ActiveRecord + def execute(scope: nil) + scoped_schedules = + case scope + when 'active' + pipeline_schedules.active + when 'inactive' + pipeline_schedules.inactive + else + pipeline_schedules + end + + scoped_schedules.order(id: :desc) + end + # rubocop: enable CodeReuse/ActiveRecord + end +end diff --git a/app/finders/ci/pipelines_finder.rb b/app/finders/ci/pipelines_finder.rb new file mode 100644 index 00000000000..9e71e92b456 --- /dev/null +++ b/app/finders/ci/pipelines_finder.rb @@ -0,0 +1,158 @@ +# frozen_string_literal: true + +module Ci + class PipelinesFinder + attr_reader :project, :pipelines, :params, :current_user + + ALLOWED_INDEXED_COLUMNS = %w[id status ref updated_at user_id].freeze + + def initialize(project, current_user, params = {}) + @project = project + @current_user = current_user + @pipelines = project.all_pipelines + @params = params + end + + def execute + unless Ability.allowed?(current_user, :read_pipeline, project) + return Ci::Pipeline.none + end + + items = pipelines.no_child + items = by_scope(items) + items = by_status(items) + items = by_ref(items) + items = by_sha(items) + items = by_name(items) + items = by_username(items) + items = by_yaml_errors(items) + items = by_updated_at(items) + sort_items(items) + end + + private + + # rubocop: disable CodeReuse/ActiveRecord + def ids_for_ref(refs) + pipelines.where(ref: refs).group(:ref).select('max(id)') + end + # rubocop: enable CodeReuse/ActiveRecord + + # rubocop: disable CodeReuse/ActiveRecord + def from_ids(ids) + pipelines.unscoped.where(project_id: project.id, id: ids) + end + # rubocop: enable CodeReuse/ActiveRecord + + def branches + project.repository.branch_names + end + + def tags + project.repository.tag_names + end + + def by_scope(items) + case params[:scope] + when 'running' + items.running + when 'pending' + items.pending + when 'finished' + items.finished + when 'branches' + from_ids(ids_for_ref(branches)) + when 'tags' + from_ids(ids_for_ref(tags)) + else + items + end + end + + # rubocop: disable CodeReuse/ActiveRecord + def by_status(items) + return items unless HasStatus::AVAILABLE_STATUSES.include?(params[:status]) + + items.where(status: params[:status]) + end + # rubocop: enable CodeReuse/ActiveRecord + + # rubocop: disable CodeReuse/ActiveRecord + def by_ref(items) + if params[:ref].present? + items.where(ref: params[:ref]) + else + items + end + end + # rubocop: enable CodeReuse/ActiveRecord + + # rubocop: disable CodeReuse/ActiveRecord + def by_sha(items) + if params[:sha].present? + items.where(sha: params[:sha]) + else + items + end + end + # rubocop: enable CodeReuse/ActiveRecord + + # rubocop: disable CodeReuse/ActiveRecord + def by_name(items) + if params[:name].present? + items.joins(:user).where(users: { name: params[:name] }) + else + items + end + end + # rubocop: enable CodeReuse/ActiveRecord + + # rubocop: disable CodeReuse/ActiveRecord + def by_username(items) + if params[:username].present? + items.joins(:user).where(users: { username: params[:username] }) + else + items + end + end + # rubocop: enable CodeReuse/ActiveRecord + + # rubocop: disable CodeReuse/ActiveRecord + def by_yaml_errors(items) + case Gitlab::Utils.to_boolean(params[:yaml_errors]) + when true + items.where("yaml_errors IS NOT NULL") + when false + items.where("yaml_errors IS NULL") + else + items + end + end + # rubocop: enable CodeReuse/ActiveRecord + + def by_updated_at(items) + items = items.updated_before(params[:updated_before]) if params[:updated_before].present? + items = items.updated_after(params[:updated_after]) if params[:updated_after].present? + + items + end + + # rubocop: disable CodeReuse/ActiveRecord + def sort_items(items) + order_by = if ALLOWED_INDEXED_COLUMNS.include?(params[:order_by]) + params[:order_by] + else + :id + end + + sort = if params[:sort] =~ /\A(ASC|DESC)\z/i + params[:sort] + else + :desc + end + + items.order(order_by => sort) + end + # rubocop: enable CodeReuse/ActiveRecord + end +end diff --git a/app/finders/ci/runner_jobs_finder.rb b/app/finders/ci/runner_jobs_finder.rb new file mode 100644 index 00000000000..ffcdb407e7e --- /dev/null +++ b/app/finders/ci/runner_jobs_finder.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +module Ci + class RunnerJobsFinder + attr_reader :runner, :params + + ALLOWED_INDEXED_COLUMNS = %w[id].freeze + + def initialize(runner, params = {}) + @runner = runner + @params = params + end + + def execute + items = @runner.builds + items = by_status(items) + sort_items(items) + end + + private + + # rubocop: disable CodeReuse/ActiveRecord + def by_status(items) + return items unless HasStatus::AVAILABLE_STATUSES.include?(params[:status]) + + items.where(status: params[:status]) + end + # rubocop: enable CodeReuse/ActiveRecord + + # rubocop: disable CodeReuse/ActiveRecord + def sort_items(items) + return items unless ALLOWED_INDEXED_COLUMNS.include?(params[:order_by]) + + order_by = params[:order_by] + sort = if /\A(ASC|DESC)\z/i.match?(params[:sort]) + params[:sort] + else + :desc + end + + items.order(order_by => sort) + end + # rubocop: enable CodeReuse/ActiveRecord + end +end diff --git a/app/finders/jobs_finder.rb b/app/finders/jobs_finder.rb deleted file mode 100644 index bac18e69618..00000000000 --- a/app/finders/jobs_finder.rb +++ /dev/null @@ -1,51 +0,0 @@ -# frozen_string_literal: true - -class JobsFinder - include Gitlab::Allowable - - def initialize(current_user:, project: nil, params: {}) - @current_user = current_user - @project = project - @params = params - end - - def execute - builds = init_collection.order_id_desc - filter_by_scope(builds) - rescue Gitlab::Access::AccessDeniedError - Ci::Build.none - end - - private - - attr_reader :current_user, :project, :params - - def init_collection - project ? project_builds : all_builds - end - - def all_builds - raise Gitlab::Access::AccessDeniedError unless current_user&.admin? - - Ci::Build.all - end - - def project_builds - raise Gitlab::Access::AccessDeniedError unless can?(current_user, :read_build, project) - - project.builds.relevant - end - - def filter_by_scope(builds) - case params[:scope] - when 'pending' - builds.pending.reverse_order - when 'running' - builds.running.reverse_order - when 'finished' - builds.finished - else - builds - end - end -end diff --git a/app/finders/pipeline_schedules_finder.rb b/app/finders/pipeline_schedules_finder.rb deleted file mode 100644 index 3beee608268..00000000000 --- a/app/finders/pipeline_schedules_finder.rb +++ /dev/null @@ -1,26 +0,0 @@ -# frozen_string_literal: true - -class PipelineSchedulesFinder - attr_reader :project, :pipeline_schedules - - def initialize(project) - @project = project - @pipeline_schedules = project.pipeline_schedules - end - - # rubocop: disable CodeReuse/ActiveRecord - def execute(scope: nil) - scoped_schedules = - case scope - when 'active' - pipeline_schedules.active - when 'inactive' - pipeline_schedules.inactive - else - pipeline_schedules - end - - scoped_schedules.order(id: :desc) - end - # rubocop: enable CodeReuse/ActiveRecord -end diff --git a/app/finders/pipelines_finder.rb b/app/finders/pipelines_finder.rb deleted file mode 100644 index 0599daab564..00000000000 --- a/app/finders/pipelines_finder.rb +++ /dev/null @@ -1,156 +0,0 @@ -# frozen_string_literal: true - -class PipelinesFinder - attr_reader :project, :pipelines, :params, :current_user - - ALLOWED_INDEXED_COLUMNS = %w[id status ref updated_at user_id].freeze - - def initialize(project, current_user, params = {}) - @project = project - @current_user = current_user - @pipelines = project.all_pipelines - @params = params - end - - def execute - unless Ability.allowed?(current_user, :read_pipeline, project) - return Ci::Pipeline.none - end - - items = pipelines.no_child - items = by_scope(items) - items = by_status(items) - items = by_ref(items) - items = by_sha(items) - items = by_name(items) - items = by_username(items) - items = by_yaml_errors(items) - items = by_updated_at(items) - sort_items(items) - end - - private - - # rubocop: disable CodeReuse/ActiveRecord - def ids_for_ref(refs) - pipelines.where(ref: refs).group(:ref).select('max(id)') - end - # rubocop: enable CodeReuse/ActiveRecord - - # rubocop: disable CodeReuse/ActiveRecord - def from_ids(ids) - pipelines.unscoped.where(project_id: project.id, id: ids) - end - # rubocop: enable CodeReuse/ActiveRecord - - def branches - project.repository.branch_names - end - - def tags - project.repository.tag_names - end - - def by_scope(items) - case params[:scope] - when 'running' - items.running - when 'pending' - items.pending - when 'finished' - items.finished - when 'branches' - from_ids(ids_for_ref(branches)) - when 'tags' - from_ids(ids_for_ref(tags)) - else - items - end - end - - # rubocop: disable CodeReuse/ActiveRecord - def by_status(items) - return items unless HasStatus::AVAILABLE_STATUSES.include?(params[:status]) - - items.where(status: params[:status]) - end - # rubocop: enable CodeReuse/ActiveRecord - - # rubocop: disable CodeReuse/ActiveRecord - def by_ref(items) - if params[:ref].present? - items.where(ref: params[:ref]) - else - items - end - end - # rubocop: enable CodeReuse/ActiveRecord - - # rubocop: disable CodeReuse/ActiveRecord - def by_sha(items) - if params[:sha].present? - items.where(sha: params[:sha]) - else - items - end - end - # rubocop: enable CodeReuse/ActiveRecord - - # rubocop: disable CodeReuse/ActiveRecord - def by_name(items) - if params[:name].present? - items.joins(:user).where(users: { name: params[:name] }) - else - items - end - end - # rubocop: enable CodeReuse/ActiveRecord - - # rubocop: disable CodeReuse/ActiveRecord - def by_username(items) - if params[:username].present? - items.joins(:user).where(users: { username: params[:username] }) - else - items - end - end - # rubocop: enable CodeReuse/ActiveRecord - - # rubocop: disable CodeReuse/ActiveRecord - def by_yaml_errors(items) - case Gitlab::Utils.to_boolean(params[:yaml_errors]) - when true - items.where("yaml_errors IS NOT NULL") - when false - items.where("yaml_errors IS NULL") - else - items - end - end - # rubocop: enable CodeReuse/ActiveRecord - - def by_updated_at(items) - items = items.updated_before(params[:updated_before]) if params[:updated_before].present? - items = items.updated_after(params[:updated_after]) if params[:updated_after].present? - - items - end - - # rubocop: disable CodeReuse/ActiveRecord - def sort_items(items) - order_by = if ALLOWED_INDEXED_COLUMNS.include?(params[:order_by]) - params[:order_by] - else - :id - end - - sort = if params[:sort] =~ /\A(ASC|DESC)\z/i - params[:sort] - else - :desc - end - - items.order(order_by => sort) - end - # rubocop: enable CodeReuse/ActiveRecord -end diff --git a/app/finders/runner_jobs_finder.rb b/app/finders/runner_jobs_finder.rb deleted file mode 100644 index ef90817416a..00000000000 --- a/app/finders/runner_jobs_finder.rb +++ /dev/null @@ -1,43 +0,0 @@ -# frozen_string_literal: true - -class RunnerJobsFinder - attr_reader :runner, :params - - ALLOWED_INDEXED_COLUMNS = %w[id].freeze - - def initialize(runner, params = {}) - @runner = runner - @params = params - end - - def execute - items = @runner.builds - items = by_status(items) - sort_items(items) - end - - private - - # rubocop: disable CodeReuse/ActiveRecord - def by_status(items) - return items unless HasStatus::AVAILABLE_STATUSES.include?(params[:status]) - - items.where(status: params[:status]) - end - # rubocop: enable CodeReuse/ActiveRecord - - # rubocop: disable CodeReuse/ActiveRecord - def sort_items(items) - return items unless ALLOWED_INDEXED_COLUMNS.include?(params[:order_by]) - - order_by = params[:order_by] - sort = if /\A(ASC|DESC)\z/i.match?(params[:sort]) - params[:sort] - else - :desc - end - - items.order(order_by => sort) - end - # rubocop: enable CodeReuse/ActiveRecord -end |