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

environments_finder.rb « environments « finders « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 46c49f096c6d1bc534b090e11a523de723a2162c (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
# frozen_string_literal: true

module Environments
  class EnvironmentsFinder
    attr_reader :project, :current_user, :params

    InvalidStatesError = Class.new(StandardError)

    def initialize(project, current_user, params = {})
      @project = project
      @current_user = current_user
      @params = params
    end

    def execute
      environments = project.environments
      environments = by_name(environments)
      environments = by_search(environments)
      environments = by_ids(environments)

      # Raises InvalidStatesError if params[:states] contains invalid states.
      by_states(environments)
    end

    private

    def by_name(environments)
      if params[:name].present?
        environments.for_name(params[:name])
      else
        environments
      end
    end

    def by_search(environments)
      if params[:search].present?
        environments.for_name_like(params[:search], limit: nil)
      else
        environments
      end
    end

    def by_states(environments)
      if params[:states].present?
        environments_with_states(environments)
      else
        environments
      end
    end

    def by_ids(environments)
      if params[:environment_ids].present?
        environments.for_id(params[:environment_ids])
      else
        environments
      end
    end

    def environments_with_states(environments)
      # Convert to array of strings
      states = Array(params[:states]).map(&:to_s)

      raise InvalidStatesError, _('Requested states are invalid') unless valid_states?(states)

      environments.with_states(states)
    end

    def valid_states?(states)
      valid_states = Environment.valid_states.map(&:to_s)

      (states - valid_states).empty?
    end
  end
end