summaryrefslogtreecommitdiff
path: root/app/models
diff options
context:
space:
mode:
authorGrzegorz Bizon <grzegorz@gitlab.com>2019-01-07 13:37:33 +0000
committerGrzegorz Bizon <grzegorz@gitlab.com>2019-01-07 13:37:33 +0000
commitcd11ede9ccdb05ff3e91bb4507af1ff063722608 (patch)
treefd27408bfa0afc1c11e7b6f34496815304291572 /app/models
parent75e6561684415b2fffb47a6293ba787c379b84b7 (diff)
parent4ab0b33db6f328fc68394fd3af992052f883401e (diff)
downloadgitlab-ce-cd11ede9ccdb05ff3e91bb4507af1ff063722608.tar.gz
Merge branch 'user-update-head-pipeline-worker-2' into 'master'
Refactor the logic of updating head pipelines for merge requests See merge request gitlab-org/gitlab-ce!23502
Diffstat (limited to 'app/models')
-rw-r--r--app/models/ci/pipeline.rb13
-rw-r--r--app/models/merge_request.rb20
2 files changed, 29 insertions, 4 deletions
diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb
index d0027ad823c..30a957b4117 100644
--- a/app/models/ci/pipeline.rb
+++ b/app/models/ci/pipeline.rb
@@ -178,6 +178,15 @@ module Ci
scope :for_user, -> (user) { where(user: user) }
+ scope :for_merge_request, -> (merge_request, ref, sha) do
+ ##
+ # We have to filter out unrelated MR pipelines.
+ # When merge request is empty, it selects general pipelines, such as push sourced pipelines.
+ # When merge request is matched, it selects MR pipelines.
+ where(merge_request: [nil, merge_request], ref: ref, sha: sha)
+ .sort_by_merge_request_pipelines
+ end
+
# Returns the pipelines in descending order (= newest first), optionally
# limited to a number of references.
#
@@ -265,6 +274,10 @@ module Ci
sources.reject { |source| source == "external" }.values
end
+ def self.latest_for_merge_request(merge_request, ref, sha)
+ for_merge_request(merge_request, ref, sha).first
+ end
+
def self.ci_sources_values
config_sources.values_at(:repository_source, :auto_devops_source, :unknown_source)
end
diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb
index 6092c56b925..613860ec31a 100644
--- a/app/models/merge_request.rb
+++ b/app/models/merge_request.rb
@@ -1092,10 +1092,15 @@ class MergeRequest < ActiveRecord::Base
def all_pipelines(shas: all_commit_shas)
return Ci::Pipeline.none unless source_project
- @all_pipelines ||= source_project.ci_pipelines
- .where(sha: shas, ref: source_branch)
- .where(merge_request: [nil, self])
- .sort_by_merge_request_pipelines
+ @all_pipelines ||=
+ source_project.ci_pipelines
+ .for_merge_request(self, source_branch, all_commit_shas)
+ end
+
+ def update_head_pipeline
+ self.head_pipeline = find_actual_head_pipeline
+
+ update_column(:head_pipeline_id, head_pipeline.id) if head_pipeline_id_changed?
end
def merge_request_pipeline_exists?
@@ -1338,4 +1343,11 @@ class MergeRequest < ActiveRecord::Base
source_project.repository.squash_in_progress?(id)
end
+
+ private
+
+ def find_actual_head_pipeline
+ source_project&.ci_pipelines
+ &.latest_for_merge_request(self, source_branch, diff_head_sha)
+ end
end