Commit 573921cb authored by Grzegorz Bizon's avatar Grzegorz Bizon

Implement entities needed for environments folders

parent f1e9c97d
class BaseSerializer class BaseSerializer
def initialize(request = {}) def initialize(parameters = {})
@request = EntityRequest.new(request) @entity = self.class.entity_class
@request = EntityRequest.new(parameters)
@opts = { request: @request } @opts = { request: @request }
end end
def set(opts) def set(parameters)
@request.merge!(opts) @request.merge!(parameters)
self self
end end
def represent(resource, opts = {}) def represent(resource, opts = {})
self.class.entity_class @entity.represent(resource, @opts.reverse_merge(opts))
.represent(resource, @opts.reverse_merge(opts))
end end
def self.entity(entity_class) def self.entity(entity_class)
......
class BuildEntity < Grape::Entity
include RequestAwareEntity
expose :id
expose :name
expose :build_url do |build|
@urls.namespace_project_build_url(
build.project.namespace,
build.project,
build)
end
expose :retry_url do |build|
@urls.retry_namespace_project_build_url(
build.project.namespace,
build.project,
build)
end
end
class CommitEntity < API::Entities::RepoCommit
include RequestAwareEntity
expose :commit_url do |commit|
@urls.namespace_project_tree_url(
@request.project.namespace,
@request.project,
id: commit.id)
end
end
class DeploymentEntity < Grape::Entity
include RequestAwareEntity
expose :id
expose :iid
expose :sha
expose :ref do
expose :name do |deployment|
deployment.ref
end
expose :ref_url do |deployment|
@urls.namespace_project_tree_url(
deployment.project.namespace,
deployment.project,
id: deployment.ref)
end
end
expose :tag
expose :last?
expose :user, using: API::Entities::UserBasic
expose :commit, using: CommitEntity
expose :deployable, using: BuildEntity
expose :manual_actions
end
class EnvironmentEntity < Grape::Entity class EnvironmentEntity < Grape::Entity
include RequestAwareEntity include RequestAwareEntity
include Gitlab::Routing.url_helpers
expose :id expose :id
expose :name expose :name
expose :state
expose :external_url
expose :environment_type
expose :project, with: ProjectEntity expose :project, with: ProjectEntity
expose :last_deployment, expose :last_deployment, using: DeploymentEntity
as: :deployment, expose :stoppable?
using: API::Entities::Deployment
expose :gitlab_path do |environment| expose :environmenturl do |environment|
namespace_project_environment_path( @urls.namespace_project_environment_url(
environment.project.namespace, environment.project.namespace,
environment.project, environment.project,
environment environment)
)
end end
expose :can_read? expose :created_at, :updated_at
def can_read?
Ability.allowed?(request.user, :read_environment, @object)
end
end end
module RequestAwareEntity module RequestAwareEntity
def request attr_reader :request
options[:request] ||
raise(StandardError, 'Request not set!!') def initialize(object, options = {})
super(object, options)
@request = options.fetch(:request)
@urls = Gitlab::Routing.url_helpers
end end
end end
FactoryGirl.define do FactoryGirl.define do
factory :deployment, class: Deployment do factory :deployment, class: Deployment do
sha '97de212e80737a608d939f648d959671fb0a0142' sha 'b83d6e391c22777fca1ed3012fce84f633d7fed0'
ref 'master' ref 'master'
tag false tag false
project nil project
environment factory: :environment environment factory: :environment
......
...@@ -2,17 +2,25 @@ require 'spec_helper' ...@@ -2,17 +2,25 @@ require 'spec_helper'
describe EnvironmentSerializer do describe EnvironmentSerializer do
let(:serializer) do let(:serializer) do
described_class.new(path: 'some path', user: user) described_class
.new(user: user, project: project)
.represent(resource) .represent(resource)
end end
let(:user) { create(:user) } let(:user) { create(:user) }
context 'when there is a single object provided' do context 'when there is a single object provided' do
let(:resource) { create(:environment) } let(:deployment) do
create(:deployment, deployable: deployable,
user: user)
end
let(:deployable) { create(:ci_build) }
let(:project) { deployment.project }
let(:resource) { deployment.environment }
it 'shows json' do it 'shows json' do
puts serializer.as_json pp serializer.as_json
end end
it 'it generates payload for single object' do it 'it generates payload for single object' do
...@@ -21,6 +29,7 @@ describe EnvironmentSerializer do ...@@ -21,6 +29,7 @@ describe EnvironmentSerializer do
end end
context 'when there is a collection of objects provided' do context 'when there is a collection of objects provided' do
let(:project) { create(:empty_project) }
let(:resource) { create_list(:environment, 2) } let(:resource) { create_list(:environment, 2) }
it 'shows json' do it 'shows json' do
......
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