Class: EnvironmentsFinder

Inherits:
Object
  • Object
show all
Defined in:
app/finders/environments_finder.rb

Constant Summary collapse

InvalidStatesError =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, current_user, params = {}) ⇒ EnvironmentsFinder

Returns a new instance of EnvironmentsFinder.


8
9
10
# File 'app/finders/environments_finder.rb', line 8

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

Instance Attribute Details

#current_userObject (readonly)

Returns the value of attribute current_user


4
5
6
# File 'app/finders/environments_finder.rb', line 4

def current_user
  @current_user
end

#paramsObject (readonly)

Returns the value of attribute params


4
5
6
# File 'app/finders/environments_finder.rb', line 4

def params
  @params
end

#projectObject (readonly)

Returns the value of attribute project


4
5
6
# File 'app/finders/environments_finder.rb', line 4

def project
  @project
end

Instance Method Details

#executeObject

rubocop: disable CodeReuse/ActiveRecord


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
# File 'app/finders/environments_finder.rb', line 13

def execute
  deployments = project.deployments
  deployments =
    if ref
      deployments_query = params[:with_tags] ? 'ref = :ref OR tag IS TRUE' : 'ref = :ref'
      deployments.where(deployments_query, ref: ref.to_s)
    elsif commit
      deployments.where(sha: commit.sha)
    else
      deployments.none
    end

  environment_ids = deployments
    .group(:environment_id)
    .select(:environment_id)

  environments = project.environments.available
    .where(id: environment_ids)

  if params[:find_latest]
    find_one(environments.order_by_last_deployed_at_desc)
  else
    find_all(environments.order_by_last_deployed_at.to_a)
  end
end

#findObject

This method will eventually take the place of `#execute` as an efficient way to get relevant environment entries. Currently, `#execute` method has a serious technical debt and we will likely rework on it in the future. See more gitlab.com/gitlab-org/gitlab-foss/issues/63381


45
46
47
48
49
50
51
52
53
54
# File 'app/finders/environments_finder.rb', line 45

def find
  environments = project.environments
  environments = by_name(environments)
  environments = by_search(environments)

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

  environments
end