2018-10-09 12:17:40 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class EnvironmentStatus
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2019-07-01 04:27:58 -04:00
|
|
|
attr_reader :project, :environment, :merge_request, :sha
|
2018-10-09 12:17:40 -04:00
|
|
|
|
|
|
|
delegate :id, to: :environment
|
|
|
|
delegate :name, to: :environment
|
2018-11-28 10:15:51 -05:00
|
|
|
delegate :status, to: :deployment, allow_nil: true
|
2018-11-06 06:47:23 -05:00
|
|
|
delegate :deployed_at, to: :deployment, allow_nil: true
|
2018-10-09 12:17:40 -04:00
|
|
|
|
2018-10-29 07:34:41 -04:00
|
|
|
def self.for_merge_request(mr, user)
|
2018-12-03 01:46:45 -05:00
|
|
|
build_environments_status(mr, user, mr.actual_head_pipeline)
|
2018-10-29 07:34:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.after_merge_request(mr, user)
|
|
|
|
return [] unless mr.merged?
|
|
|
|
|
2018-12-03 01:46:45 -05:00
|
|
|
build_environments_status(mr, user, mr.merge_pipeline)
|
2018-10-29 07:34:41 -04:00
|
|
|
end
|
|
|
|
|
2019-11-29 10:06:43 -05:00
|
|
|
def self.for_deployed_merge_request(mr, user)
|
|
|
|
statuses = []
|
|
|
|
|
|
|
|
mr.recent_visible_deployments.each do |deploy|
|
|
|
|
env = deploy.environment
|
|
|
|
|
|
|
|
next unless Ability.allowed?(user, :read_environment, env)
|
|
|
|
|
|
|
|
statuses <<
|
|
|
|
EnvironmentStatus.new(deploy.project, env, mr, deploy.sha)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Existing projects that used deployments prior to the introduction of
|
|
|
|
# explicitly linked merge requests won't have any data using this new
|
|
|
|
# approach, so we fall back to retrieving deployments based on CI pipelines.
|
|
|
|
if statuses.any?
|
|
|
|
statuses
|
|
|
|
else
|
|
|
|
after_merge_request(mr, user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-01 04:27:58 -04:00
|
|
|
def initialize(project, environment, merge_request, sha)
|
|
|
|
@project = project
|
2018-10-09 12:17:40 -04:00
|
|
|
@environment = environment
|
|
|
|
@merge_request = merge_request
|
2018-10-29 07:34:41 -04:00
|
|
|
@sha = sha
|
2018-10-09 12:17:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def deployment
|
|
|
|
strong_memoize(:deployment) do
|
2018-11-07 00:29:16 -05:00
|
|
|
Deployment.where(environment: environment).find_by_sha(sha)
|
2018-10-09 12:17:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-01 05:40:59 -04:00
|
|
|
def has_metrics?
|
2019-07-07 17:19:21 -04:00
|
|
|
strong_memoize(:has_metrics) do
|
|
|
|
deployment_metrics.has_metrics?
|
|
|
|
end
|
2019-07-01 05:40:59 -04:00
|
|
|
end
|
|
|
|
|
2018-10-18 10:17:24 -04:00
|
|
|
def changes
|
2020-02-25 16:09:23 -05:00
|
|
|
strong_memoize(:changes) do
|
|
|
|
has_route_map? ? changed_files.map { |file| build_change(file) }.compact : []
|
|
|
|
end
|
2018-10-18 10:17:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def changed_files
|
|
|
|
merge_request.merge_request_diff
|
|
|
|
.merge_request_diff_files.where(deleted_file: false)
|
|
|
|
end
|
|
|
|
|
2020-02-25 16:09:23 -05:00
|
|
|
def changed_paths
|
|
|
|
changes.map { |change| change[:path] }
|
|
|
|
end
|
|
|
|
|
|
|
|
def changed_urls
|
|
|
|
changes.map { |change| change[:external_url] }
|
|
|
|
end
|
|
|
|
|
2019-07-10 10:07:14 -04:00
|
|
|
def has_route_map?
|
|
|
|
project.route_map_for(sha).present?
|
|
|
|
end
|
|
|
|
|
2018-10-18 10:17:24 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
PAGE_EXTENSIONS = /\A\.(s?html?|php|asp|cgi|pl)\z/i.freeze
|
|
|
|
|
2019-07-07 17:19:21 -04:00
|
|
|
def deployment_metrics
|
|
|
|
@deployment_metrics ||= DeploymentMetrics.new(project, deployment)
|
|
|
|
end
|
|
|
|
|
2018-10-29 07:34:41 -04:00
|
|
|
def build_change(file)
|
2018-10-18 10:17:24 -04:00
|
|
|
public_path = project.public_path_for_source_path(file.new_path, sha)
|
|
|
|
return if public_path.nil?
|
|
|
|
|
|
|
|
ext = File.extname(public_path)
|
|
|
|
return if ext.present? && ext !~ PAGE_EXTENSIONS
|
|
|
|
|
|
|
|
{
|
|
|
|
path: public_path,
|
|
|
|
external_url: environment.external_url_for(file.new_path, sha)
|
|
|
|
}
|
|
|
|
end
|
2018-10-29 07:34:41 -04:00
|
|
|
|
2018-12-03 01:46:45 -05:00
|
|
|
def self.build_environments_status(mr, user, pipeline)
|
|
|
|
return [] unless pipeline
|
|
|
|
|
2019-11-29 04:06:31 -05:00
|
|
|
pipeline.environments.includes(:project).available.map do |environment|
|
2018-11-06 08:08:33 -05:00
|
|
|
next unless Ability.allowed?(user, :read_environment, environment)
|
2018-10-29 07:34:41 -04:00
|
|
|
|
2019-07-01 04:27:58 -04:00
|
|
|
EnvironmentStatus.new(pipeline.project, environment, mr, pipeline.sha)
|
2018-11-06 08:08:33 -05:00
|
|
|
end.compact
|
2018-10-29 07:34:41 -04:00
|
|
|
end
|
|
|
|
private_class_method :build_environments_status
|
2018-10-09 12:17:40 -04:00
|
|
|
end
|