debian-mirror-gitlab/app/finders/environments_finder.rb

123 lines
3.1 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
class EnvironmentsFinder
attr_reader :project, :current_user, :params
2020-04-22 19:07:51 +05:30
InvalidStatesError = Class.new(StandardError)
2017-08-17 22:00:37 +05:30
def initialize(project, current_user, params = {})
@project, @current_user, @params = project, current_user, params
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
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
2020-03-13 15:44:24 +05:30
.where(id: environment_ids)
2017-08-17 22:00:37 +05:30
2020-03-13 15:44:24 +05:30
if params[:find_latest]
find_one(environments.order_by_last_deployed_at_desc)
else
find_all(environments.order_by_last_deployed_at.to_a)
2017-08-17 22:00:37 +05:30
end
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
2019-09-30 21:07:59 +05:30
# 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-12-04 20:38:33 +05:30
# See more https://gitlab.com/gitlab-org/gitlab-foss/issues/63381
2019-09-30 21:07:59 +05:30
def find
environments = project.environments
environments = by_name(environments)
environments = by_search(environments)
2020-04-22 19:07:51 +05:30
# Raises InvalidStatesError if params[:states] contains invalid states.
environments = by_states(environments)
2019-09-30 21:07:59 +05:30
environments
end
2017-08-17 22:00:37 +05:30
private
2020-03-13 15:44:24 +05:30
def find_one(environments)
[environments.find { |environment| valid_environment?(environment) }].compact
end
def find_all(environments)
environments.select { |environment| valid_environment?(environment) }
end
def valid_environment?(environment)
# Go in order of cost: SQL calls are cheaper than Gitaly calls
return false unless Ability.allowed?(current_user, :read_environment, environment)
return false if ref && params[:recently_updated] && !environment.recently_updated_on_branch?(ref)
return false if ref && commit && !environment.includes_commit?(commit)
true
end
2017-08-17 22:00:37 +05:30
def ref
params[:ref].try(:to_s)
end
def commit
params[:commit]
end
2019-09-30 21:07:59 +05:30
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
2020-04-22 19:07:51 +05:30
def by_states(environments)
if params[:states].present?
environments_with_states(environments)
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
2017-08-17 22:00:37 +05:30
end