Skip to content
Snippets Groups Projects
Commit 0527bfcd authored by Shinya Maeda's avatar Shinya Maeda
Browse files

Expose upcoming deployment

This commit exposes the upcoming deployment in EnvironmentEntity.
parent e08701cb
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -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_deployable, through: :last_visible_deployment, source: 'deployable', source_type: 'CommitStatus'
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
 
before_validation :nullify_external_url
Loading
Loading
Loading
Loading
@@ -3,6 +3,9 @@
class EnvironmentEntity < Grape::Entity
include RequestAwareEntity
 
UNNECESSARY_ENTRIES_FOR_UPCOMING_DEPLOYMENT =
%i[manual_actions scheduled_actions playable_build cluster].freeze
expose :id
 
expose :global_id do |environment|
Loading
Loading
@@ -17,6 +20,11 @@ class EnvironmentEntity < Grape::Entity
expose :last_deployment, using: DeploymentEntity
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|
metrics_project_environment_path(environment.project, environment)
end
Loading
Loading
---
title: Expose upcoming deployment in environment.json
merge_request: 48449
author:
type: added
Loading
Loading
@@ -19,6 +19,7 @@
it { is_expected.to have_many(:deployments) }
it { is_expected.to have_many(:metrics_dashboard_annotations) }
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 delegate_method(:stop_action).to(:last_deployment) }
Loading
Loading
@@ -723,6 +724,22 @@
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
subject { environment.has_terminals? }
 
Loading
Loading
Loading
Loading
@@ -7,15 +7,20 @@
 
let(:request) { double('request') }
let(:entity) do
described_class.new(environment, request: spy('request'))
described_class.new(environment, request: request)
end
 
let_it_be(:user) { create(:user) }
let_it_be(:project) { create(:project) }
let_it_be(:environment) { create(:environment, project: project) }
let_it_be(:project) { create(:project, :repository) }
let_it_be(:environment, refind: true) { create(:environment, project: project) }
before_all do
project.add_developer(user)
end
 
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
 
subject { entity.as_json }
Loading
Loading
@@ -32,6 +37,51 @@
expect(subject).to include(:folder_path)
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
before do
allow(environment).to receive(:has_metrics?).and_return(false)
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