summaryrefslogtreecommitdiff
path: root/app/models/ci/pipeline.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/models/ci/pipeline.rb')
-rw-r--r--app/models/ci/pipeline.rb102
1 files changed, 42 insertions, 60 deletions
diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb
index f730b949ee9..29ec41ef1a1 100644
--- a/app/models/ci/pipeline.rb
+++ b/app/models/ci/pipeline.rb
@@ -14,6 +14,7 @@ module Ci
include HasRef
include ShaAttribute
include FromUnion
+ include UpdatedAtFilterable
sha_attribute :source_sha
sha_attribute :target_sha
@@ -204,15 +205,7 @@ module Ci
end
scope :internal, -> { where(source: internal_sources) }
- scope :ci_sources, -> { where(config_source: ci_sources_values) }
-
- scope :sort_by_merge_request_pipelines, -> do
- sql = 'CASE ci_pipelines.source WHEN (?) THEN 0 ELSE 1 END, ci_pipelines.id DESC'
- query = ApplicationRecord.send(:sanitize_sql_array, [sql, sources[:merge_request_event]]) # rubocop:disable GitlabSecurity/PublicSend
-
- order(Arel.sql(query))
- end
-
+ scope :ci_sources, -> { where(config_source: ::Ci::PipelineEnums.ci_config_sources_values) }
scope :for_user, -> (user) { where(user: user) }
scope :for_sha, -> (sha) { where(sha: sha) }
scope :for_source_sha, -> (source_sha) { where(source_sha: source_sha) }
@@ -221,22 +214,6 @@ module Ci
scope :for_id, -> (id) { where(id: id) }
scope :created_after, -> (time) { where('ci_pipelines.created_at > ?', time) }
- scope :triggered_by_merge_request, -> (merge_request) do
- where(source: :merge_request_event, merge_request: merge_request)
- end
-
- scope :detached_merge_request_pipelines, -> (merge_request, sha) do
- triggered_by_merge_request(merge_request).for_sha(sha)
- end
-
- scope :merge_request_pipelines, -> (merge_request, source_sha) do
- triggered_by_merge_request(merge_request).for_source_sha(source_sha)
- end
-
- scope :triggered_for_branch, -> (ref) do
- where(source: branch_pipeline_sources).where(ref: ref, tag: false)
- end
-
scope :with_reports, -> (reports_scope) do
where('EXISTS (?)', ::Ci::Build.latest.with_reports(reports_scope).where('ci_pipelines.id=ci_builds.commit_id').select(1))
end
@@ -323,11 +300,6 @@ module Ci
end
end
- def self.latest_for_shas(shas)
- max_id_per_sha = for_sha(shas).group(:sha).select("max(id)")
- where(id: max_id_per_sha)
- end
-
def self.latest_successful_ids_per_project
success.group(:project_id).select('max(id) as id')
end
@@ -344,14 +316,6 @@ module Ci
sources.reject { |source| source == "external" }.values
end
- def self.branch_pipeline_sources
- @branch_pipeline_sources ||= sources.reject { |source| source == 'merge_request_event' }.values
- end
-
- def self.ci_sources_values
- config_sources.values_at(:repository_source, :auto_devops_source, :unknown_source)
- end
-
def self.bridgeable_statuses
::Ci::Pipeline::AVAILABLE_STATUSES - %w[created preparing pending]
end
@@ -478,6 +442,10 @@ module Ci
end
end
+ def before_sha
+ super || Gitlab::Git::BLANK_SHA
+ end
+
def short_sha
Ci::Pipeline.truncate_sha(sha)
end
@@ -534,6 +502,10 @@ module Ci
builds.skipped.after_stage(stage_idx).find_each(&:process)
end
+ def child?
+ false
+ end
+
def latest?
return false unless git_ref && commit.present?
@@ -599,12 +571,6 @@ module Ci
project.notes.for_commit_id(sha)
end
- # rubocop: disable CodeReuse/ServiceClass
- def process!(trigger_build_ids = nil)
- Ci::ProcessPipelineService.new(project, user).execute(self, trigger_build_ids)
- end
- # rubocop: enable CodeReuse/ServiceClass
-
def update_status
retry_optimistic_lock(self) do
new_status = latest_builds_status.to_s
@@ -646,12 +612,11 @@ module Ci
def predefined_variables
Gitlab::Ci::Variables::Collection.new.tap do |variables|
variables.append(key: 'CI_PIPELINE_IID', value: iid.to_s)
- variables.append(key: 'CI_CONFIG_PATH', value: config_path)
variables.append(key: 'CI_PIPELINE_SOURCE', value: source.to_s)
- variables.append(key: 'CI_COMMIT_MESSAGE', value: git_commit_message.to_s)
- variables.append(key: 'CI_COMMIT_TITLE', value: git_commit_full_title.to_s)
- variables.append(key: 'CI_COMMIT_DESCRIPTION', value: git_commit_description.to_s)
- variables.append(key: 'CI_COMMIT_REF_PROTECTED', value: (!!protected_ref?).to_s)
+
+ variables.append(key: 'CI_CONFIG_PATH', value: config_path)
+
+ variables.concat(predefined_commit_variables)
if merge_request_event? && merge_request
variables.append(key: 'CI_MERGE_REQUEST_EVENT_TYPE', value: merge_request_event_type.to_s)
@@ -666,6 +631,29 @@ module Ci
end
end
+ def predefined_commit_variables
+ Gitlab::Ci::Variables::Collection.new.tap do |variables|
+ variables.append(key: 'CI_COMMIT_SHA', value: sha)
+ variables.append(key: 'CI_COMMIT_SHORT_SHA', value: short_sha)
+ variables.append(key: 'CI_COMMIT_BEFORE_SHA', value: before_sha)
+ variables.append(key: 'CI_COMMIT_REF_NAME', value: source_ref)
+ variables.append(key: 'CI_COMMIT_REF_SLUG', value: source_ref_slug)
+ variables.append(key: 'CI_COMMIT_BRANCH', value: ref) if branch?
+ variables.append(key: 'CI_COMMIT_TAG', value: ref) if tag?
+ variables.append(key: 'CI_COMMIT_MESSAGE', value: git_commit_message.to_s)
+ variables.append(key: 'CI_COMMIT_TITLE', value: git_commit_full_title.to_s)
+ variables.append(key: 'CI_COMMIT_DESCRIPTION', value: git_commit_description.to_s)
+ variables.append(key: 'CI_COMMIT_REF_PROTECTED', value: (!!protected_ref?).to_s)
+
+ # legacy variables
+ variables.append(key: 'CI_BUILD_REF', value: sha)
+ variables.append(key: 'CI_BUILD_BEFORE_SHA', value: before_sha)
+ variables.append(key: 'CI_BUILD_REF_NAME', value: source_ref)
+ variables.append(key: 'CI_BUILD_REF_SLUG', value: source_ref_slug)
+ variables.append(key: 'CI_BUILD_TAG', value: ref) if tag?
+ end
+ end
+
def queued_duration
return unless started_at
@@ -781,18 +769,10 @@ module Ci
triggered_by_merge_request? && target_sha.present?
end
- def merge_train_pipeline?
- merge_request_pipeline? && merge_train_ref?
- end
-
def merge_request_ref?
MergeRequest.merge_request_ref?(ref)
end
- def merge_train_ref?
- MergeRequest.merge_train_ref?(ref)
- end
-
def matches_sha_or_source_sha?(sha)
self.sha == sha || self.source_sha == sha
end
@@ -825,9 +805,7 @@ module Ci
return unless merge_request_event?
strong_memoize(:merge_request_event_type) do
- if merge_train_pipeline?
- :merge_train
- elsif merge_request_pipeline?
+ if merge_request_pipeline?
:merged_result
elsif detached_merge_request_pipeline?
:detached
@@ -839,6 +817,10 @@ module Ci
@persistent_ref ||= PersistentRef.new(pipeline: self)
end
+ def find_successful_build_ids_by_names(names)
+ statuses.latest.success.where(name: names).pluck(:id)
+ end
+
private
def pipeline_data