Commit a78a059f authored by Tetiana Chupryna's avatar Tetiana Chupryna

Merge branch 'refactor-entities-and-serializers' into 'master'

Rename BuildSerializer to JobSerializer

See merge request gitlab-org/gitlab!75763
parents 2f7177ee 3afd7d89
...@@ -42,7 +42,7 @@ class Projects::JobsController < Projects::ApplicationController ...@@ -42,7 +42,7 @@ class Projects::JobsController < Projects::ApplicationController
format.json do format.json do
Gitlab::PollingInterval.set_header(response, interval: 10_000) Gitlab::PollingInterval.set_header(response, interval: 10_000)
render json: BuildSerializer render json: Ci::JobSerializer
.new(project: @project, current_user: @current_user) .new(project: @project, current_user: @current_user)
.represent(@build.present(current_user: current_user), {}, BuildDetailsEntity) .represent(@build.present(current_user: current_user), {}, BuildDetailsEntity)
end end
...@@ -118,7 +118,7 @@ class Projects::JobsController < Projects::ApplicationController ...@@ -118,7 +118,7 @@ class Projects::JobsController < Projects::ApplicationController
end end
def status def status
render json: BuildSerializer render json: Ci::JobSerializer
.new(project: @project, current_user: @current_user) .new(project: @project, current_user: @current_user)
.represent_status(@build.present(current_user: current_user)) .represent_status(@build.present(current_user: current_user))
end end
......
# frozen_string_literal: true # frozen_string_literal: true
class BuildDetailsEntity < JobEntity class BuildDetailsEntity < Ci::JobEntity
expose :coverage, :erased_at, :duration expose :coverage, :erased_at, :duration
expose :tag_list, as: :tags expose :tag_list, as: :tags
expose :has_trace?, as: :has_trace expose :has_trace?, as: :has_trace
...@@ -109,6 +109,8 @@ class BuildDetailsEntity < JobEntity ...@@ -109,6 +109,8 @@ class BuildDetailsEntity < JobEntity
private private
alias_method :build, :object
def build_failed_issue_options def build_failed_issue_options
{ title: "Job Failed ##{build.id}", { title: "Job Failed ##{build.id}",
description: "Job [##{build.id}](#{project_job_url(project, build)}) failed for #{build.sha}:\n" } description: "Job [##{build.id}](#{project_job_url(project, build)}) failed for #{build.sha}:\n" }
......
# frozen_string_literal: true
class BuildSerializer < BaseSerializer
entity JobEntity
def represent_status(resource)
data = represent(resource, { only: [:status] })
data.fetch(:status, {})
end
end
# frozen_string_literal: true
module Ci
class JobEntity < Grape::Entity
include RequestAwareEntity
expose :id
expose :name
expose :started?, as: :started
expose :complete?, as: :complete
expose :archived?, as: :archived
# bridge jobs don't have build details pages
expose :build_path, if: ->(job) { !job.is_a?(Ci::Bridge) } do |job|
job_path(job)
end
expose :retry_path, if: -> (*) { retryable? } do |job|
path_to(:retry_namespace_project_job, job)
end
expose :cancel_path, if: -> (*) { cancelable? } do |job|
path_to(
:cancel_namespace_project_job,
job,
{ continue: { to: job_path(job) } }
)
end
expose :play_path, if: -> (*) { playable? } do |job|
path_to(:play_namespace_project_job, job)
end
expose :unschedule_path, if: -> (*) { scheduled? } do |job|
path_to(:unschedule_namespace_project_job, job)
end
expose :playable?, as: :playable
expose :scheduled?, as: :scheduled
expose :scheduled_at, if: -> (*) { scheduled? }
expose :created_at
expose :updated_at
expose :detailed_status, as: :status, with: DetailedStatusEntity
expose :callout_message, if: -> (*) { failed? && !job.script_failure? }
expose :recoverable, if: -> (*) { failed? }
private
alias_method :job, :object
def cancelable?
job.cancelable? && can?(request.current_user, :update_build, job)
end
def retryable?
job.retryable? && can?(request.current_user, :update_build, job)
end
def playable?
job.playable? && can?(request.current_user, :update_build, job)
end
def scheduled?
job.scheduled?
end
def detailed_status
job.detailed_status(request.current_user)
end
def path_to(route, job, params = {})
send("#{route}_path", job.project.namespace, job.project, job, params) # rubocop:disable GitlabSecurity/PublicSend
end
def job_path(job)
job.target_url || path_to(:namespace_project_job, job)
end
def failed?
job.failed?
end
def callout_message
job_presenter.callout_failure_message
end
def recoverable
job_presenter.recoverable?
end
def job_presenter
@job_presenter ||= job.present
end
end
end
# frozen_string_literal: true
module Ci
class JobSerializer < BaseSerializer
entity Ci::JobEntity
def represent_status(resource)
data = represent(resource, { only: [:status] })
data.fetch(:status, {})
end
end
end
...@@ -82,7 +82,7 @@ class Ci::PipelineEntity < Grape::Entity ...@@ -82,7 +82,7 @@ class Ci::PipelineEntity < Grape::Entity
project_pipeline_path(pipeline.project, pipeline) project_pipeline_path(pipeline.project, pipeline)
end end
expose :failed_builds, if: -> (*) { can_retry? }, using: JobEntity do |pipeline| expose :failed_builds, if: -> (*) { can_retry? }, using: Ci::JobEntity do |pipeline|
pipeline.failed_builds.each do |build| pipeline.failed_builds.each do |build|
build.project = pipeline.project build.project = pipeline.project
end end
......
...@@ -27,7 +27,7 @@ class DeploymentEntity < Grape::Entity ...@@ -27,7 +27,7 @@ class DeploymentEntity < Grape::Entity
expose :deployable, if: -> (deployment) { deployment.deployable.present? } do |deployment, opts| expose :deployable, if: -> (deployment) { deployment.deployable.present? } do |deployment, opts|
deployment.deployable.yield_self do |deployable| deployment.deployable.yield_self do |deployable|
if include_details? if include_details?
JobEntity.represent(deployable, opts) Ci::JobEntity.represent(deployable, opts)
elsif can_read_deployables? elsif can_read_deployables?
{ name: deployable.name, { name: deployable.name,
build_path: project_job_path(deployable.project, deployable) } build_path: project_job_path(deployable.project, deployable) }
...@@ -36,10 +36,10 @@ class DeploymentEntity < Grape::Entity ...@@ -36,10 +36,10 @@ class DeploymentEntity < Grape::Entity
end end
expose :commit, using: CommitEntity, if: -> (*) { include_details? } expose :commit, using: CommitEntity, if: -> (*) { include_details? }
expose :manual_actions, using: JobEntity, if: -> (*) { include_details? && can_create_deployment? } expose :manual_actions, using: Ci::JobEntity, if: -> (*) { include_details? && can_create_deployment? }
expose :scheduled_actions, using: JobEntity, if: -> (*) { include_details? && can_create_deployment? } expose :scheduled_actions, using: Ci::JobEntity, if: -> (*) { include_details? && can_create_deployment? }
expose :playable_build, if: -> (deployment) { include_details? && can_create_deployment? && deployment.playable_build } do |deployment, options| expose :playable_build, if: -> (deployment) { include_details? && can_create_deployment? && deployment.playable_build } do |deployment, options|
JobEntity.represent(deployment.playable_build, options.merge(only: [:play_path, :retry_path])) Ci::JobEntity.represent(deployment.playable_build, options.merge(only: [:play_path, :retry_path]))
end end
expose :cluster do |deployment, options| expose :cluster do |deployment, options|
......
# frozen_string_literal: true
class JobEntity < Grape::Entity
include RequestAwareEntity
expose :id
expose :name
expose :started?, as: :started
expose :complete?, as: :complete
expose :archived?, as: :archived
# bridge jobs don't have build detail pages
expose :build_path, if: ->(build) { !build.is_a?(Ci::Bridge) } do |build|
build_path(build)
end
expose :retry_path, if: -> (*) { retryable? } do |build|
path_to(:retry_namespace_project_job, build)
end
expose :cancel_path, if: -> (*) { cancelable? } do |build|
path_to(
:cancel_namespace_project_job,
build,
{ continue: { to: build_path(build) } }
)
end
expose :play_path, if: -> (*) { playable? } do |build|
path_to(:play_namespace_project_job, build)
end
expose :unschedule_path, if: -> (*) { scheduled? } do |build|
path_to(:unschedule_namespace_project_job, build)
end
expose :playable?, as: :playable
expose :scheduled?, as: :scheduled
expose :scheduled_at, if: -> (*) { scheduled? }
expose :created_at
expose :updated_at
expose :detailed_status, as: :status, with: DetailedStatusEntity
expose :callout_message, if: -> (*) { failed? && !build.script_failure? }
expose :recoverable, if: -> (*) { failed? }
private
alias_method :build, :object
def cancelable?
build.cancelable? && can?(request.current_user, :update_build, build)
end
def retryable?
build.retryable? && can?(request.current_user, :update_build, build)
end
def playable?
build.playable? && can?(request.current_user, :update_build, build)
end
def scheduled?
build.scheduled?
end
def detailed_status
build.detailed_status(request.current_user)
end
def path_to(route, build, params = {})
send("#{route}_path", build.project.namespace, build.project, build, params) # rubocop:disable GitlabSecurity/PublicSend
end
def build_path(build)
build.target_url || path_to(:namespace_project_job, build)
end
def failed?
build.failed?
end
def callout_message
build_presenter.callout_failure_message
end
def recoverable
build_presenter.recoverable?
end
def build_presenter
@build_presenter ||= build.present
end
end
...@@ -6,7 +6,7 @@ class JobGroupEntity < Grape::Entity ...@@ -6,7 +6,7 @@ class JobGroupEntity < Grape::Entity
expose :name expose :name
expose :size expose :size
expose :detailed_status, as: :status, with: DetailedStatusEntity expose :detailed_status, as: :status, with: DetailedStatusEntity
expose :jobs, with: JobEntity expose :jobs, with: Ci::JobEntity
private private
......
...@@ -15,13 +15,13 @@ class StageEntity < Grape::Entity ...@@ -15,13 +15,13 @@ class StageEntity < Grape::Entity
expose :latest_statuses, expose :latest_statuses,
if: -> (_, opts) { opts[:details] }, if: -> (_, opts) { opts[:details] },
with: JobEntity do |stage| with: Ci::JobEntity do |stage|
latest_statuses latest_statuses
end end
expose :retried, expose :retried,
if: -> (_, opts) { opts[:retried] }, if: -> (_, opts) { opts[:retried] },
with: JobEntity do |stage| with: Ci::JobEntity do |stage|
retried_statuses retried_statuses
end end
......
# frozen_string_literal: true # frozen_string_literal: true
require 'spec_helper' require 'spec_helper'
RSpec.describe JobEntity do RSpec.describe Ci::JobEntity do
using RSpec::Parameterized::TableSyntax using RSpec::Parameterized::TableSyntax
let_it_be(:group) { create(:group) } let_it_be(:group) { create(:group) }
......
...@@ -5,8 +5,8 @@ require 'spec_helper' ...@@ -5,8 +5,8 @@ require 'spec_helper'
RSpec.describe BuildDetailsEntity do RSpec.describe BuildDetailsEntity do
include ProjectForksHelper include ProjectForksHelper
it 'inherits from JobEntity' do it 'inherits from Ci::JobEntity' do
expect(described_class).to be < JobEntity expect(described_class).to be < Ci::JobEntity
end end
describe '#as_json' do describe '#as_json' do
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe JobEntity do RSpec.describe Ci::JobEntity do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:job) { create(:ci_build) } let(:job) { create(:ci_build) }
let(:project) { job.project } let(:project) { job.project }
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe BuildSerializer do RSpec.describe Ci::JobSerializer do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:serializer) do let(:serializer) 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