Commit cff9c16b authored by Grzegorz Bizon's avatar Grzegorz Bizon

Pass memoizable warnings attribute to stage object

parent ee18d89f
...@@ -128,16 +128,21 @@ module Ci ...@@ -128,16 +128,21 @@ module Ci
end end
def stages def stages
# TODO, this needs refactoring, see gitlab-ce#26481.
stages_query = statuses
.group('stage').select(:stage).order('max(stage_idx)')
status_sql = statuses.latest.where('stage=sg.stage').status_sql status_sql = statuses.latest.where('stage=sg.stage').status_sql
stages_query = statuses.group('stage').select(:stage) warnings_sql = statuses.latest.select('COUNT(*) > 0')
.order('max(stage_idx)') .where('stage=sg.stage').failed_but_allowed.to_sql
stages_with_statuses = CommitStatus.from(stages_query, :sg). stages_with_statuses = CommitStatus.from(stages_query, :sg)
pluck('sg.stage', status_sql) .pluck('sg.stage', status_sql, "(#{warnings_sql})")
stages_with_statuses.map do |stage| stages_with_statuses.map do |stage|
Ci::Stage.new(self, name: stage.first, status: stage.last) Ci::Stage.new(self, Hash[%i[name status warnings].zip(stage)])
end end
end end
......
...@@ -8,10 +8,11 @@ module Ci ...@@ -8,10 +8,11 @@ module Ci
delegate :project, to: :pipeline delegate :project, to: :pipeline
def initialize(pipeline, name:, status: nil) def initialize(pipeline, name:, status: nil, warnings: nil)
@pipeline = pipeline @pipeline = pipeline
@name = name @name = name
@status = status @status = status
@warnings = warnings
end end
def to_param def to_param
...@@ -45,7 +46,7 @@ module Ci ...@@ -45,7 +46,7 @@ module Ci
end end
def has_warnings? def has_warnings?
statuses.latest.failed_but_allowed.any? @warnings ||= statuses.latest.failed_but_allowed.any?
end end
end end
end end
...@@ -3,11 +3,12 @@ FactoryGirl.define do ...@@ -3,11 +3,12 @@ FactoryGirl.define do
transient do transient do
name 'test' name 'test'
status nil status nil
warnings nil
pipeline factory: :ci_empty_pipeline pipeline factory: :ci_empty_pipeline
end end
initialize_with do initialize_with do
Ci::Stage.new(pipeline, name: name, status: status) Ci::Stage.new(pipeline, name: name, status: status, warnings: warnings)
end end
end end
end end
...@@ -168,13 +168,23 @@ describe Ci::Stage, models: true do ...@@ -168,13 +168,23 @@ describe Ci::Stage, models: true do
describe '#has_warnings?' do describe '#has_warnings?' do
context 'when stage has warnings' do context 'when stage has warnings' do
before do context 'when using memoized warnings flag' do
create(:ci_build, :failed, :allowed_to_fail, let(:stage) { build(:ci_stage, warnings: true) }
stage: stage_name, pipeline: pipeline)
it 'has warnings' do
expect(stage).to have_warnings
end
end end
it 'has warnings' do context 'when calculating warnings from statuses' do
expect(stage).to have_warnings before do
create(:ci_build, :failed, :allowed_to_fail,
stage: stage_name, pipeline: pipeline)
end
it 'has warnings' do
expect(stage).to have_warnings
end
end 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