Welcome to mirror list, hosted at ThFree Co, Russian Federation.

filter_authorizations_service.rb « agents « clusters « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 68517ceec04e8aaf65d087abc2483990dcc2ed33 (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
# frozen_string_literal: true

module Clusters
  module Agents
    class FilterAuthorizationsService
      def initialize(authorizations, filter_params)
        @authorizations = authorizations
        @filter_params = filter_params

        @environments_matcher = {}
      end

      def execute
        filter_by_environment(authorizations)
      end

      private

      attr_reader :authorizations, :filter_params

      def filter_by_environment(auths)
        return auths unless filter_by_environment?

        auths.select do |auth|
          next true if auth.config['environments'].blank?

          auth.config['environments'].any? { |environment_pattern| matches_environment?(environment_pattern) }
        end
      end

      def filter_by_environment?
        filter_params.has_key?(:environment)
      end

      def environment_filter
        @environment_filter ||= filter_params[:environment]
      end

      def matches_environment?(environment_pattern)
        return false if environment_filter.nil?

        environments_matcher(environment_pattern).match?(environment_filter)
      end

      def environments_matcher(environment_pattern)
        @environments_matcher[environment_pattern] ||= ::Gitlab::Ci::EnvironmentMatcher.new(environment_pattern)
      end
    end
  end
end