debian-mirror-gitlab/app/serializers/pipeline_entity.rb

121 lines
3.5 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
class PipelineEntity < Grape::Entity
include RequestAwareEntity
2019-12-04 20:38:33 +05:30
include Gitlab::Utils::StrongMemoize
delegate :name, :failure_reason, to: :presented_pipeline
2017-08-17 22:00:37 +05:30
expose :id
expose :user, using: UserEntity
expose :active?, as: :active
2018-11-08 19:23:39 +05:30
# Coverage isn't always necessary (e.g. when displaying project pipelines in
# the UI). Instead of creating an entirely different entity we just allow the
# disabling of this specific field whenever necessary.
expose :coverage, unless: proc { options[:disable_coverage] }
2017-09-10 17:25:29 +05:30
expose :source
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
expose :created_at, :updated_at
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
expose :path do |pipeline|
project_pipeline_path(pipeline.project, pipeline)
2017-08-17 22:00:37 +05:30
end
expose :flags do
expose :stuck?, as: :stuck
2018-03-17 18:26:18 +05:30
expose :auto_devops_source?, as: :auto_devops
2020-03-13 15:44:24 +05:30
expose :merge_request?, as: :merge_request
2017-08-17 22:00:37 +05:30
expose :has_yaml_errors?, as: :yaml_errors
expose :can_retry?, as: :retryable
expose :can_cancel?, as: :cancelable
2018-03-17 18:26:18 +05:30
expose :failure_reason?, as: :failure_reason
2019-07-07 11:18:12 +05:30
expose :detached_merge_request_pipeline?, as: :detached_merge_request_pipeline
expose :merge_request_pipeline?, as: :merge_request_pipeline
2017-08-17 22:00:37 +05:30
end
2017-09-10 17:25:29 +05:30
expose :details do
2018-12-05 23:21:45 +05:30
expose :detailed_status, as: :status, with: DetailedStatusEntity
2019-09-04 21:01:54 +05:30
expose :ordered_stages, as: :stages, using: StageEntity
2017-09-10 17:25:29 +05:30
expose :duration
expose :finished_at
2019-12-04 20:38:33 +05:30
expose :name
2017-09-10 17:25:29 +05:30
end
2019-07-07 11:18:12 +05:30
expose :merge_request, if: -> (*) { has_presentable_merge_request? }, with: MergeRequestForPipelineEntity do |pipeline|
pipeline.merge_request.present(current_user: request.current_user)
end
2017-08-17 22:00:37 +05:30
expose :ref do
expose :name do |pipeline|
pipeline.ref
end
expose :path do |pipeline|
if pipeline.ref
2017-09-10 17:25:29 +05:30
project_ref_path(pipeline.project, pipeline.ref)
2017-08-17 22:00:37 +05:30
end
end
expose :tag?, as: :tag
expose :branch?, as: :branch
2020-03-13 15:44:24 +05:30
expose :merge_request?, as: :merge_request
2017-08-17 22:00:37 +05:30
end
expose :commit, using: CommitEntity
2020-03-13 15:44:24 +05:30
expose :merge_request_event_type, if: -> (pipeline, _) { pipeline.merge_request? }
2019-07-07 11:18:12 +05:30
expose :source_sha, if: -> (pipeline, _) { pipeline.merge_request_pipeline? }
expose :target_sha, if: -> (pipeline, _) { pipeline.merge_request_pipeline? }
2018-03-17 18:26:18 +05:30
expose :yaml_errors, if: -> (pipeline, _) { pipeline.has_yaml_errors? }
2019-12-04 20:38:33 +05:30
expose :failure_reason, if: -> (pipeline, _) { pipeline.failure_reason? }
2017-08-17 22:00:37 +05:30
2019-03-02 22:35:43 +05:30
expose :retry_path, if: -> (*) { can_retry? } do |pipeline|
2017-09-10 17:25:29 +05:30
retry_project_pipeline_path(pipeline.project, pipeline)
2017-08-17 22:00:37 +05:30
end
expose :cancel_path, if: -> (*) { can_cancel? } do |pipeline|
2017-09-10 17:25:29 +05:30
cancel_project_pipeline_path(pipeline.project, pipeline)
2017-08-17 22:00:37 +05:30
end
2020-03-13 15:44:24 +05:30
expose :delete_path, if: -> (*) { can_delete? } do |pipeline|
project_pipeline_path(pipeline.project, pipeline)
end
2020-01-01 13:55:28 +05:30
expose :failed_builds, if: -> (*) { can_retry? }, using: JobEntity do |pipeline|
pipeline.failed_builds
end
2017-08-17 22:00:37 +05:30
private
alias_method :pipeline, :object
def can_retry?
can?(request.current_user, :update_pipeline, pipeline) &&
pipeline.retryable?
end
def can_cancel?
can?(request.current_user, :update_pipeline, pipeline) &&
pipeline.cancelable?
end
2020-03-13 15:44:24 +05:30
def can_delete?
can?(request.current_user, :destroy_pipeline, pipeline)
end
2019-07-07 11:18:12 +05:30
def has_presentable_merge_request?
2020-03-13 15:44:24 +05:30
pipeline.merge_request? &&
2019-07-07 11:18:12 +05:30
can?(request.current_user, :read_merge_request, pipeline.merge_request)
end
2017-08-17 22:00:37 +05:30
def detailed_status
pipeline.detailed_status(request.current_user)
end
2019-12-04 20:38:33 +05:30
def presented_pipeline
strong_memoize(:presented_pipeline) do
pipeline.present
end
end
2017-08-17 22:00:37 +05:30
end