summaryrefslogtreecommitdiff
path: root/app/finders/ci/pipelines_for_merge_request_finder.rb
blob: da8dfc2579aab8e2e32d9f572793b9434a8de60c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# frozen_string_literal: true

module Ci
  # A state object to centralize logic related to merge request pipelines
  class PipelinesForMergeRequestFinder
    include Gitlab::Utils::StrongMemoize

    def initialize(merge_request, current_user)
      @merge_request = merge_request
      @current_user = current_user
    end

    attr_reader :merge_request, :current_user

    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

        pipelines =
          if merge_request.persisted?
            if Feature.enabled?(:ci_pipelines_for_merge_request_finder_new_cte, target_project)
              pipelines_using_cte
            else
              pipelines_using_legacy_cte
            end
          else
            triggered_for_branch.for_sha(commit_shas)
          end

        sort(pipelines)
      end
    end

    private

    def pipelines_using_legacy_cte
      cte = Gitlab::SQL::CTE.new(:shas, merge_request.all_commits.select(:sha))

      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([merged_result_pipelines, detached_merge_request_pipelines, pipelines_for_branch])
    end

    def pipelines_using_cte
      cte = Gitlab::SQL::CTE.new(:shas, merge_request.all_commits.select(:sha))

      pipelines_for_merge_requests = triggered_by_merge_request
      pipelines_for_branch = filter_by_sha(triggered_for_branch, cte)

      Ci::Pipeline.with(cte.to_arel) # rubocop: disable CodeReuse/ActiveRecord
        .from_union([pipelines_for_merge_requests, pipelines_for_branch])
    end

    def filter_by_sha(pipelines, cte)
      hex = Arel::Nodes::SqlLiteral.new("'hex'")
      string_sha = Arel::Nodes::NamedFunction.new('encode', [cte.table[:sha], hex])
      join_condition = string_sha.eq(Ci::Pipeline.arel_table[:sha])

      filter_by(pipelines, cte, join_condition)
    end

    def filter_by(pipelines, cte, join_condition)
      shas_table =
        Ci::Pipeline.arel_table
          .join(cte.table, Arel::Nodes::InnerJoin)
          .on(join_condition)
          .join_sources

      pipelines.joins(shas_table) # rubocop: disable CodeReuse/ActiveRecord
    end

    # NOTE: this method returns only parent merge request pipelines.
    # Child merge request pipelines have a different source.
    def triggered_by_merge_request
      Ci::Pipeline.triggered_by_merge_request(merge_request)
    end

    def triggered_for_branch
      source_project.all_pipelines.ci_branch_sources.for_branch(source_branch)
    end

    def sort(pipelines)
      sql = 'CASE ci_pipelines.source WHEN (?) THEN 0 ELSE 1 END, ci_pipelines.id DESC'
      query = ApplicationRecord.send(:sanitize_sql_array, [sql, Ci::Pipeline.sources[:merge_request_event]]) # rubocop:disable GitlabSecurity/PublicSend

      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