diff options
author | Dylan Griffith <dgriffith@gitlab.com> | 2019-09-02 03:13:30 +0000 |
---|---|---|
committer | Dylan Griffith <dgriffith@gitlab.com> | 2019-09-13 00:40:45 +0000 |
commit | e39a7f0e4baf5d1a0bd8cd021aeb6e391d4d666a (patch) | |
tree | ce5c5884d2d65008cbb0bd3ad5ae00e85ce6789b | |
parent | 25cb337cf12438169f1b14bc5dace8a06a7356e3 (diff) | |
download | gitlab-ce-e39a7f0e4baf5d1a0bd8cd021aeb6e391d4d666a.tar.gz |
Return cluster details from job endpoint
This cluster detail will be used to show cluster link in the job output
as per https://gitlab.com/gitlab-org/gitlab-ce/issues/55095 . We needed
to create a new model here for ClusterBasicEntity as the only other
model ClusterStatus had much more data than necessary including nested
model lookups for applications.
-rw-r--r-- | app/serializers/cluster_basic_entity.rb | 10 | ||||
-rw-r--r-- | app/serializers/deployment_entity.rb | 2 | ||||
-rw-r--r-- | spec/controllers/projects/jobs_controller_spec.rb | 8 | ||||
-rw-r--r-- | spec/factories/deployments.rb | 4 | ||||
-rw-r--r-- | spec/fixtures/api/schemas/cluster_basic.json | 16 | ||||
-rw-r--r-- | spec/fixtures/api/schemas/deployment.json | 6 | ||||
-rw-r--r-- | spec/fixtures/api/schemas/environment.json | 2 | ||||
-rw-r--r-- | spec/serializers/cluster_basic_entity_spec.rb | 35 |
8 files changed, 80 insertions, 3 deletions
diff --git a/app/serializers/cluster_basic_entity.rb b/app/serializers/cluster_basic_entity.rb new file mode 100644 index 00000000000..d104f2c8bbd --- /dev/null +++ b/app/serializers/cluster_basic_entity.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +class ClusterBasicEntity < Grape::Entity + include RequestAwareEntity + + expose :name + expose :path, if: -> (cluster) { can?(request.current_user, :read_cluster, cluster) } do |cluster| + cluster.present(current_user: request.current_user).show_path + end +end diff --git a/app/serializers/deployment_entity.rb b/app/serializers/deployment_entity.rb index 94a827658f0..e6421315b34 100644 --- a/app/serializers/deployment_entity.rb +++ b/app/serializers/deployment_entity.rb @@ -38,6 +38,8 @@ class DeploymentEntity < Grape::Entity expose :manual_actions, using: JobEntity, if: -> (*) { include_details? && can_create_deployment? } expose :scheduled_actions, using: JobEntity, if: -> (*) { include_details? && can_create_deployment? } + expose :cluster, using: ClusterBasicEntity + private def include_details? diff --git a/spec/controllers/projects/jobs_controller_spec.rb b/spec/controllers/projects/jobs_controller_spec.rb index bd3e66efd58..b455b55bd11 100644 --- a/spec/controllers/projects/jobs_controller_spec.rb +++ b/spec/controllers/projects/jobs_controller_spec.rb @@ -265,7 +265,8 @@ describe Projects::JobsController, :clean_gitlab_redis_shared_state do let(:job) { create(:ci_build, :running, environment: environment.name, pipeline: pipeline) } before do - create(:deployment, :success, environment: environment, project: project) + create(:deployment, :success, :on_cluster, environment: environment, project: project) + project.add_maintainer(user) # Need to be a maintianer to view cluster.path end it 'exposes the deployment information' do @@ -276,8 +277,9 @@ describe Projects::JobsController, :clean_gitlab_redis_shared_state do expect(json_response.dig('deployment_status', 'status')).to eq 'creating' expect(json_response.dig('deployment_status', 'environment')).not_to be_nil expect(json_response.dig('deployment_status', 'environment', 'last_deployment')).not_to be_nil - expect(json_response.dig('deployment_status', 'environment', 'last_deployment')) - .not_to include('commit') + expect(json_response.dig('deployment_status', 'environment', 'last_deployment')).not_to include('commit') + expect(json_response.dig('deployment_status', 'environment', 'last_deployment', 'cluster', 'name')).to eq('test-cluster') + expect(json_response.dig('deployment_status', 'environment', 'last_deployment', 'cluster', 'path')).to be_present end end diff --git a/spec/factories/deployments.rb b/spec/factories/deployments.rb index 89ff1c527df..50dc304a10e 100644 --- a/spec/factories/deployments.rb +++ b/spec/factories/deployments.rb @@ -17,6 +17,10 @@ FactoryBot.define do unless deployment.project.repository_exists? allow(deployment.project.repository).to receive(:create_ref) end + + if deployment.cluster && deployment.cluster.project_type? && deployment.cluster.project.nil? + deployment.cluster.projects << deployment.project + end end trait :review_app do diff --git a/spec/fixtures/api/schemas/cluster_basic.json b/spec/fixtures/api/schemas/cluster_basic.json new file mode 100644 index 00000000000..6f0e77997f0 --- /dev/null +++ b/spec/fixtures/api/schemas/cluster_basic.json @@ -0,0 +1,16 @@ +{ + "type": "object", + "required": [ + "name" + ], + "properties": { + "name": { "type": "string" }, + "path": { + "oneOf": [ + { "type": "null" }, + { "type": "string" } + ] + } + }, + "additionalProperties": false +} diff --git a/spec/fixtures/api/schemas/deployment.json b/spec/fixtures/api/schemas/deployment.json index fe725b97c21..81c2d1ef5ab 100644 --- a/spec/fixtures/api/schemas/deployment.json +++ b/spec/fixtures/api/schemas/deployment.json @@ -47,6 +47,12 @@ { "$ref": "job/job.json" } ] }, + "cluster": { + "oneOf": [ + { "type": "null" }, + { "$ref": "cluster_basic.json" } + ] + }, "manual_actions": { "type": "array", "items": { "$ref": "job/job.json" } diff --git a/spec/fixtures/api/schemas/environment.json b/spec/fixtures/api/schemas/environment.json index 5b1e3c049fa..e38b05ec008 100644 --- a/spec/fixtures/api/schemas/environment.json +++ b/spec/fixtures/api/schemas/environment.json @@ -28,6 +28,8 @@ "created_at": { "type": "string", "format": "date-time" }, "updated_at": { "type": "string", "format": "date-time" }, "can_stop": { "type": "boolean" }, + "cluster_type": { "type": "types/nullable_string.json" }, + "terminal_path": { "type": "types/nullable_string.json" }, "last_deployment": { "oneOf": [ { "type": "null" }, diff --git a/spec/serializers/cluster_basic_entity_spec.rb b/spec/serializers/cluster_basic_entity_spec.rb new file mode 100644 index 00000000000..6762eb6ab3d --- /dev/null +++ b/spec/serializers/cluster_basic_entity_spec.rb @@ -0,0 +1,35 @@ +require 'spec_helper' + +describe ClusterBasicEntity do + describe '#as_json' do + subject { described_class.new(cluster, request: request).as_json } + let(:maintainer) { create(:user) } + let(:developer) { create(:user) } + let(:current_user) { maintainer } + let(:request) { double(:request, current_user: current_user) } + let(:project) { create(:project) } + let(:cluster) { create(:cluster, name: 'the-cluster', projects: [project]) } + + before do + project.add_maintainer(maintainer) + project.add_developer(developer) + end + + it 'matches cluster_basic entity schema' do + expect(subject.as_json).to match_schema('cluster_basic') + end + + it 'exposes the cluster details' do + expect(subject[:name]).to eq('the-cluster') + expect(subject[:path]).to eq("/#{project.full_path}/clusters/#{cluster.id}") + end + + context 'when the user does not have permission to view the cluster' do + let(:current_user) { developer } + + it 'does not include the path' do + expect(subject[:path]).to be_nil + end + end + end +end |