Commit 45e4a2a5 authored by gentcys's avatar gentcys

Fix commit latest pipeline status with child pipeline

parent c4b8fe7e
......@@ -394,26 +394,13 @@ module Ci
# given we simply get the latest pipelines for the commits, regardless
# of what refs the pipelines belong to.
def self.latest_pipeline_per_commit(commits, ref = nil)
p1 = arel_table
p2 = arel_table.alias
sql = select('DISTINCT ON (sha) *')
.where(sha: commits)
.order(:sha, id: :desc)
# This LEFT JOIN will filter out all but the newest row for every
# combination of (project_id, sha) or (project_id, sha, ref) if a ref is
# given.
cond = p1[:sha].eq(p2[:sha])
.and(p1[:project_id].eq(p2[:project_id]))
.and(p1[:id].lt(p2[:id]))
sql = sql.where(ref: ref) if ref
cond = cond.and(p1[:ref].eq(p2[:ref])) if ref
join = p1.join(p2, Arel::Nodes::OuterJoin).on(cond)
relation = where(sha: commits)
.where(p2[:id].eq(nil))
.joins(join.join_sources)
relation = relation.where(ref: ref) if ref
relation.each_with_object({}) do |pipeline, hash|
sql.each_with_object({}) do |pipeline, hash|
hash[pipeline.sha] = pipeline
end
end
......
---
title: Filter out pipelines that were excluded in the relation scope in Ci::Pipeline#latest_pipeline_per_commit
merge_request: 55657
author: Cong Chen @gentcys
type: fixed
......@@ -2282,6 +2282,35 @@ RSpec.describe Ci::Pipeline, :mailer, factory_default: :keep do
)
end
end
context 'when method is scoped' do
let!(:commit_123_ref_master_parent_pipeline) do
create(
:ci_pipeline,
sha: '123',
ref: 'master',
project: project
)
end
let!(:commit_123_ref_master_child_pipeline) do
create(
:ci_pipeline,
sha: '123',
ref: 'master',
project: project,
child_of: commit_123_ref_master_parent_pipeline
)
end
it 'returns the latest pipeline after applying the scope' do
result = described_class.ci_sources.latest_pipeline_per_commit(%w[123], 'master')
expect(result).to match(
'123' => commit_123_ref_master_parent_pipeline
)
end
end
end
describe '.latest_successful_ids_per_project' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment