BigW Consortium Gitlab

pipeline_entity.rb 1.92 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 14 15 16 17
    namespace_project_pipeline_path(
      pipeline.project.namespace,
      pipeline.project,
      pipeline)
  end
18

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

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

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

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

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

48
  expose :commit, using: CommitEntity
49

50
  expose :retry_path, if: -> (*) { can_retry? }  do |pipeline|
51 52 53
    retry_namespace_project_pipeline_path(pipeline.project.namespace,
                                          pipeline.project,
                                          pipeline.id)
54 55
  end

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

Z.J. van de Weg committed
62 63
  expose :yaml_errors, if: -> (pipeline, _) { pipeline.has_yaml_errors? }

64 65 66 67 68
  private

  alias_method :pipeline, :object

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

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

  def detailed_status
79
    pipeline.detailed_status(request.current_user)
80
  end
81
end