summaryrefslogtreecommitdiff
path: root/app/serializers/pipeline_entity.rb
blob: 6191d63fd7f2a8e0024606a5d7501ebdb83fb5d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
class PipelineEntity < Grape::Entity
  include RequestAwareEntity

  expose :id
  expose :user, if: -> (pipeline, opts) { created?(pipeline, opts) }, using: UserEntity
  expose :url do |pipeline|
    namespace_project_pipeline_path(
      pipeline.project.namespace,
      pipeline.project,
      pipeline)
  end

  expose :details, if: -> (pipeline, opts) { updated?(pipeline, opts) } do
    expose :status
    expose :duration
    expose :finished_at
    expose :stages_with_statuses, as: :stages, using: PipelineStageEntity
    expose :artifacts, using: PipelineArtifactEntity
    expose :manual_actions, using: PipelineActionEntity
  end

  expose :flags, if: -> (pipeline, opts) { created?(pipeline, opts) } do
    expose :latest?, as: :latest
    expose :triggered?, as: :triggered
    expose :yaml_errors?, as: :yaml_errors do |pipeline|
      pipeline.yaml_errors.present?
    end
    expose :stuck?, as: :stuck do |pipeline|
      pipeline.builds.any?(&:stuck?)
    end
  end

  expose :ref, if: -> (pipeline, opts) { created?(pipeline, opts) } do
    expose :name do |pipeline|
      pipeline.ref
    end

    expose :url do |pipeline|
      namespace_project_tree_url(
        pipeline.project.namespace,
        pipeline.project,
        id: pipeline.ref)
    end

    expose :tag?
  end

  expose :commit, if: -> (pipeline, opts) { created?(pipeline, opts) }, using: CommitEntity

  expose :retry_url, if: -> (pipeline, opts) { updated?(pipeline, opts) } do |pipeline|
    can?(current_user, :update_pipeline, pipeline.project) &&
      pipeline.retryable? &&
      retry_namespace_project_pipeline_path(pipeline.project.namespace, pipeline.project, pipeline.id)
  end

  expose :cancel_url, if: -> (pipeline, opts) { updated?(pipeline, opts) } do |pipeline|
    can?(current_user, :update_pipeline, pipeline.project) &&
      pipeline.cancelable? &&
      cancel_namespace_project_pipeline_path(pipeline.project.namespace, pipeline.project, pipeline.id)
  end

  private

  def last_updated(opts)
    opts.fetch(:last_updated)
  end

  def created?(pipeline, opts)
    !last_updated(opts) || pipeline.created_at > last_updated(opts)
  end

  def updated?(pipeline, opts)
    !last_updated(opts) || pipeline.updated_at > last_updated(opts)
  end
end