Skip to content
Snippets Groups Projects
Commit 1b5b0dea authored by Thong Kuah's avatar Thong Kuah :speech_balloon:
Browse files

Share project object in EnvironmentStatus

Otherwise, each EnvironmentStatus object instantiates its own project
when really they are the same. Improves query count performance
parent 1668f40f
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -3,11 +3,10 @@
class EnvironmentStatus
include Gitlab::Utils::StrongMemoize
 
attr_reader :environment, :merge_request, :sha
attr_reader :project, :environment, :merge_request, :sha
 
delegate :id, to: :environment
delegate :name, to: :environment
delegate :project, to: :environment
delegate :status, to: :deployment, allow_nil: true
delegate :deployed_at, to: :deployment, allow_nil: true
 
Loading
Loading
@@ -21,7 +20,8 @@ class EnvironmentStatus
build_environments_status(mr, user, mr.merge_pipeline)
end
 
def initialize(environment, merge_request, sha)
def initialize(project, environment, merge_request, sha)
@project = project
@environment = environment
@merge_request = merge_request
@sha = sha
Loading
Loading
@@ -67,7 +67,7 @@ class EnvironmentStatus
pipeline.environments.available.map do |environment|
next unless Ability.allowed?(user, :read_environment, environment)
 
EnvironmentStatus.new(environment, mr, pipeline.sha)
EnvironmentStatus.new(pipeline.project, environment, mr, pipeline.sha)
end.compact
end
private_class_method :build_environments_status
Loading
Loading
Loading
Loading
@@ -11,11 +11,10 @@ describe EnvironmentStatus do
let(:merge_request) { create(:merge_request, :deployed_review_app, deployment: deployment) }
let(:sha) { deployment.sha }
 
subject(:environment_status) { described_class.new(environment, merge_request, sha) }
subject(:environment_status) { described_class.new(project, environment, merge_request, sha) }
 
it { is_expected.to delegate_method(:id).to(:environment) }
it { is_expected.to delegate_method(:name).to(:environment) }
it { is_expected.to delegate_method(:project).to(:environment) }
it { is_expected.to delegate_method(:deployed_at).to(:deployment) }
it { is_expected.to delegate_method(:status).to(:deployment) }
 
Loading
Loading
Loading
Loading
@@ -9,7 +9,7 @@ describe EnvironmentStatusEntity do
let(:project) { deployment.project }
let(:merge_request) { create(:merge_request, :deployed_review_app, deployment: deployment) }
 
let(:environment_status) { EnvironmentStatus.new(environment, merge_request, merge_request.diff_head_sha) }
let(:environment_status) { EnvironmentStatus.new(project, environment, merge_request, merge_request.diff_head_sha) }
let(:entity) { described_class.new(environment_status, request: request) }
 
subject { entity.as_json }
Loading
Loading
@@ -55,8 +55,14 @@ describe EnvironmentStatusEntity do
before do
project.add_maintainer(user)
allow(deployment).to receive(:prometheus_adapter).and_return(prometheus_adapter)
allow(prometheus_adapter).to receive(:query).with(:deployment, deployment).and_return(simple_metrics)
allow(entity).to receive(:deployment).and_return(deployment)
expect_next_instance_of(DeploymentMetrics) do |deployment_metrics|
allow(deployment_metrics).to receive(:prometheus_adapter).and_return(prometheus_adapter)
allow(prometheus_adapter).to receive(:query)
.with(:deployment, deployment).and_return(simple_metrics)
end
end
 
context 'when deployment succeeded' do
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment