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

Implement entities needed for environments folders

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