BigW Consortium Gitlab

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

  expose :id
5
  expose :user, using: UserEntity
6

7
  expose :path do |pipeline|
8 9 10 11 12
    namespace_project_pipeline_path(
      pipeline.project.namespace,
      pipeline.project,
      pipeline)
  end
13

14
  expose :details do
15
    expose :detailed_status, as: :status, with: StatusEntity
16 17
    expose :duration
    expose :finished_at
18 19 20
    expose :stages, using: StageEntity
    expose :artifacts, using: BuildArtifactEntity
    expose :manual_actions, using: BuildActionEntity
21
  end
22

23
  expose :flags do
24 25
    expose :latest?, as: :latest
    expose :triggered?, as: :triggered
26
    expose :stuck?, as: :stuck
27 28 29
    expose :has_yaml_errors?, as: :yaml_errors
    expose :can_retry?, as: :retryable
    expose :can_cancel?, as: :cancelable
30 31
  end

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

37
    expose :path do |pipeline|
38 39 40 41 42 43
      if pipeline.ref
        namespace_project_tree_path(
          pipeline.project.namespace,
          pipeline.project,
          id: pipeline.ref)
      end
44 45
    end

46 47
    expose :tag?, as: :tag
    expose :branch?, as: :branch
48 49
  end

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

53 54 55 56
  expose :retry_path, if: proc { can_retry? }  do |pipeline|
    retry_namespace_project_pipeline_path(pipeline.project.namespace,
                                          pipeline.project,
                                          pipeline.id)
57 58
  end

59 60 61 62
  expose :cancel_path, if: proc { can_cancel? } do |pipeline|
    cancel_namespace_project_pipeline_path(pipeline.project.namespace,
                                           pipeline.project,
                                           pipeline.id)
63 64
  end

65
  expose :created_at, :updated_at
66 67 68 69 70 71

  private

  alias_method :pipeline, :object

  def can_retry?
72 73
    can?(request.user, :update_pipeline, pipeline) &&
      pipeline.retryable?
74 75 76
  end

  def can_cancel?
77 78
    can?(request.user, :update_pipeline, pipeline) &&
      pipeline.cancelable?
79
  end
80 81 82 83

  def detailed_status
    pipeline.detailed_status(request.user)
  end
84
end