2018-07-19 18:43:13 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-07 09:36:41 +00:00
|
|
|
class JobEntity < Grape::Entity
|
2016-11-03 11:54:10 +00:00
|
|
|
include RequestAwareEntity
|
|
|
|
|
|
|
|
expose :id
|
|
|
|
expose :name
|
|
|
|
|
2017-12-19 11:04:04 +00:00
|
|
|
expose :started?, as: :started
|
|
|
|
|
2016-11-17 15:08:05 +00:00
|
|
|
expose :build_path do |build|
|
2018-10-31 08:44:22 +00:00
|
|
|
build_path(build)
|
2016-11-03 11:54:10 +00:00
|
|
|
end
|
|
|
|
|
2017-06-12 09:20:19 +00:00
|
|
|
expose :retry_path, if: -> (*) { retryable? } do |build|
|
2017-06-02 18:32:37 +00:00
|
|
|
path_to(:retry_namespace_project_job, build)
|
2016-11-03 12:35:57 +00:00
|
|
|
end
|
|
|
|
|
2017-06-12 09:20:19 +00:00
|
|
|
expose :cancel_path, if: -> (*) { cancelable? } do |build|
|
2018-10-31 08:44:22 +00:00
|
|
|
path_to(
|
|
|
|
:cancel_namespace_project_job,
|
|
|
|
build,
|
|
|
|
{ continue: { to: build_path(build) } }
|
|
|
|
)
|
2017-06-12 09:20:19 +00:00
|
|
|
end
|
|
|
|
|
2017-05-03 10:32:39 +00:00
|
|
|
expose :play_path, if: -> (*) { playable? } do |build|
|
2017-05-16 11:41:15 +00:00
|
|
|
path_to(:play_namespace_project_job, build)
|
2016-11-03 12:35:57 +00:00
|
|
|
end
|
|
|
|
|
2018-10-03 06:17:12 +00:00
|
|
|
expose :unschedule_path, if: -> (*) { scheduled? } do |build|
|
|
|
|
path_to(:unschedule_namespace_project_job, build)
|
|
|
|
end
|
|
|
|
|
2017-03-17 16:25:17 +00:00
|
|
|
expose :playable?, as: :playable
|
2018-10-03 06:17:12 +00:00
|
|
|
expose :scheduled_at, if: -> (*) { scheduled? }
|
2016-11-29 10:57:16 +00:00
|
|
|
expose :created_at
|
|
|
|
expose :updated_at
|
2018-09-11 17:13:24 +00:00
|
|
|
expose :detailed_status, as: :status, with: DetailedStatusEntity
|
2018-05-30 13:18:18 +00:00
|
|
|
expose :callout_message, if: -> (*) { failed? && !build.script_failure? }
|
2018-04-19 07:20:53 +00:00
|
|
|
expose :recoverable, if: -> (*) { failed? }
|
2016-11-29 10:57:16 +00:00
|
|
|
|
2016-11-03 12:35:57 +00:00
|
|
|
private
|
|
|
|
|
2017-03-03 06:59:25 +00:00
|
|
|
alias_method :build, :object
|
|
|
|
|
2017-06-12 09:20:19 +00:00
|
|
|
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-04-06 10:58:13 +00:00
|
|
|
def playable?
|
2017-05-09 04:15:34 +00:00
|
|
|
build.playable? && can?(request.current_user, :update_build, build)
|
2016-11-03 11:54:10 +00:00
|
|
|
end
|
2017-03-03 06:59:25 +00:00
|
|
|
|
2018-10-03 06:17:12 +00:00
|
|
|
def scheduled?
|
|
|
|
build.scheduled?
|
|
|
|
end
|
|
|
|
|
2017-03-03 06:59:25 +00:00
|
|
|
def detailed_status
|
2017-05-09 04:15:34 +00:00
|
|
|
build.detailed_status(request.current_user)
|
2017-03-03 06:59:25 +00:00
|
|
|
end
|
2017-04-06 10:58:13 +00:00
|
|
|
|
2018-10-31 08:44:22 +00:00
|
|
|
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-04-06 10:58:13 +00:00
|
|
|
end
|
2018-04-19 07:20:53 +00:00
|
|
|
|
|
|
|
def failed?
|
2018-05-30 13:18:18 +00:00
|
|
|
build.failed?
|
2018-04-19 07:20:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def callout_message
|
|
|
|
build_presenter.callout_failure_message
|
|
|
|
end
|
|
|
|
|
|
|
|
def recoverable
|
|
|
|
build_presenter.recoverable?
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_presenter
|
|
|
|
@build_presenter ||= build.present
|
|
|
|
end
|
2016-11-03 11:54:10 +00:00
|
|
|
end
|