Commit 18a71c47 authored by Lin Jen-Shin's avatar Lin Jen-Shin

Show commit status from latest pipeline

Rather than compound status from all pipelines.

Closes #20560
parent 7ce03197
......@@ -83,9 +83,13 @@ module Ci
end
end
scope :latest, -> { order(id: :desc) }
# ref can't be HEAD or SHA, can only be branch/tag name
scope :latest_for, ->(ref) { where(ref: ref).latest }
def self.latest_successful_for(ref)
where(ref: ref).order(id: :desc).success.first
latest_for(ref).success.first
end
def self.truncate_sha(sha)
......
......@@ -232,13 +232,15 @@ class Commit
def status(ref = nil)
@statuses ||= {}
if @statuses.key?(ref)
@statuses[ref]
elsif ref
@statuses[ref] = pipelines.where(ref: ref).status
return @statuses[ref] if @statuses.key?(ref)
latest_pipeline = if ref
pipelines.latest_for(ref)
else
@statuses[ref] = pipelines.status
end
pipelines.latest
end.first
@statuses[ref] = latest_pipeline.try(:status)
end
def revert_branch_name
......
---
title: Show commit status from latest pipeline
merge_request: 7333
author:
......@@ -206,23 +206,18 @@ eos
end
describe '#status' do
context 'without arguments for compound status' do
shared_examples 'giving the status from pipeline' do
it do
expect(commit.status).to eq(Ci::Pipeline.status)
end
end
context 'with pipelines' do
let!(:pipeline) do
create(:ci_empty_pipeline, project: project, sha: commit.sha)
context 'without arguments' do
before do
5.times do
create(:ci_empty_pipeline,
project: project,
sha: commit.sha,
status: Ci::Pipeline.all_state_names.sample)
end
it_behaves_like 'giving the status from pipeline'
end
context 'without pipelines' do
it_behaves_like 'giving the status from pipeline'
it 'gives the status from latest pipeline' do
expect(commit.status).to eq(Ci::Pipeline.latest.first.status)
end
end
......@@ -248,8 +243,8 @@ eos
expect(commit.status('fix')).to eq(pipeline_from_fix.status)
end
it 'gives compound status if ref is nil' do
expect(commit.status(nil)).to eq(commit.status)
it 'gives status from latest pipeline for whatever branch' do
expect(commit.status(nil)).to eq(Ci::Pipeline.latest.first.status)
end
end
end
......
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