job_entity.rb 1.85 KB
Newer Older
1 2
# frozen_string_literal: true

3
class JobEntity < Grape::Entity
4 5 6 7 8
  include RequestAwareEntity

  expose :id
  expose :name

9 10
  expose :started?, as: :started

11
  expose :build_path do |build|
12
    build.target_url || path_to(:namespace_project_job, build)
13 14
  end

15
  expose :retry_path, if: -> (*) { retryable? } do |build|
Kamil Trzciński's avatar
Kamil Trzciński committed
16
    path_to(:retry_namespace_project_job, build)
17 18
  end

19 20 21 22
  expose :cancel_path, if: -> (*) { cancelable? } do |build|
    path_to(:cancel_namespace_project_job, build)
  end

23
  expose :play_path, if: -> (*) { playable? } do |build|
24
    path_to(:play_namespace_project_job, build)
25 26
  end

Shinya Maeda's avatar
Shinya Maeda committed
27 28 29 30
  expose :unschedule_path, if: -> (*) { scheduled? } do |build|
    path_to(:unschedule_namespace_project_job, build)
  end

31
  expose :playable?, as: :playable
Shinya Maeda's avatar
Shinya Maeda committed
32
  expose :scheduled_at, if: -> (*) { scheduled? }
33 34
  expose :created_at
  expose :updated_at
35
  expose :detailed_status, as: :status, with: DetailedStatusEntity
36
  expose :callout_message, if: -> (*) { failed? && !build.script_failure? }
37
  expose :recoverable, if: -> (*) { failed? }
38

39 40
  private

41 42
  alias_method :build, :object

43 44 45 46 47 48 49 50
  def cancelable?
    build.cancelable? && can?(request.current_user, :update_build, build)
  end

  def retryable?
    build.retryable? && can?(request.current_user, :update_build, build)
  end

51
  def playable?
52
    build.playable? && can?(request.current_user, :update_build, build)
53
  end
54

Shinya Maeda's avatar
Shinya Maeda committed
55 56 57 58
  def scheduled?
    build.scheduled?
  end

59
  def detailed_status
60
    build.detailed_status(request.current_user)
61
  end
62 63

  def path_to(route, build)
64
    send("#{route}_path", build.project.namespace, build.project, build) # rubocop:disable GitlabSecurity/PublicSend
65
  end
66 67

  def failed?
68
    build.failed?
69 70 71 72 73 74 75 76 77 78 79 80 81
  end

  def callout_message
    build_presenter.callout_failure_message
  end

  def recoverable
    build_presenter.recoverable?
  end

  def build_presenter
    @build_presenter ||= build.present
  end
82
end