BigW Consortium Gitlab

pipeline_entity.rb 1.85 KB
Newer Older
1 2 3 4
class PipelineEntity < Grape::Entity
  include RequestAwareEntity

  expose :id
5
  expose :user, using: UserEntity
6 7
  expose :active?, as: :active
  expose :coverage
8
  expose :source
9

10 11
  expose :created_at, :updated_at

12
  expose :path do |pipeline|
13
    project_pipeline_path(pipeline.project, pipeline)
14
  end
15

Z.J. van de Weg committed
16 17 18
  expose :flags do
    expose :latest?, as: :latest
    expose :stuck?, as: :stuck
19
    expose :auto_devops_source?, as: :auto_devops
Z.J. van de Weg committed
20 21 22
    expose :has_yaml_errors?, as: :yaml_errors
    expose :can_retry?, as: :retryable
    expose :can_cancel?, as: :cancelable
23
    expose :failure_reason?, as: :failure_reason
Z.J. van de Weg committed
24 25 26 27 28 29 30 31
  end

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

32
  expose :ref do
33 34 35 36
    expose :name do |pipeline|
      pipeline.ref
    end

37
    expose :path do |pipeline|
38
      if pipeline.ref
39
        project_ref_path(pipeline.project, pipeline.ref)
40
      end
41 42
    end

43 44
    expose :tag?, as: :tag
    expose :branch?, as: :branch
45 46
  end

47
  expose :commit, using: CommitEntity
48 49 50 51 52
  expose :yaml_errors, if: -> (pipeline, _) { pipeline.has_yaml_errors? }

  expose :failure_reason, if: -> (pipeline, _) { pipeline.failure_reason? } do |pipeline|
    pipeline.present.failure_reason
  end
53

54
  expose :retry_path, if: -> (*) { can_retry? }  do |pipeline|
55
    retry_project_pipeline_path(pipeline.project, pipeline)
56 57
  end

58
  expose :cancel_path, if: -> (*) { can_cancel? } do |pipeline|
59
    cancel_project_pipeline_path(pipeline.project, pipeline)
60 61
  end

62 63 64 65 66
  private

  alias_method :pipeline, :object

  def can_retry?
67
    can?(request.current_user, :update_pipeline, pipeline) &&
68
      pipeline.retryable?
69 70 71
  end

  def can_cancel?
72
    can?(request.current_user, :update_pipeline, pipeline) &&
73
      pipeline.cancelable?
74
  end
75 76

  def detailed_status
77
    pipeline.detailed_status(request.current_user)
78
  end
79
end