summaryrefslogtreecommitdiff
path: root/app/serializers/job_entity.rb
blob: a0a66511b7b87b49d86d000d93e80258eeb4b7c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# frozen_string_literal: true

class JobEntity < Grape::Entity
  include RequestAwareEntity

  expose :id
  expose :name

  expose :started?, as: :started

  expose :build_path do |build|
    build_path(build)
  end

  expose :retry_path, if: -> (*) { retryable? } do |build|
    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

  expose :unschedule_path, if: -> (*) { scheduled? } do |build|
    path_to(:unschedule_namespace_project_job, build)
  end

  expose :playable?, as: :playable
  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?
    build.playable? && can?(request.current_user, :update_build, build)
  end

  def scheduled?
    build.scheduled?
  end

  def detailed_status
    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