debian-mirror-gitlab/app/serializers/job_entity.rb

93 lines
2.1 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
class JobEntity < Grape::Entity
2017-08-17 22:00:37 +05:30
include RequestAwareEntity
expose :id
expose :name
2018-03-17 18:26:18 +05:30
expose :started?, as: :started
2018-12-13 13:39:08 +05:30
expose :archived?, as: :archived
2018-03-17 18:26:18 +05:30
2017-08-17 22:00:37 +05:30
expose :build_path do |build|
2018-12-13 13:39:08 +05:30
build_path(build)
2017-08-17 22:00:37 +05:30
end
2017-09-10 17:25:29 +05:30
expose :retry_path, if: -> (*) { retryable? } do |build|
path_to(:retry_namespace_project_job, build)
end
expose :cancel_path, if: -> (*) { cancelable? } do |build|
2018-12-13 13:39:08 +05:30
path_to(
:cancel_namespace_project_job,
build,
{ continue: { to: build_path(build) } }
)
2017-08-17 22:00:37 +05:30
end
expose :play_path, if: -> (*) { playable? } do |build|
2017-09-10 17:25:29 +05:30
path_to(:play_namespace_project_job, build)
2017-08-17 22:00:37 +05:30
end
2018-12-05 23:21:45 +05:30
expose :unschedule_path, if: -> (*) { scheduled? } do |build|
path_to(:unschedule_namespace_project_job, build)
end
2017-08-17 22:00:37 +05:30
expose :playable?, as: :playable
2018-12-13 13:39:08 +05:30
expose :scheduled?, as: :scheduled
2018-12-05 23:21:45 +05:30
expose :scheduled_at, if: -> (*) { scheduled? }
2017-08-17 22:00:37 +05:30
expose :created_at
expose :updated_at
2018-12-05 23:21:45 +05:30
expose :detailed_status, as: :status, with: DetailedStatusEntity
2018-11-08 19:23:39 +05:30
expose :callout_message, if: -> (*) { failed? && !build.script_failure? }
2018-10-15 14:42:47 +05:30
expose :recoverable, if: -> (*) { failed? }
2017-08-17 22:00:37 +05:30
private
alias_method :build, :object
2017-09-10 17:25:29 +05:30
def cancelable?
build.cancelable? && can?(request.current_user, :update_build, build)
end
def retryable?
build.retryable? && can?(request.current_user, :update_build, build)
end
2017-08-17 22:00:37 +05:30
def playable?
build.playable? && can?(request.current_user, :update_build, build)
end
2018-12-05 23:21:45 +05:30
def scheduled?
build.scheduled?
end
2017-08-17 22:00:37 +05:30
def detailed_status
build.detailed_status(request.current_user)
end
2018-12-13 13:39:08 +05:30
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)
2017-08-17 22:00:37 +05:30
end
2018-10-15 14:42:47 +05:30
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
2017-08-17 22:00:37 +05:30
end