pipeline_entity.rb 2.93 KB
Newer Older
1 2
# frozen_string_literal: true

Kamil Trzcinski's avatar
Kamil Trzcinski committed
3 4 5 6
class PipelineEntity < Grape::Entity
  include RequestAwareEntity

  expose :id
7
  expose :user, using: UserEntity
Fatih Acet's avatar
Fatih Acet committed
8
  expose :active?, as: :active
9 10 11 12 13

  # Coverage isn't always necessary (e.g. when displaying project pipelines in
  # the UI). Instead of creating an entirely different entity we just allow the
  # disabling of this specific field whenever necessary.
  expose :coverage, unless: proc { options[:disable_coverage] }
14
  expose :source
15

16 17
  expose :created_at, :updated_at

18
  expose :path do |pipeline|
19
    project_pipeline_path(pipeline.project, pipeline)
20
  end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
21

Z.J. van de Weg's avatar
Z.J. van de Weg committed
22 23
  expose :flags do
    expose :stuck?, as: :stuck
24
    expose :auto_devops_source?, as: :auto_devops
25
    expose :merge_request_event?, as: :merge_request
Z.J. van de Weg's avatar
Z.J. van de Weg committed
26 27 28
    expose :has_yaml_errors?, as: :yaml_errors
    expose :can_retry?, as: :retryable
    expose :can_cancel?, as: :cancelable
29
    expose :failure_reason?, as: :failure_reason
30 31
    expose :detached_merge_request_pipeline?, as: :detached_merge_request_pipeline
    expose :merge_request_pipeline?, as: :merge_request_pipeline
Z.J. van de Weg's avatar
Z.J. van de Weg committed
32 33 34
  end

  expose :details do
35
    expose :detailed_status, as: :status, with: DetailedStatusEntity
36
    expose :ordered_stages, as: :stages, using: StageEntity
Z.J. van de Weg's avatar
Z.J. van de Weg committed
37 38 39 40
    expose :duration
    expose :finished_at
  end

41 42 43 44
  expose :merge_request, if: -> (*) { has_presentable_merge_request? }, with: MergeRequestForPipelineEntity do |pipeline|
    pipeline.merge_request.present(current_user: request.current_user)
  end

45
  expose :ref do
Kamil Trzcinski's avatar
Kamil Trzcinski committed
46 47 48 49
    expose :name do |pipeline|
      pipeline.ref
    end

50
    expose :path do |pipeline|
51
      if pipeline.ref
52
        project_ref_path(pipeline.project, pipeline.ref)
53
      end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
54 55
    end

56 57
    expose :tag?, as: :tag
    expose :branch?, as: :branch
58
    expose :merge_request_event?, as: :merge_request
Kamil Trzcinski's avatar
Kamil Trzcinski committed
59 60
  end

61
  expose :commit, using: CommitEntity
Shinya Maeda's avatar
Shinya Maeda committed
62 63
  expose :source_sha, if: -> (pipeline, _) { pipeline.merge_request_pipeline? }
  expose :target_sha, if: -> (pipeline, _) { pipeline.merge_request_pipeline? }
64 65 66 67 68
  expose :yaml_errors, if: -> (pipeline, _) { pipeline.has_yaml_errors? }

  expose :failure_reason, if: -> (pipeline, _) { pipeline.failure_reason? } do |pipeline|
    pipeline.present.failure_reason
  end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
69

70
  expose :retry_path, if: -> (*) { can_retry? } do |pipeline|
71
    retry_project_pipeline_path(pipeline.project, pipeline)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
72 73
  end

74
  expose :cancel_path, if: -> (*) { can_cancel? } do |pipeline|
75
    cancel_project_pipeline_path(pipeline.project, pipeline)
76 77
  end

78 79 80 81 82
  private

  alias_method :pipeline, :object

  def can_retry?
Fatih Acet's avatar
Fatih Acet committed
83
    can?(request.current_user, :update_pipeline, pipeline) &&
84
      pipeline.retryable?
85 86 87
  end

  def can_cancel?
Fatih Acet's avatar
Fatih Acet committed
88
    can?(request.current_user, :update_pipeline, pipeline) &&
89
      pipeline.cancelable?
90
  end
91

92 93 94 95 96
  def has_presentable_merge_request?
    pipeline.triggered_by_merge_request? &&
      can?(request.current_user, :read_merge_request, pipeline.merge_request)
  end

97
  def detailed_status
Fatih Acet's avatar
Fatih Acet committed
98
    pipeline.detailed_status(request.current_user)
99
  end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
100
end