debian-mirror-gitlab/app/finders/environments/environments_by_deployments_finder.rb

64 lines
1.6 KiB
Ruby
Raw Normal View History

2021-06-08 01:23:25 +05:30
# frozen_string_literal: true
module Environments
class EnvironmentsByDeploymentsFinder
attr_reader :project, :current_user, :params
def initialize(project, current_user, params = {})
@project = project
@current_user = current_user
@params = params
end
# rubocop: disable CodeReuse/ActiveRecord
def execute
deployments =
if ref
2022-03-02 08:16:31 +05:30
Deployment.where(ref: ref.to_s)
2022-04-04 11:22:00 +05:30
elsif sha
Deployment.where(sha: sha)
2021-06-08 01:23:25 +05:30
else
2022-01-26 12:08:38 +05:30
Deployment.none
2021-06-08 01:23:25 +05:30
end
2022-01-26 12:08:38 +05:30
environments = project.environments.available
.where('EXISTS (?)', deployments.where('environment_id = environments.id'))
2021-06-08 01:23:25 +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)
end
end
# rubocop: enable CodeReuse/ActiveRecord
private
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)
2022-04-04 11:22:00 +05:30
return false if ref && sha && !environment.includes_commit?(sha)
2021-06-08 01:23:25 +05:30
true
end
def ref
params[:ref].try(:to_s)
end
2022-04-04 11:22:00 +05:30
def sha
params[:sha] || params[:commit]&.id
2021-06-08 01:23:25 +05:30
end
end
end