Commit 6c2a3c24 authored by Kerri Miller's avatar Kerri Miller

Merge branch 'expose-upcoming-deployment' into 'master'

Expose upcoming deployment in environment.json

See merge request gitlab-org/gitlab!48449
parents d72665bc 0527bfcd
...@@ -32,6 +32,7 @@ class Environment < ApplicationRecord ...@@ -32,6 +32,7 @@ class Environment < ApplicationRecord
has_one :last_visible_deployment, -> { visible.distinct_on_environment }, inverse_of: :environment, class_name: 'Deployment' has_one :last_visible_deployment, -> { visible.distinct_on_environment }, inverse_of: :environment, class_name: 'Deployment'
has_one :last_visible_deployable, through: :last_visible_deployment, source: 'deployable', source_type: 'CommitStatus' has_one :last_visible_deployable, through: :last_visible_deployment, source: 'deployable', source_type: 'CommitStatus'
has_one :last_visible_pipeline, through: :last_visible_deployable, source: 'pipeline' has_one :last_visible_pipeline, through: :last_visible_deployable, source: 'pipeline'
has_one :upcoming_deployment, -> { running.order('deployments.id DESC') }, class_name: 'Deployment'
has_one :latest_opened_most_severe_alert, -> { order_severity_with_open_prometheus_alert }, class_name: 'AlertManagement::Alert', inverse_of: :environment has_one :latest_opened_most_severe_alert, -> { order_severity_with_open_prometheus_alert }, class_name: 'AlertManagement::Alert', inverse_of: :environment
before_validation :nullify_external_url before_validation :nullify_external_url
......
...@@ -3,6 +3,9 @@ ...@@ -3,6 +3,9 @@
class EnvironmentEntity < Grape::Entity class EnvironmentEntity < Grape::Entity
include RequestAwareEntity include RequestAwareEntity
UNNECESSARY_ENTRIES_FOR_UPCOMING_DEPLOYMENT =
%i[manual_actions scheduled_actions playable_build cluster].freeze
expose :id expose :id
expose :global_id do |environment| expose :global_id do |environment|
...@@ -17,6 +20,11 @@ class EnvironmentEntity < Grape::Entity ...@@ -17,6 +20,11 @@ class EnvironmentEntity < Grape::Entity
expose :last_deployment, using: DeploymentEntity expose :last_deployment, using: DeploymentEntity
expose :stop_action_available?, as: :has_stop_action expose :stop_action_available?, as: :has_stop_action
expose :upcoming_deployment, expose_nil: false do |environment, ops|
DeploymentEntity.represent(environment.upcoming_deployment,
ops.merge(except: UNNECESSARY_ENTRIES_FOR_UPCOMING_DEPLOYMENT))
end
expose :metrics_path, if: -> (*) { environment.has_metrics? } do |environment| expose :metrics_path, if: -> (*) { environment.has_metrics? } do |environment|
metrics_project_environment_path(environment.project, environment) metrics_project_environment_path(environment.project, environment)
end end
......
---
title: Expose upcoming deployment in environment.json
merge_request: 48449
author:
type: added
...@@ -19,6 +19,7 @@ RSpec.describe Environment, :use_clean_rails_memory_store_caching do ...@@ -19,6 +19,7 @@ RSpec.describe Environment, :use_clean_rails_memory_store_caching do
it { is_expected.to have_many(:deployments) } it { is_expected.to have_many(:deployments) }
it { is_expected.to have_many(:metrics_dashboard_annotations) } it { is_expected.to have_many(:metrics_dashboard_annotations) }
it { is_expected.to have_many(:alert_management_alerts) } it { is_expected.to have_many(:alert_management_alerts) }
it { is_expected.to have_one(:upcoming_deployment) }
it { is_expected.to have_one(:latest_opened_most_severe_alert) } it { is_expected.to have_one(:latest_opened_most_severe_alert) }
it { is_expected.to delegate_method(:stop_action).to(:last_deployment) } it { is_expected.to delegate_method(:stop_action).to(:last_deployment) }
...@@ -723,6 +724,22 @@ RSpec.describe Environment, :use_clean_rails_memory_store_caching do ...@@ -723,6 +724,22 @@ RSpec.describe Environment, :use_clean_rails_memory_store_caching do
end end
end end
describe '#upcoming_deployment' do
subject { environment.upcoming_deployment }
context 'when environment has a successful deployment' do
let!(:deployment) { create(:deployment, :success, environment: environment, project: project) }
it { is_expected.to be_nil }
end
context 'when environment has a running deployment' do
let!(:deployment) { create(:deployment, :running, environment: environment, project: project) }
it { is_expected.to eq(deployment) }
end
end
describe '#has_terminals?' do describe '#has_terminals?' do
subject { environment.has_terminals? } subject { environment.has_terminals? }
......
...@@ -7,15 +7,20 @@ RSpec.describe EnvironmentEntity do ...@@ -7,15 +7,20 @@ RSpec.describe EnvironmentEntity do
let(:request) { double('request') } let(:request) { double('request') }
let(:entity) do let(:entity) do
described_class.new(environment, request: spy('request')) described_class.new(environment, request: request)
end end
let_it_be(:user) { create(:user) } let_it_be(:user) { create(:user) }
let_it_be(:project) { create(:project) } let_it_be(:project) { create(:project, :repository) }
let_it_be(:environment) { create(:environment, project: project) } let_it_be(:environment, refind: true) { create(:environment, project: project) }
before_all do
project.add_developer(user)
end
before do before do
allow(entity).to receive(:current_user).and_return(user) allow(request).to receive(:current_user).and_return(user)
allow(request).to receive(:project).and_return(project)
end end
subject { entity.as_json } subject { entity.as_json }
...@@ -32,6 +37,51 @@ RSpec.describe EnvironmentEntity do ...@@ -32,6 +37,51 @@ RSpec.describe EnvironmentEntity do
expect(subject).to include(:folder_path) expect(subject).to include(:folder_path)
end end
context 'when there is a successful deployment' do
let!(:pipeline) { create(:ci_pipeline, :success, project: project) }
let!(:deployable) { create(:ci_build, :success, project: project, pipeline: pipeline) }
let!(:deployment) { create(:deployment, :success, project: project, environment: environment, deployable: deployable) }
it 'exposes it as the latest deployment' do
expect(subject[:last_deployment][:sha]).to eq(deployment.sha)
end
it 'does not expose it as an upcoming deployment' do
expect(subject[:upcoming_deployment]).to be_nil
end
context 'when the deployment pipeline has the other manual job' do
let!(:manual_job) { create(:ci_build, :manual, name: 'stop-review', project: project, pipeline: pipeline) }
it 'exposes the manual job in the latest deployment' do
expect(subject[:last_deployment][:manual_actions].first[:name])
.to eq(manual_job.name)
end
end
end
context 'when there is a running deployment' do
let!(:pipeline) { create(:ci_pipeline, :running, project: project) }
let!(:deployable) { create(:ci_build, :running, project: project, pipeline: pipeline) }
let!(:deployment) { create(:deployment, :running, project: project, environment: environment, deployable: deployable) }
it 'does not expose it as the latest deployment' do
expect(subject[:last_deployment]).to be_nil
end
it 'exposes it as an upcoming deployment' do
expect(subject[:upcoming_deployment][:sha]).to eq(deployment.sha)
end
context 'when the deployment pipeline has the other manual job' do
let!(:manual_job) { create(:ci_build, :manual, name: 'stop-review', project: project, pipeline: pipeline) }
it 'does not expose the manual job in the latest deployment' do
expect(subject[:upcoming_deployment][:manual_actions]).to be_nil
end
end
end
context 'metrics disabled' do context 'metrics disabled' do
before do before do
allow(environment).to receive(:has_metrics?).and_return(false) allow(environment).to receive(:has_metrics?).and_return(false)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment