Skip to content

Commit

Permalink
Do not serialize deployment details for build details page
Browse files Browse the repository at this point in the history
  • Loading branch information
grzesiek authored and Filipa Lacerda committed May 24, 2019
1 parent 412a385 commit de24df9
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/serializers/build_details_entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class BuildDetailsEntity < JobEntity
expose :deployment_status, if: -> (*) { build.starts_environment? } do
expose :deployment_status, as: :status
expose :persisted_environment, as: :environment do |build, options|
options.merge(except: [{ last_deployment: [:commit] }]).yield_self do |opts|
options.merge(deployment_details: false).yield_self do |opts|
EnvironmentEntity.represent(build.persisted_environment, opts)
end
end
Expand Down
14 changes: 9 additions & 5 deletions app/serializers/deployment_entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@ class DeploymentEntity < Grape::Entity
expose :created_at
expose :tag
expose :last?

expose :user, using: UserEntity
expose :commit, using: CommitEntity
expose :deployable, using: JobEntity
expose :manual_actions, using: JobEntity, if: -> (*) { can_create_deployment? }
expose :scheduled_actions, using: JobEntity, if: -> (*) { can_create_deployment? }

expose :commit, using: CommitEntity, if: -> (*) { include_details? }
expose :deployable, using: JobEntity, if: -> (*) { include_details? }
expose :manual_actions, using: JobEntity, if: -> (*) { include_details? && can_create_deployment? }
expose :scheduled_actions, using: JobEntity, if: -> (*) { include_details? && can_create_deployment? }

private

def include_details?
options.fetch(:deployment_details, true)
end

def can_create_deployment?
can?(request.current_user, :create_deployment, request.project)
end
Expand Down
8 changes: 4 additions & 4 deletions spec/controllers/projects/jobs_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,14 +253,14 @@ def get_index(**extra_params)
end

context 'with deployment' do
before do
create(:deployment, :success, environment: environment, project: project)
end

let(:merge_request) { create(:merge_request, source_project: project) }
let(:environment) { create(:environment, project: project, name: 'staging', state: :available) }
let(:job) { create(:ci_build, :running, environment: environment.name, pipeline: pipeline) }

before do
create(:deployment, :success, environment: environment, project: project)
end

it 'exposes the deployment information' do
get_show_json

Expand Down
4 changes: 2 additions & 2 deletions spec/serializers/build_details_entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@
allow(request).to receive(:project).and_return(project)
end

it 'does not serialize latest deployment commit' do
it 'does not serialize latest deployment commit and associated builds' do
response = subject.with_indifferent_access

response.dig(:deployment_status, :environment, :last_deployment).tap do |deployment|
expect(deployment).not_to include(:commit)
expect(deployment).not_to include(:commit, :deployable, :manual_actions, :scheduled_actions)
end
end
end
Expand Down
32 changes: 32 additions & 0 deletions spec/serializers/deployment_entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
let(:build) { create(:ci_build, :manual, pipeline: pipeline) }
let(:pipeline) { create(:ci_pipeline, project: project, user: user) }
let(:entity) { described_class.new(deployment, request: request) }

subject { entity.as_json }

before do
Expand Down Expand Up @@ -47,6 +48,16 @@
expect(subject[:manual_actions]).not_to be_present
end
end

context 'when deployment details serialization was disabled' do
let(:entity) do
described_class.new(deployment, request: request, deployment_details: false)
end

it 'does not serialize manual actions details' do
expect(subject.with_indifferent_access).not_to include(:manual_actions)
end
end
end

describe 'scheduled_actions' do
Expand All @@ -69,5 +80,26 @@
expect(subject[:scheduled_actions]).to be_empty
end
end

context 'when deployment details serialization was disabled' do
let(:entity) do
described_class.new(deployment, request: request, deployment_details: false)
end

it 'does not serialize scheduled actions details' do
expect(subject.with_indifferent_access).not_to include(:scheduled_actions)
end
end
end

context 'when deployment details serialization was disabled' do
let(:entity) do
described_class.new(deployment, request: request, deployment_details: false)
end

it 'does not serialize deployment details' do
expect(subject.with_indifferent_access)
.not_to include(:commit, :deployable, :manual_actions, :scheduled_actions)
end
end
end

0 comments on commit de24df9

Please sign in to comment.