Commit 6bf3b0bc authored by Dylan Griffith's avatar Dylan Griffith

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.
parent 01e12b05
# 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
...@@ -38,6 +38,8 @@ class DeploymentEntity < Grape::Entity ...@@ -38,6 +38,8 @@ class DeploymentEntity < Grape::Entity
expose :manual_actions, using: JobEntity, if: -> (*) { include_details? && can_create_deployment? } expose :manual_actions, using: JobEntity, if: -> (*) { include_details? && can_create_deployment? }
expose :scheduled_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 private
def include_details? def include_details?
......
...@@ -265,7 +265,8 @@ describe Projects::JobsController, :clean_gitlab_redis_shared_state do ...@@ -265,7 +265,8 @@ describe Projects::JobsController, :clean_gitlab_redis_shared_state do
let(:job) { create(:ci_build, :running, environment: environment.name, pipeline: pipeline) } let(:job) { create(:ci_build, :running, environment: environment.name, pipeline: pipeline) }
before do 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 end
it 'exposes the deployment information' do it 'exposes the deployment information' do
...@@ -276,8 +277,9 @@ describe Projects::JobsController, :clean_gitlab_redis_shared_state 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', 'status')).to eq 'creating'
expect(json_response.dig('deployment_status', 'environment')).not_to be_nil 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 be_nil
expect(json_response.dig('deployment_status', 'environment', 'last_deployment')) expect(json_response.dig('deployment_status', 'environment', 'last_deployment')).not_to include('commit')
.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
end end
......
...@@ -17,6 +17,10 @@ FactoryBot.define do ...@@ -17,6 +17,10 @@ FactoryBot.define do
unless deployment.project.repository_exists? unless deployment.project.repository_exists?
allow(deployment.project.repository).to receive(:create_ref) allow(deployment.project.repository).to receive(:create_ref)
end end
if deployment.cluster && deployment.cluster.project_type? && deployment.cluster.project.nil?
deployment.cluster.projects << deployment.project
end
end end
trait :review_app do trait :review_app do
......
{
"type": "object",
"required": [
"name"
],
"properties": {
"name": { "type": "string" },
"path": {
"oneOf": [
{ "type": "null" },
{ "type": "string" }
]
}
},
"additionalProperties": false
}
...@@ -47,6 +47,12 @@ ...@@ -47,6 +47,12 @@
{ "$ref": "job/job.json" } { "$ref": "job/job.json" }
] ]
}, },
"cluster": {
"oneOf": [
{ "type": "null" },
{ "$ref": "cluster_basic.json" }
]
},
"manual_actions": { "manual_actions": {
"type": "array", "type": "array",
"items": { "$ref": "job/job.json" } "items": { "$ref": "job/job.json" }
......
...@@ -28,6 +28,8 @@ ...@@ -28,6 +28,8 @@
"created_at": { "type": "string", "format": "date-time" }, "created_at": { "type": "string", "format": "date-time" },
"updated_at": { "type": "string", "format": "date-time" }, "updated_at": { "type": "string", "format": "date-time" },
"can_stop": { "type": "boolean" }, "can_stop": { "type": "boolean" },
"cluster_type": { "type": "types/nullable_string.json" },
"terminal_path": { "type": "types/nullable_string.json" },
"last_deployment": { "last_deployment": {
"oneOf": [ "oneOf": [
{ "type": "null" }, { "type": "null" },
......
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
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