debian-mirror-gitlab/lib/gitlab/data_builder/pipeline.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

157 lines
4.7 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2016-09-13 17:45:13 +05:30
module Gitlab
module DataBuilder
2021-10-27 15:23:28 +05:30
# Some callers want to include retried builds, so we wrap the payload hash
# in a SimpleDelegator with additional methods.
class Pipeline < SimpleDelegator
def self.build(pipeline)
new(pipeline)
end
2016-09-13 17:45:13 +05:30
2021-10-27 15:23:28 +05:30
def initialize(pipeline)
@pipeline = pipeline
2022-07-23 23:45:48 +05:30
attrs = {
2016-09-13 17:45:13 +05:30
object_kind: 'pipeline',
object_attributes: hook_attrs(pipeline),
2019-12-04 20:38:33 +05:30
merge_request: pipeline.merge_request && merge_request_attrs(pipeline.merge_request),
2016-09-13 17:45:13 +05:30
user: pipeline.user.try(:hook_attrs),
project: pipeline.project.hook_attrs(backward: false),
commit: pipeline.commit.try(:hook_attrs),
2021-10-27 15:23:28 +05:30
builds: Gitlab::Lazy.new do
preload_builds(pipeline, :latest_builds)
pipeline.latest_builds.map(&method(:build_hook_attrs))
end
2022-07-23 23:45:48 +05:30
}
if pipeline.source_pipeline.present?
attrs[:source_pipeline] = source_pipeline_attrs(pipeline.source_pipeline)
end
super(attrs)
2021-10-27 15:23:28 +05:30
end
def with_retried_builds
merge(
builds: Gitlab::Lazy.new do
preload_builds(@pipeline, :builds)
@pipeline.builds.map(&method(:build_hook_attrs))
end
)
end
private
# rubocop: disable CodeReuse/ActiveRecord
def preload_builds(pipeline, association)
ActiveRecord::Associations::Preloader.new.preload(pipeline,
{
association => {
**::Ci::Pipeline::PROJECT_ROUTE_AND_NAMESPACE_ROUTE,
runner: :tags,
job_artifacts_archive: [],
user: [],
metadata: []
}
}
)
2016-09-13 17:45:13 +05:30
end
2021-10-27 15:23:28 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2016-09-13 17:45:13 +05:30
def hook_attrs(pipeline)
{
id: pipeline.id,
2019-07-07 11:18:12 +05:30
ref: pipeline.source_ref,
2016-09-13 17:45:13 +05:30
tag: pipeline.tag,
sha: pipeline.sha,
before_sha: pipeline.before_sha,
2019-12-04 20:38:33 +05:30
source: pipeline.source,
2016-09-13 17:45:13 +05:30
status: pipeline.status,
2018-03-27 19:54:05 +05:30
detailed_status: pipeline.detailed_status(nil).label,
2017-09-10 17:25:29 +05:30
stages: pipeline.stages_names,
2016-09-13 17:45:13 +05:30
created_at: pipeline.created_at,
finished_at: pipeline.finished_at,
2018-12-05 23:21:45 +05:30
duration: pipeline.duration,
2021-06-08 01:23:25 +05:30
queued_duration: pipeline.queued_duration,
2018-12-05 23:21:45 +05:30
variables: pipeline.variables.map(&:hook_attrs)
2016-09-13 17:45:13 +05:30
}
end
2022-07-23 23:45:48 +05:30
def source_pipeline_attrs(source_pipeline)
project = source_pipeline.source_project
{
project: {
id: project.id,
web_url: project.web_url,
path_with_namespace: project.full_path
},
job_id: source_pipeline.source_job_id,
pipeline_id: source_pipeline.source_pipeline_id
}
end
2019-12-04 20:38:33 +05:30
def merge_request_attrs(merge_request)
{
id: merge_request.id,
iid: merge_request.iid,
title: merge_request.title,
source_branch: merge_request.source_branch,
source_project_id: merge_request.source_project_id,
target_branch: merge_request.target_branch,
target_project_id: merge_request.target_project_id,
state: merge_request.state,
2020-04-22 19:07:51 +05:30
merge_status: merge_request.public_merge_status,
2019-12-04 20:38:33 +05:30
url: Gitlab::UrlBuilder.build(merge_request)
}
end
2016-09-13 17:45:13 +05:30
def build_hook_attrs(build)
{
id: build.id,
stage: build.stage,
name: build.name,
status: build.status,
created_at: build.created_at,
started_at: build.started_at,
finished_at: build.finished_at,
2021-06-08 01:23:25 +05:30
duration: build.duration,
queued_duration: build.queued_duration,
2016-09-13 17:45:13 +05:30
when: build.when,
2017-08-17 22:00:37 +05:30
manual: build.action?,
2020-01-01 13:55:28 +05:30
allow_failure: build.allow_failure,
2016-09-13 17:45:13 +05:30
user: build.user.try(:hook_attrs),
runner: build.runner && runner_hook_attrs(build.runner),
artifacts_file: {
2019-07-07 11:18:12 +05:30
filename: build.artifacts_file&.filename,
2016-09-13 17:45:13 +05:30
size: build.artifacts_size
2021-04-17 20:07:23 +05:30
},
environment: environment_hook_attrs(build)
2016-09-13 17:45:13 +05:30
}
end
def runner_hook_attrs(runner)
{
id: runner.id,
description: runner.description,
2021-09-04 01:27:46 +05:30
runner_type: runner.runner_type,
2016-09-13 17:45:13 +05:30
active: runner.active?,
2021-09-04 01:27:46 +05:30
is_shared: runner.instance_type?,
2021-03-11 19:13:27 +05:30
tags: runner.tags&.map(&:name)
2016-09-13 17:45:13 +05:30
}
end
2021-04-17 20:07:23 +05:30
def environment_hook_attrs(build)
return unless build.has_environment?
{
name: build.expanded_environment_name,
2021-10-27 15:23:28 +05:30
action: build.environment_action,
deployment_tier: build.persisted_environment.try(:tier)
2021-04-17 20:07:23 +05:30
}
end
2016-09-13 17:45:13 +05:30
end
end
end