summaryrefslogtreecommitdiff
path: root/app/serializers/ci/job_entity.rb
blob: b6b11e54a1678111153bc6ea3ae4a307b4828678 (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
91
92
93
94
95
96
97
# frozen_string_literal: true

module Ci
  class JobEntity < Grape::Entity
    include RequestAwareEntity

    expose :id
    expose :name

    expose :started?, as: :started
    expose :started_at, if: -> (job) { job.started? }
    expose :complete?, as: :complete
    expose :archived?, as: :archived

    # bridge jobs don't have build details pages
    expose :build_path, if: ->(job) { !job.is_a?(Ci::Bridge) } do |job|
      job_path(job)
    end

    expose :retry_path, if: -> (*) { retryable? } do |job|
      path_to(:retry_namespace_project_job, job)
    end

    expose :cancel_path, if: -> (*) { cancelable? } do |job|
      path_to(
        :cancel_namespace_project_job,
        job,
        { continue: { to: job_path(job) } }
      )
    end

    expose :play_path, if: -> (*) { playable? } do |job|
      path_to(:play_namespace_project_job, job)
    end

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

    expose :playable?, as: :playable
    expose :scheduled?, as: :scheduled
    expose :scheduled_at, if: -> (*) { scheduled? }
    expose :created_at
    expose :updated_at
    expose :detailed_status, as: :status, with: DetailedStatusEntity
    expose :callout_message, if: -> (*) { failed? && !job.script_failure? }
    expose :recoverable, if: -> (*) { failed? }

    private

    alias_method :job, :object

    def cancelable?
      job.cancelable? && can?(request.current_user, :update_build, job)
    end

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

    def playable?
      job.playable? && can?(request.current_user, :update_build, job)
    end

    def scheduled?
      job.scheduled?
    end

    def detailed_status
      job.detailed_status(request.current_user)
    end

    def path_to(route, job, params = {})
      send("#{route}_path", job.project.namespace, job.project, job, params) # rubocop:disable GitlabSecurity/PublicSend
    end

    def job_path(job)
      job.target_url || path_to(:namespace_project_job, job)
    end

    def failed?
      job.failed?
    end

    def callout_message
      job_presenter.callout_failure_message
    end

    def recoverable
      job_presenter.recoverable?
    end

    def job_presenter
      @job_presenter ||= job.present
    end
  end
end