summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/models/ci/pipeline.rb6
-rw-r--r--app/models/commit.rb16
-rw-r--r--app/services/ci/image_for_build_service.rb13
-rw-r--r--changelogs/unreleased/show-commit-status-from-latest-pipeline.yml4
-rw-r--r--spec/models/commit_spec.rb27
5 files changed, 39 insertions, 27 deletions
diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb
index 3fee6c18770..cd7d8fd3af7 100644
--- a/app/models/ci/pipeline.rb
+++ b/app/models/ci/pipeline.rb
@@ -89,9 +89,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)
diff --git a/app/models/commit.rb b/app/models/commit.rb
index 9e7fde9503d..2134ba2d75f 100644
--- a/app/models/commit.rb
+++ b/app/models/commit.rb
@@ -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
- else
- @statuses[ref] = pipelines.status
- end
+ return @statuses[ref] if @statuses.key?(ref)
+
+ latest_pipeline = if ref
+ pipelines.latest_for(ref)
+ else
+ pipelines.latest
+ end.first
+
+ @statuses[ref] = latest_pipeline.try(:status)
end
def revert_branch_name
diff --git a/app/services/ci/image_for_build_service.rb b/app/services/ci/image_for_build_service.rb
index 75d847d5bee..026a727a8f9 100644
--- a/app/services/ci/image_for_build_service.rb
+++ b/app/services/ci/image_for_build_service.rb
@@ -1,11 +1,18 @@
module Ci
class ImageForBuildService
def execute(project, opts)
- sha = opts[:sha] || ref_sha(project, opts[:ref])
+ ref = opts[:ref]
+ sha = opts[:sha] || ref_sha(project, ref)
pipelines = project.pipelines.where(sha: sha)
- pipelines = pipelines.where(ref: opts[:ref]) if opts[:ref]
- image_name = image_for_status(pipelines.status)
+
+ latest_pipeline = if ref
+ pipelines.latest_for(ref)
+ else
+ pipelines.latest
+ end.first
+
+ image_name = image_for_status(latest_pipeline.try(:status))
image_path = Rails.root.join('public/ci', image_name)
OpenStruct.new(path: image_path, name: image_name)
diff --git a/changelogs/unreleased/show-commit-status-from-latest-pipeline.yml b/changelogs/unreleased/show-commit-status-from-latest-pipeline.yml
new file mode 100644
index 00000000000..bbd7a217493
--- /dev/null
+++ b/changelogs/unreleased/show-commit-status-from-latest-pipeline.yml
@@ -0,0 +1,4 @@
+---
+title: Show commit status from latest pipeline
+merge_request: 7333
+author:
diff --git a/spec/models/commit_spec.rb b/spec/models/commit_spec.rb
index e3bb3482d67..ca277601970 100644
--- a/spec/models/commit_spec.rb
+++ b/spec/models/commit_spec.rb
@@ -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