pipeline_entity.rb 2.1 KB
Newer Older
Kamil Trzcinski's avatar
Kamil Trzcinski committed
1 2 3 4
class PipelineEntity < Grape::Entity
  include RequestAwareEntity

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

  # 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] }
12
  expose :source
13

14 15
  expose :created_at, :updated_at

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

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

  expose :details do
    expose :detailed_status, as: :status, with: StatusEntity
    expose :duration
    expose :finished_at
  end

36
  expose :ref do
Kamil Trzcinski's avatar
Kamil Trzcinski committed
37 38 39 40
    expose :name do |pipeline|
      pipeline.ref
    end

41
    expose :path do |pipeline|
42
      if pipeline.ref
43
        project_ref_path(pipeline.project, pipeline.ref)
44
      end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
45 46
    end

47 48
    expose :tag?, as: :tag
    expose :branch?, as: :branch
Kamil Trzcinski's avatar
Kamil Trzcinski committed
49 50
  end

51
  expose :commit, using: CommitEntity
52 53 54 55 56
  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
57

58
  expose :retry_path, if: -> (*) { can_retry? }  do |pipeline|
59
    retry_project_pipeline_path(pipeline.project, pipeline)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
60 61
  end

62
  expose :cancel_path, if: -> (*) { can_cancel? } do |pipeline|
63
    cancel_project_pipeline_path(pipeline.project, pipeline)
64 65
  end

66 67 68 69 70
  private

  alias_method :pipeline, :object

  def can_retry?
Fatih Acet's avatar
Fatih Acet committed
71
    can?(request.current_user, :update_pipeline, pipeline) &&
72
      pipeline.retryable?
73 74 75
  end

  def can_cancel?
Fatih Acet's avatar
Fatih Acet committed
76
    can?(request.current_user, :update_pipeline, pipeline) &&
77
      pipeline.cancelable?
78
  end
79 80

  def detailed_status
Fatih Acet's avatar
Fatih Acet committed
81
    pipeline.detailed_status(request.current_user)
82
  end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
83
end