summaryrefslogtreecommitdiff
path: root/app/models/ci/job_token/allowlist.rb
blob: 9e9a0a68ebd438671464c6945bc09b7af8a76845 (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
# frozen_string_literal: true
module Ci
  module JobToken
    class Allowlist
      def initialize(source_project, direction:)
        @source_project = source_project
        @direction = direction
      end

      def includes?(target_project)
        source_links
          .with_target(target_project)
          .exists?
      end

      def projects
        Project.from_union(target_projects, remove_duplicates: false)
      end

      private

      def source_links
        Ci::JobToken::ProjectScopeLink
          .with_source(@source_project)
          .where(direction: @direction)
      end

      def target_project_ids
        source_links
          # pluck needed to avoid ci and main db join
          .pluck(:target_project_id)
      end

      def target_projects
        [
          Project.id_in(@source_project),
          Project.id_in(target_project_ids)
        ]
      end
    end
  end
end