2018-07-19 14:43:13 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-01-25 19:08:57 -05:00
|
|
|
class Ci::PipelineEntity < Grape::Entity
|
2016-11-10 09:32:23 -05:00
|
|
|
include RequestAwareEntity
|
2019-08-28 03:51:26 -04:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
|
|
|
delegate :name, :failure_reason, to: :presented_pipeline
|
2016-11-10 09:32:23 -05:00
|
|
|
|
|
|
|
expose :id
|
2021-08-03 11:10:03 -04:00
|
|
|
expose :iid
|
2016-12-01 05:59:00 -05:00
|
|
|
expose :user, using: UserEntity
|
2017-05-09 00:15:34 -04:00
|
|
|
expose :active?, as: :active
|
2018-05-11 10:17:03 -04:00
|
|
|
|
|
|
|
# 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-05-24 09:13:51 -04:00
|
|
|
expose :source
|
2016-11-10 15:16:54 -05:00
|
|
|
|
2017-05-26 04:31:42 -04:00
|
|
|
expose :created_at, :updated_at
|
|
|
|
|
2016-12-15 09:57:35 -05:00
|
|
|
expose :path do |pipeline|
|
2017-06-29 13:06:35 -04:00
|
|
|
project_pipeline_path(pipeline.project, pipeline)
|
2016-11-10 12:15:16 -05:00
|
|
|
end
|
2016-11-10 09:32:23 -05:00
|
|
|
|
2017-05-29 03:58:20 -04:00
|
|
|
expose :flags do
|
|
|
|
expose :stuck?, as: :stuck
|
2017-09-06 09:14:21 -04:00
|
|
|
expose :auto_devops_source?, as: :auto_devops
|
2020-02-13 07:08:49 -05:00
|
|
|
expose :merge_request?, as: :merge_request
|
2017-05-29 03:58:20 -04:00
|
|
|
expose :has_yaml_errors?, as: :yaml_errors
|
|
|
|
expose :can_retry?, as: :retryable
|
|
|
|
expose :can_cancel?, as: :cancelable
|
2017-10-03 07:02:48 -04:00
|
|
|
expose :failure_reason?, as: :failure_reason
|
2019-03-07 02:33:13 -05:00
|
|
|
expose :detached_merge_request_pipeline?, as: :detached_merge_request_pipeline
|
2021-03-01 01:10:47 -05:00
|
|
|
expose :merged_result_pipeline?, as: :merge_request_pipeline
|
2017-05-29 03:58:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
expose :details do
|
2018-09-11 13:13:24 -04:00
|
|
|
expose :detailed_status, as: :status, with: DetailedStatusEntity
|
2020-08-13 11:10:03 -04:00
|
|
|
expose :stages, using: StageEntity
|
2017-05-29 03:58:20 -04:00
|
|
|
expose :duration
|
|
|
|
expose :finished_at
|
2019-08-28 03:51:26 -04:00
|
|
|
expose :name
|
2017-05-29 03:58:20 -04:00
|
|
|
end
|
|
|
|
|
2019-03-01 05:05:04 -05:00
|
|
|
expose :merge_request, if: -> (*) { has_presentable_merge_request? }, with: MergeRequestForPipelineEntity do |pipeline|
|
|
|
|
pipeline.merge_request.present(current_user: request.current_user)
|
|
|
|
end
|
|
|
|
|
2016-12-01 05:59:00 -05:00
|
|
|
expose :ref do
|
2016-11-10 09:32:23 -05:00
|
|
|
expose :name do |pipeline|
|
|
|
|
pipeline.ref
|
|
|
|
end
|
|
|
|
|
2016-12-21 08:26:47 -05:00
|
|
|
expose :path do |pipeline|
|
2017-01-20 06:44:31 -05:00
|
|
|
if pipeline.ref
|
2017-05-11 11:47:44 -04:00
|
|
|
project_ref_path(pipeline.project, pipeline.ref)
|
2017-01-20 06:44:31 -05:00
|
|
|
end
|
2016-11-10 09:32:23 -05:00
|
|
|
end
|
|
|
|
|
2016-12-21 08:26:47 -05:00
|
|
|
expose :tag?, as: :tag
|
|
|
|
expose :branch?, as: :branch
|
2020-02-13 07:08:49 -05:00
|
|
|
expose :merge_request?, as: :merge_request
|
2016-11-10 09:32:23 -05:00
|
|
|
end
|
|
|
|
|
2016-12-01 05:59:00 -05:00
|
|
|
expose :commit, using: CommitEntity
|
2020-02-13 07:08:49 -05:00
|
|
|
expose :merge_request_event_type, if: -> (pipeline, _) { pipeline.merge_request? }
|
2021-03-01 01:10:47 -05:00
|
|
|
expose :source_sha, if: -> (pipeline, _) { pipeline.merged_result_pipeline? }
|
|
|
|
expose :target_sha, if: -> (pipeline, _) { pipeline.merged_result_pipeline? }
|
2017-10-03 07:02:48 -04:00
|
|
|
expose :yaml_errors, if: -> (pipeline, _) { pipeline.has_yaml_errors? }
|
2019-08-28 03:51:26 -04:00
|
|
|
expose :failure_reason, if: -> (pipeline, _) { pipeline.failure_reason? }
|
2016-11-10 09:32:23 -05:00
|
|
|
|
2019-01-16 07:09:29 -05:00
|
|
|
expose :retry_path, if: -> (*) { can_retry? } do |pipeline|
|
2017-06-29 13:06:35 -04:00
|
|
|
retry_project_pipeline_path(pipeline.project, pipeline)
|
2016-11-10 09:32:23 -05:00
|
|
|
end
|
|
|
|
|
2017-05-03 06:32:39 -04:00
|
|
|
expose :cancel_path, if: -> (*) { can_cancel? } do |pipeline|
|
2017-06-29 13:06:35 -04:00
|
|
|
cancel_project_pipeline_path(pipeline.project, pipeline)
|
2016-11-10 15:16:54 -05:00
|
|
|
end
|
|
|
|
|
2020-01-08 07:07:59 -05:00
|
|
|
expose :delete_path, if: -> (*) { can_delete? } do |pipeline|
|
|
|
|
project_pipeline_path(pipeline.project, pipeline)
|
|
|
|
end
|
|
|
|
|
2019-11-29 04:06:31 -05:00
|
|
|
expose :failed_builds, if: -> (*) { can_retry? }, using: JobEntity do |pipeline|
|
2020-10-05 08:08:47 -04:00
|
|
|
pipeline.failed_builds.each do |build|
|
|
|
|
build.project = pipeline.project
|
|
|
|
end
|
2019-11-29 04:06:31 -05:00
|
|
|
end
|
|
|
|
|
2016-12-21 08:26:47 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
alias_method :pipeline, :object
|
|
|
|
|
|
|
|
def can_retry?
|
2017-05-09 00:15:34 -04:00
|
|
|
can?(request.current_user, :update_pipeline, pipeline) &&
|
2017-04-06 08:31:38 -04:00
|
|
|
pipeline.retryable?
|
2016-12-21 08:26:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_cancel?
|
2017-05-09 00:15:34 -04:00
|
|
|
can?(request.current_user, :update_pipeline, pipeline) &&
|
2017-04-06 08:31:38 -04:00
|
|
|
pipeline.cancelable?
|
2016-12-21 08:26:47 -05:00
|
|
|
end
|
2017-03-03 01:59:25 -05:00
|
|
|
|
2020-01-08 07:07:59 -05:00
|
|
|
def can_delete?
|
|
|
|
can?(request.current_user, :destroy_pipeline, pipeline)
|
|
|
|
end
|
|
|
|
|
2019-03-01 05:05:04 -05:00
|
|
|
def has_presentable_merge_request?
|
2020-02-13 07:08:49 -05:00
|
|
|
pipeline.merge_request? &&
|
2019-03-01 05:05:04 -05:00
|
|
|
can?(request.current_user, :read_merge_request, pipeline.merge_request)
|
|
|
|
end
|
|
|
|
|
2017-03-03 01:59:25 -05:00
|
|
|
def detailed_status
|
2017-05-09 00:15:34 -04:00
|
|
|
pipeline.detailed_status(request.current_user)
|
2017-03-03 01:59:25 -05:00
|
|
|
end
|
2019-08-28 03:51:26 -04:00
|
|
|
|
|
|
|
def presented_pipeline
|
|
|
|
strong_memoize(:presented_pipeline) do
|
|
|
|
pipeline.present
|
|
|
|
end
|
|
|
|
end
|
2016-11-10 09:32:23 -05:00
|
|
|
end
|
2021-01-26 01:09:07 -05:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
Ci::PipelineEntity.prepend_mod_with('Ci::PipelineEntity')
|