Commit c7916c8b authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch 'environments-list-data-d' into 'master'

Fill in Pipeline Footer in Environment Dashboard

See merge request gitlab-org/gitlab!17645
parents 398fef1c e6ca39f1
......@@ -10,7 +10,11 @@ class Environment < ApplicationRecord
has_many :successful_deployments, -> { success }, class_name: 'Deployment'
has_one :last_deployment, -> { success.order('deployments.id DESC') }, class_name: 'Deployment'
has_one :last_deployable, through: :last_deployment, source: 'deployable', source_type: 'CommitStatus'
has_one :last_pipeline, through: :last_deployable, source: 'pipeline'
has_one :last_visible_deployment, -> { visible.distinct_on_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'
before_validation :nullify_external_url
before_validation :generate_slug, if: ->(env) { env.slug.blank? }
......
......@@ -9,8 +9,6 @@ module EE
prepended do
has_many :prometheus_alerts, inverse_of: :environment
has_many :self_managed_prometheus_alert_events, inverse_of: :environment
has_one :last_deployable, through: :last_deployment, source: 'deployable', source_type: 'CommitStatus'
has_one :last_pipeline, through: :last_deployable, source: 'pipeline'
# Returns environments where its latest deployment is to a cluster
scope :deployed_to_cluster, -> (cluster) do
......
......@@ -15,6 +15,10 @@ class DashboardEnvironmentEntity < Grape::Entity
DeploymentEntity.represent(environment.last_visible_deployment, options.merge(request: request_with_project))
end
expose :last_visible_pipeline, as: :last_pipeline, expose_nil: false do |environment|
PipelineDetailsEntity.represent(environment.last_visible_pipeline, options.merge(request: request_with_project))
end
private
alias_method :environment, :object
......
......@@ -454,7 +454,7 @@ describe OperationsController do
it 'returns the last deployment for an environment' do
environment = create(:environment, project: project)
deployment = create(:deployment, project: project, environment: environment, status: :success)
deployment = create(:deployment, :success, project: project, environment: environment)
get :environments_list
......@@ -471,7 +471,7 @@ describe OperationsController do
it "returns the last deployment's deployable" do
environment = create(:environment, project: project)
ci_build = create(:ci_build, project: project)
create(:deployment, project: project, environment: environment, deployable: ci_build, status: :success)
create(:deployment, :success, project: project, environment: environment, deployable: ci_build)
get :environments_list
......@@ -501,6 +501,51 @@ describe OperationsController do
expect(last_deployment_json['id']).to eq(deployment.id)
end
context 'with a pipeline' do
let(:project) { create(:project, :repository) }
let(:commit) { project.commit }
let(:environment) { create(:environment, project: project) }
before do
project.add_developer(user)
user.update!(ops_dashboard_projects: [project])
end
it 'returns the last pipeline for an environment' do
pipeline = create(:ci_pipeline, project: project, user: user, sha: commit.sha)
ci_build = create(:ci_build, project: project, pipeline: pipeline)
create(:deployment, :success, project: project, environment: environment, deployable: ci_build, sha: commit.sha)
get :environments_list
expect(response).to have_gitlab_http_status(:ok)
expect(response).to match_response_schema('dashboard/operations/environments_list', dir: 'ee')
project_json = json_response['projects'].first
environment_json = project_json['environments'].first
last_pipeline_json = environment_json['last_pipeline']
expect(last_pipeline_json['id']).to eq(pipeline.id)
end
it 'returns the last pipeline details' do
pipeline = create(:ci_pipeline, project: project, user: user, sha: commit.sha, status: :canceled)
ci_build = create(:ci_build, project: project, pipeline: pipeline)
create(:deployment, :canceled, project: project, environment: environment, deployable: ci_build, sha: commit.sha)
get :environments_list
expect(response).to have_gitlab_http_status(:ok)
expect(response).to match_response_schema('dashboard/operations/environments_list', dir: 'ee')
project_json = json_response['projects'].first
environment_json = project_json['environments'].first
last_pipeline_json = environment_json['last_pipeline']
expect(last_pipeline_json.dig('details', 'status', 'group')).to eq('canceled')
end
end
end
end
end
......
......@@ -12,7 +12,8 @@ describe DashboardEnvironmentEntity do
result = described_class.new(environment, request: request).as_json
expect(result.keys.sort).to eq([:environment_path, :external_url, :id, :last_deployment, :name])
expect(result.keys.sort).to eq([:environment_path, :external_url, :id,
:last_deployment, :last_pipeline, :name])
end
end
end
......@@ -14,7 +14,8 @@ describe DashboardEnvironmentsFolderEntity do
result = described_class.new(environment_folder, request: request).as_json
expect(result.keys.sort).to eq([:environment_path, :external_url, :id, :last_deployment, :name, :size, :within_folder])
expect(result.keys.sort).to eq([:environment_path, :external_url, :id, :last_deployment,
:last_pipeline, :name, :size, :within_folder])
end
end
end
......@@ -51,6 +51,10 @@ FactoryBot.define do
finished_at { Time.now }
end
trait :created do
status { :created }
end
# This trait hooks the state maechine's events
trait :succeed do
after(:create) do |deployment, evaluator|
......
......@@ -5,6 +5,7 @@ require 'spec_helper'
describe Environment, :use_clean_rails_memory_store_caching do
include ReactiveCachingHelpers
using RSpec::Parameterized::TableSyntax
include RepoHelpers
let(:project) { create(:project, :stubbed_repository) }
subject(:environment) { create(:environment, project: project) }
......@@ -505,6 +506,14 @@ describe Environment, :use_clean_rails_memory_store_caching do
end
end
context 'when there is a deployment record with failed status' do
let!(:deployment) { create(:deployment, :failed, environment: environment) }
it 'returns the previous deployment' do
is_expected.to eq(previous_deployment)
end
end
context 'when there is a deployment record with success status' do
let!(:deployment) { create(:deployment, :success, environment: environment) }
......@@ -557,6 +566,89 @@ describe Environment, :use_clean_rails_memory_store_caching do
end
end
describe '#last_visible_pipeline' do
let(:user) { create(:user) }
let_it_be(:project) { create(:project, :repository) }
let(:environment) { create(:environment, project: project) }
let(:commit) { project.commit }
let(:success_pipeline) do
create(:ci_pipeline, :success, project: project, user: user, sha: commit.sha)
end
let(:failed_pipeline) do
create(:ci_pipeline, :failed, project: project, user: user, sha: commit.sha)
end
it 'uses the last deployment even if it failed' do
pipeline = create(:ci_pipeline, project: project, user: user, sha: commit.sha)
ci_build = create(:ci_build, project: project, pipeline: pipeline)
create(:deployment, :failed, project: project, environment: environment, deployable: ci_build, sha: commit.sha)
last_pipeline = environment.last_visible_pipeline
expect(last_pipeline).to eq(pipeline)
end
it 'returns nil if there is no deployment' do
create(:ci_build, project: project, pipeline: success_pipeline)
expect(environment.last_visible_pipeline).to be_nil
end
it 'does not return an invisible pipeline' do
failed_pipeline = create(:ci_pipeline, project: project, user: user, sha: commit.sha)
ci_build_a = create(:ci_build, project: project, pipeline: failed_pipeline)
create(:deployment, :failed, project: project, environment: environment, deployable: ci_build_a, sha: commit.sha)
pipeline = create(:ci_pipeline, project: project, user: user, sha: commit.sha)
ci_build_b = create(:ci_build, project: project, pipeline: pipeline)
create(:deployment, :created, project: project, environment: environment, deployable: ci_build_b, sha: commit.sha)
last_pipeline = environment.last_visible_pipeline
expect(last_pipeline).to eq(failed_pipeline)
end
context 'for the environment' do
it 'returns the last pipeline' do
pipeline = create(:ci_pipeline, project: project, user: user, sha: commit.sha)
ci_build = create(:ci_build, project: project, pipeline: pipeline)
create(:deployment, :success, project: project, environment: environment, deployable: ci_build, sha: commit.sha)
last_pipeline = environment.last_visible_pipeline
expect(last_pipeline).to eq(pipeline)
end
context 'with multiple deployments' do
it 'returns the last pipeline' do
pipeline_a = create(:ci_pipeline, project: project, user: user)
pipeline_b = create(:ci_pipeline, project: project, user: user)
ci_build_a = create(:ci_build, project: project, pipeline: pipeline_a)
ci_build_b = create(:ci_build, project: project, pipeline: pipeline_b)
create(:deployment, :success, project: project, environment: environment, deployable: ci_build_a)
create(:deployment, :success, project: project, environment: environment, deployable: ci_build_b)
last_pipeline = environment.last_visible_pipeline
expect(last_pipeline).to eq(pipeline_b)
end
end
context 'with multiple pipelines' do
it 'returns the last pipeline' do
create(:ci_build, project: project, pipeline: success_pipeline)
ci_build_b = create(:ci_build, project: project, pipeline: failed_pipeline)
create(:deployment, :failed, project: project, environment: environment, deployable: ci_build_b, sha: commit.sha)
last_pipeline = environment.last_visible_pipeline
expect(last_pipeline).to eq(failed_pipeline)
end
end
end
end
describe '#has_terminals?' do
subject { environment.has_terminals? }
......
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