BigW Consortium Gitlab

pipeline_entity.rb 2.09 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 16 17 18 19 20
    expose :status do |pipeline, options|
      StatusEntity.represent(
        pipeline.detailed_status(request.user),
        options)
    end

21 22
    expose :duration
    expose :finished_at
23 24 25
    expose :stages, using: StageEntity
    expose :artifacts, using: BuildArtifactEntity
    expose :manual_actions, using: BuildActionEntity
26
  end
27

28
  expose :flags do
29 30
    expose :latest?, as: :latest
    expose :triggered?, as: :triggered
31
    expose :stuck?, as: :stuck
32 33 34
    expose :has_yaml_errors?, as: :yaml_errors
    expose :can_retry?, as: :retryable
    expose :can_cancel?, as: :cancelable
35 36
  end

37
  expose :ref do
38 39 40 41
    expose :name do |pipeline|
      pipeline.ref
    end

42
    expose :path do |pipeline|
43 44 45 46 47 48
      if pipeline.ref
        namespace_project_tree_path(
          pipeline.project.namespace,
          pipeline.project,
          id: pipeline.ref)
      end
49 50
    end

51 52
    expose :tag?, as: :tag
    expose :branch?, as: :branch
53 54
  end

55
  expose :commit, using: CommitEntity
56
  expose :yaml_errors, if: ->(pipeline, _) { pipeline.has_yaml_errors? }
57

58 59 60 61
  expose :retry_path, if: proc { can_retry? }  do |pipeline|
    retry_namespace_project_pipeline_path(pipeline.project.namespace,
                                          pipeline.project,
                                          pipeline.id)
62 63
  end

64 65 66 67
  expose :cancel_path, if: proc { can_cancel? } do |pipeline|
    cancel_namespace_project_pipeline_path(pipeline.project.namespace,
                                           pipeline.project,
                                           pipeline.id)
68 69
  end

70
  expose :created_at, :updated_at
71 72 73 74 75 76 77 78 79 80 81 82 83 84

  private

  alias_method :pipeline, :object

  def can_retry?
    pipeline.retryable? &&
      can?(request.user, :update_pipeline, pipeline)
  end

  def can_cancel?
    pipeline.cancelable? &&
      can?(request.user, :update_pipeline, pipeline)
  end
85
end