diff --git a/app/models/commit_status.rb b/app/models/commit_status.rb index 213cd20e8d8..6fef1c038e9 100644 --- a/app/models/commit_status.rb +++ b/app/models/commit_status.rb @@ -55,11 +55,16 @@ class CommitStatus < ActiveRecord::Base def self.stages # We group by stage name, but order stages by theirs' index - unscoped.where(id: all.ids).group('stage').order('max(stage_idx)', 'stage').pluck('stage') + unscoped.from(all, :sg).group('stage').order('max(stage_idx)', 'stage').pluck('sg.stage') end - def self.status_for_stage(stage) - where(stage: stage).status + def self.stages_status + # We execute subquery for each stage to calculate a stage status + statuses = unscoped.from(all, :sg).group('stage').pluck('sg.stage', all.where('stage=sg.stage').status_sql) + statuses.inject({}) do |h, k| + h[k.first] = k.last + h + end end def ignored? diff --git a/app/views/projects/ci/commits/_commit.html.haml b/app/views/projects/ci/commits/_commit.html.haml index 7f9a3417836..90ac41666d0 100644 --- a/app/views/projects/ci/commits/_commit.html.haml +++ b/app/views/projects/ci/commits/_commit.html.haml @@ -31,9 +31,10 @@ Cant find HEAD commit for this branch + - stages_status = commit.statuses.stages_status - stages.each do |stage| %td - - if status = commit.statuses.status_for_stage(stage) + - if status = stages_status[stage] - tooltip = "#{stage.titleize}: #{status}" %span.has-tooltip(title="#{tooltip}"){class: "ci-status-icon-#{status}"} = ci_icon_for_status(status) diff --git a/spec/models/commit_status_spec.rb b/spec/models/commit_status_spec.rb index ea60894c419..434e58cfd06 100644 --- a/spec/models/commit_status_spec.rb +++ b/spec/models/commit_status_spec.rb @@ -185,5 +185,17 @@ describe CommitStatus, models: true do is_expected.to eq(%w(build test deploy)) end end + + context 'stages with statuses' do + subject { CommitStatus.where(commit: commit).stages_status } + + it 'return list of stages with statuses' do + is_expected.to eq({ + 'build' => 'failed', + 'test' => 'success', + 'deploy' => 'running' + }) + end + end end end