2018-09-11 15:08:34 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-02-06 19:06:46 -05:00
|
|
|
class EnvironmentsFinder
|
|
|
|
attr_reader :project, :current_user, :params
|
|
|
|
|
|
|
|
def initialize(project, current_user, params = {})
|
|
|
|
@project, @current_user, @params = project, current_user, params
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-02-07 12:26:59 -05:00
|
|
|
def execute
|
2017-02-06 19:06:46 -05:00
|
|
|
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).order_by_last_deployed_at.to_a
|
|
|
|
|
2017-02-07 12:43:36 -05:00
|
|
|
environments.select! do |environment|
|
|
|
|
Ability.allowed?(current_user, :read_environment, environment)
|
|
|
|
end
|
|
|
|
|
2017-02-06 19:06:46 -05:00
|
|
|
if ref && commit
|
|
|
|
environments.select! do |environment|
|
|
|
|
environment.includes_commit?(commit)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if ref && params[:recently_updated]
|
|
|
|
environments.select! do |environment|
|
|
|
|
environment.recently_updated_on_branch?(ref)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-07 12:43:36 -05:00
|
|
|
environments
|
2017-02-06 19:06:46 -05:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-02-06 19:06:46 -05:00
|
|
|
|
2019-06-24 08:18:40 -04:00
|
|
|
# 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.
|
2019-09-18 10:02:45 -04:00
|
|
|
# See more https://gitlab.com/gitlab-org/gitlab-foss/issues/63381
|
2019-06-24 08:18:40 -04:00
|
|
|
def find
|
|
|
|
environments = project.environments
|
|
|
|
environments = by_name(environments)
|
|
|
|
environments = by_search(environments)
|
|
|
|
|
|
|
|
environments
|
|
|
|
end
|
|
|
|
|
2017-02-06 19:06:46 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def ref
|
|
|
|
params[:ref].try(:to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
def commit
|
|
|
|
params[:commit]
|
|
|
|
end
|
2019-06-24 08:18:40 -04:00
|
|
|
|
|
|
|
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
|
2017-02-06 19:06:46 -05:00
|
|
|
end
|