diff --git a/app/serializers/base_serializer.rb b/app/serializers/base_serializer.rb index f9f7135551b..aeb01dc2ad5 100644 --- a/app/serializers/base_serializer.rb +++ b/app/serializers/base_serializer.rb @@ -1,17 +1,17 @@ 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) diff --git a/app/serializers/build_entity.rb b/app/serializers/build_entity.rb new file mode 100644 index 00000000000..38169ab9864 --- /dev/null +++ b/app/serializers/build_entity.rb @@ -0,0 +1,20 @@ +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 diff --git a/app/serializers/commit_entity.rb b/app/serializers/commit_entity.rb new file mode 100644 index 00000000000..ae53de0c750 --- /dev/null +++ b/app/serializers/commit_entity.rb @@ -0,0 +1,10 @@ +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 diff --git a/app/serializers/deployment_entity.rb b/app/serializers/deployment_entity.rb new file mode 100644 index 00000000000..3462b188337 --- /dev/null +++ b/app/serializers/deployment_entity.rb @@ -0,0 +1,27 @@ +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 diff --git a/app/serializers/environment_entity.rb b/app/serializers/environment_entity.rb index 006b2841e8f..6b037e65e55 100644 --- a/app/serializers/environment_entity.rb +++ b/app/serializers/environment_entity.rb @@ -1,25 +1,21 @@ 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 diff --git a/app/serializers/request_aware_entity.rb b/app/serializers/request_aware_entity.rb index fc7d1698b1a..1586507492d 100644 --- a/app/serializers/request_aware_entity.rb +++ b/app/serializers/request_aware_entity.rb @@ -1,6 +1,10 @@ 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 diff --git a/spec/factories/deployments.rb b/spec/factories/deployments.rb index 6f24bf58d14..8c8e5dc54c2 100644 --- a/spec/factories/deployments.rb +++ b/spec/factories/deployments.rb @@ -1,9 +1,9 @@ FactoryGirl.define do factory :deployment, class: Deployment do - sha '97de212e80737a608d939f648d959671fb0a0142' + sha 'b83d6e391c22777fca1ed3012fce84f633d7fed0' ref 'master' tag false - project nil + project environment factory: :environment diff --git a/spec/serializers/environment_serializer_spec.rb b/spec/serializers/environment_serializer_spec.rb index cd9486111f1..f7de6c450ab 100644 --- a/spec/serializers/environment_serializer_spec.rb +++ b/spec/serializers/environment_serializer_spec.rb @@ -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