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