diff options
Diffstat (limited to 'app/finders/ci')
-rw-r--r-- | app/finders/ci/pipelines_finder.rb | 2 | ||||
-rw-r--r-- | app/finders/ci/pipelines_for_merge_request_finder.rb | 44 | ||||
-rw-r--r-- | app/finders/ci/runner_jobs_finder.rb | 2 | ||||
-rw-r--r-- | app/finders/ci/variables_finder.rb | 31 |
4 files changed, 68 insertions, 11 deletions
diff --git a/app/finders/ci/pipelines_finder.rb b/app/finders/ci/pipelines_finder.rb index 9e71e92b456..7347a83d294 100644 --- a/app/finders/ci/pipelines_finder.rb +++ b/app/finders/ci/pipelines_finder.rb @@ -71,7 +71,7 @@ module Ci # rubocop: disable CodeReuse/ActiveRecord def by_status(items) - return items unless HasStatus::AVAILABLE_STATUSES.include?(params[:status]) + return items unless Ci::HasStatus::AVAILABLE_STATUSES.include?(params[:status]) items.where(status: params[:status]) end diff --git a/app/finders/ci/pipelines_for_merge_request_finder.rb b/app/finders/ci/pipelines_for_merge_request_finder.rb index c01a68d6749..93d139652b9 100644 --- a/app/finders/ci/pipelines_for_merge_request_finder.rb +++ b/app/finders/ci/pipelines_for_merge_request_finder.rb @@ -7,14 +7,29 @@ module Ci EVENT = 'merge_request_event' - def initialize(merge_request) + def initialize(merge_request, current_user) @merge_request = merge_request + @current_user = current_user end - attr_reader :merge_request + attr_reader :merge_request, :current_user - delegate :commit_shas, :source_project, :source_branch, to: :merge_request + delegate :commit_shas, :target_project, :source_project, :source_branch, to: :merge_request + # Fetch all pipelines that the user can read. + def execute + if can_read_pipeline_in_target_project? && can_read_pipeline_in_source_project? + all + elsif can_read_pipeline_in_source_project? + all.for_project(merge_request.source_project) + elsif can_read_pipeline_in_target_project? + all.for_project(merge_request.target_project) + else + Ci::Pipeline.none + end + end + + # Fetch all pipelines without permission check. def all strong_memoize(:all_pipelines) do next Ci::Pipeline.none unless source_project @@ -35,13 +50,13 @@ module Ci def pipelines_using_cte cte = Gitlab::SQL::CTE.new(:shas, merge_request.all_commits.select(:sha)) - source_pipelines_join = cte.table[:sha].eq(Ci::Pipeline.arel_table[:source_sha]) - source_pipelines = filter_by(triggered_by_merge_request, cte, source_pipelines_join) - detached_pipelines = filter_by_sha(triggered_by_merge_request, cte) + source_sha_join = cte.table[:sha].eq(Ci::Pipeline.arel_table[:source_sha]) + merged_result_pipelines = filter_by(triggered_by_merge_request, cte, source_sha_join) + detached_merge_request_pipelines = filter_by_sha(triggered_by_merge_request, cte) pipelines_for_branch = filter_by_sha(triggered_for_branch, cte) Ci::Pipeline.with(cte.to_arel) # rubocop: disable CodeReuse/ActiveRecord - .from_union([source_pipelines, detached_pipelines, pipelines_for_branch]) + .from_union([merged_result_pipelines, detached_merge_request_pipelines, pipelines_for_branch]) end def filter_by_sha(pipelines, cte) @@ -65,8 +80,7 @@ module Ci # NOTE: this method returns only parent merge request pipelines. # Child merge request pipelines have a different source. def triggered_by_merge_request - source_project.ci_pipelines - .where(source: :merge_request_event, merge_request: merge_request) # rubocop: disable CodeReuse/ActiveRecord + Ci::Pipeline.triggered_by_merge_request(merge_request) end def triggered_for_branch @@ -86,5 +100,17 @@ module Ci pipelines.order(Arel.sql(query)) # rubocop: disable CodeReuse/ActiveRecord end + + def can_read_pipeline_in_target_project? + strong_memoize(:can_read_pipeline_in_target_project) do + Ability.allowed?(current_user, :read_pipeline, target_project) + end + end + + def can_read_pipeline_in_source_project? + strong_memoize(:can_read_pipeline_in_source_project) do + Ability.allowed?(current_user, :read_pipeline, source_project) + end + end end end diff --git a/app/finders/ci/runner_jobs_finder.rb b/app/finders/ci/runner_jobs_finder.rb index ffcdb407e7e..9dc3c2a2427 100644 --- a/app/finders/ci/runner_jobs_finder.rb +++ b/app/finders/ci/runner_jobs_finder.rb @@ -21,7 +21,7 @@ module Ci # rubocop: disable CodeReuse/ActiveRecord def by_status(items) - return items unless HasStatus::AVAILABLE_STATUSES.include?(params[:status]) + return items unless Ci::HasStatus::AVAILABLE_STATUSES.include?(params[:status]) items.where(status: params[:status]) end diff --git a/app/finders/ci/variables_finder.rb b/app/finders/ci/variables_finder.rb new file mode 100644 index 00000000000..d933643ffb2 --- /dev/null +++ b/app/finders/ci/variables_finder.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +module Ci + class VariablesFinder + attr_reader :project, :params + + def initialize(project, params) + @project, @params = project, params + + raise ArgumentError, 'Please provide params[:key]' if params[:key].blank? + end + + def execute + variables = project.variables + variables = by_key(variables) + variables = by_environment_scope(variables) + variables + end + + private + + def by_key(variables) + variables.by_key(params[:key]) + end + + def by_environment_scope(variables) + environment_scope = params.dig(:filter, :environment_scope) + environment_scope.present? ? variables.by_environment_scope(environment_scope) : variables + end + end +end |