gitlab-org--gitlab-foss/app/serializers/job_entity.rb

94 lines
2.1 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2017-06-07 05:36:41 -04:00
class JobEntity < Grape::Entity
include RequestAwareEntity
expose :id
expose :name
expose :started?, as: :started
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|
2017-06-02 14:32:37 -04:00
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
2018-10-03 02:17:12 -04:00
expose :unschedule_path, if: -> (*) { scheduled? } do |build|
path_to(:unschedule_namespace_project_job, build)
end
expose :playable?, as: :playable
2018-11-02 07:52:34 -04:00
expose :scheduled?, as: :scheduled
2018-10-03 02:17:12 -04:00
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?
2017-05-09 00:15:34 -04:00
build.playable? && can?(request.current_user, :update_build, build)
end
2018-10-03 02:17:12 -04:00
def scheduled?
build.scheduled?
end
def detailed_status
2017-05-09 00:15:34 -04:00
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