debian-mirror-gitlab/app/presenters/ci/build_runner_presenter.rb

150 lines
4.2 KiB
Ruby
Raw Normal View History

2018-11-20 20:47:30 +05:30
# frozen_string_literal: true
2018-11-18 11:00:15 +05:30
module Ci
class BuildRunnerPresenter < SimpleDelegator
2019-07-07 11:18:12 +05:30
include Gitlab::Utils::StrongMemoize
2019-12-04 20:38:33 +05:30
RUNNER_REMOTE_TAG_PREFIX = 'refs/tags/'
RUNNER_REMOTE_BRANCH_PREFIX = 'refs/remotes/origin/'
2019-07-07 11:18:12 +05:30
2018-11-18 11:00:15 +05:30
def artifacts
return unless options[:artifacts]
list = []
list << create_archive(options[:artifacts])
list << create_reports(options[:artifacts][:reports], expire_in: options[:artifacts][:expire_in])
list.flatten.compact
end
2019-07-07 11:18:12 +05:30
def ref_type
if tag
'tag'
else
'branch'
end
end
def git_depth
2019-09-04 21:01:54 +05:30
if git_depth_variable
git_depth_variable[:value]
2020-05-24 23:13:21 +05:30
else
2019-09-04 21:01:54 +05:30
project.ci_default_git_depth
end.to_i
2019-07-07 11:18:12 +05:30
end
2021-04-17 20:07:23 +05:30
def runner_variables
2022-11-25 23:54:43 +05:30
variables
2023-01-13 00:05:48 +05:30
.sort_and_expand_all(keep_undefined: true,
expand_file_refs: false,
2023-03-17 16:20:25 +05:30
expand_raw_refs: false)
2022-11-25 23:54:43 +05:30
.to_runner_variables
2021-04-17 20:07:23 +05:30
end
2019-07-07 11:18:12 +05:30
def refspecs
specs = []
2019-12-21 20:55:43 +05:30
specs << refspec_for_persistent_ref if persistent_ref_exist?
2019-07-07 11:18:12 +05:30
if git_depth > 0
specs << refspec_for_branch(ref) if branch? || legacy_detached_merge_request_pipeline?
specs << refspec_for_tag(ref) if tag?
else
specs << refspec_for_branch
specs << refspec_for_tag
end
specs
end
2021-04-29 21:17:54 +05:30
# rubocop: disable CodeReuse/ActiveRecord
def all_dependencies
dependencies = super
2021-10-27 15:23:28 +05:30
ActiveRecord::Associations::Preloader.new.preload(dependencies, :job_artifacts_archive)
2021-04-29 21:17:54 +05:30
dependencies
end
# rubocop: enable CodeReuse/ActiveRecord
2018-11-18 11:00:15 +05:30
private
def create_archive(artifacts)
return unless artifacts[:untracked] || artifacts[:paths]
2022-06-21 17:19:12 +05:30
BuildArtifact.for_archive(artifacts).to_h.tap do |artifact|
artifact.delete(:exclude) unless artifact[:exclude].present?
2020-05-24 23:13:21 +05:30
end
2018-11-18 11:00:15 +05:30
end
def create_reports(reports, expire_in:)
return unless reports&.any?
2022-06-21 17:19:12 +05:30
reports.map { |report| BuildArtifact.for_report(report, expire_in).to_h.compact }
end
BuildArtifact = Struct.new(:name, :untracked, :paths, :exclude, :when, :expire_in, :artifact_type, :artifact_format, keyword_init: true) do
def self.for_archive(artifacts)
self.new(
artifact_type: :archive,
artifact_format: :zip,
name: artifacts[:name],
untracked: artifacts[:untracked],
paths: artifacts[:paths],
when: artifacts[:when],
expire_in: artifacts[:expire_in],
exclude: artifacts[:exclude]
)
end
def self.for_report(report, expire_in)
type, params = report
if type == :coverage_report
artifact_type = params[:coverage_format].to_sym
paths = [params[:path]]
else
artifact_type = type
paths = params
end
self.new(
artifact_type: artifact_type,
artifact_format: ::Ci::JobArtifact::TYPE_AND_FORMAT_PAIRS.fetch(artifact_type),
name: ::Ci::JobArtifact::DEFAULT_FILE_NAMES.fetch(artifact_type),
paths: paths,
2018-11-18 11:00:15 +05:30
when: 'always',
expire_in: expire_in
2022-06-21 17:19:12 +05:30
)
2018-11-18 11:00:15 +05:30
end
end
2019-07-07 11:18:12 +05:30
def refspec_for_branch(ref = '*')
"+#{Gitlab::Git::BRANCH_REF_PREFIX}#{ref}:#{RUNNER_REMOTE_BRANCH_PREFIX}#{ref}"
end
def refspec_for_tag(ref = '*')
"+#{Gitlab::Git::TAG_REF_PREFIX}#{ref}:#{RUNNER_REMOTE_TAG_PREFIX}#{ref}"
end
2019-12-21 20:55:43 +05:30
def refspec_for_persistent_ref
2022-05-07 20:08:51 +05:30
"+#{pipeline.persistent_ref.path}:#{pipeline.persistent_ref.path}"
2019-12-21 20:55:43 +05:30
end
def persistent_ref_exist?
2020-05-24 23:13:21 +05:30
##
# Persistent refs for pipelines definitely exist from GitLab 12.4,
# hence, we don't need to check the ref existence before passing it to runners.
# Checking refs pressurizes gitaly node and should be avoided.
# Issue: https://gitlab.com/gitlab-com/gl-infra/production/-/issues/2143
return true if Feature.enabled?(:ci_skip_persistent_ref_existence_check)
2019-12-21 20:55:43 +05:30
pipeline.persistent_ref.exist?
end
2019-09-04 21:01:54 +05:30
def git_depth_variable
strong_memoize(:git_depth_variable) do
variables&.find { |variable| variable[:key] == 'GIT_DEPTH' }
end
end
2018-11-18 11:00:15 +05:30
end
end
2020-10-24 23:57:45 +05:30
2021-06-08 01:23:25 +05:30
Ci::BuildRunnerPresenter.prepend_mod_with('Ci::BuildRunnerPresenter')