summaryrefslogtreecommitdiff
path: root/lib/gitlab/cross_project_access/check_collection.rb
blob: 55527ba5e87ced5fd93cb4438d5cca6fe9f526ef (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
# frozen_string_literal: true

module Gitlab
  class CrossProjectAccess
    class CheckCollection
      attr_reader :checks

      def initialize
        @checks = []
      end

      def add_collection(collection)
        @checks |= collection.checks
      end

      def add_check(check)
        @checks << check
      end

      def should_run?(object)
        skips, runs = arranged_checks

        # If one rule tells us to skip, we skip the cross project check
        return false if skips.any? { |check| check.should_skip?(object) }

        # If the rule isn't skipped, we run it if any of the checks says we
        # should run
        runs.any? { |check| check.should_run?(object) }
      end

      def arranged_checks
        return [@skips, @runs] if @skips && @runs

        @skips = []
        @runs = []

        @checks.each do |check|
          if check.skip
            @skips << check
          else
            @runs << check
          end
        end

        [@skips, @runs]
      end
    end
  end
end