Commit 35a464cc authored by Kamil Trzciński's avatar Kamil Trzciński

Remove HasStatus.status

parent b8003a78
...@@ -904,7 +904,9 @@ module Ci ...@@ -904,7 +904,9 @@ module Ci
def latest_builds_status def latest_builds_status
return 'failed' unless yaml_errors.blank? return 'failed' unless yaml_errors.blank?
statuses.latest.status || 'skipped' Gitlab::Ci::Status::GroupedStatuses
.new(statuses.latest)
.one[:status] || 'skipped'
end end
def keep_around_commits def keep_around_commits
......
...@@ -78,7 +78,7 @@ module Ci ...@@ -78,7 +78,7 @@ module Ci
def update_status def update_status
retry_optimistic_lock(self) do retry_optimistic_lock(self) do
case statuses.latest.status case latest_stage_status
when 'created' then nil when 'created' then nil
when 'preparing' then prepare when 'preparing' then prepare
when 'pending' then enqueue when 'pending' then enqueue
...@@ -124,5 +124,11 @@ module Ci ...@@ -124,5 +124,11 @@ module Ci
def manual_playable? def manual_playable?
blocked? || skipped? blocked? || skipped?
end end
def latest_stage_status
Gitlab::Ci::Status::GroupedStatuses
.new(statuses.latest)
.one[:status] || 'skipped'
end
end end
end end
...@@ -153,11 +153,15 @@ class CommitStatus < ApplicationRecord ...@@ -153,11 +153,15 @@ class CommitStatus < ApplicationRecord
end end
def self.status_for_prior_stages(index) def self.status_for_prior_stages(index)
before_stage(index).latest.status || 'success' Gitlab::Ci::Status::GroupedStatuses
.new(before_stage(index).latest)
.one[:status] || 'success'
end end
def self.status_for_names(names) def self.status_for_names(names)
where(name: names).latest.status || 'success' Gitlab::Ci::Status::GroupedStatuses
.new(where(name: names).latest)
.one[:status] || 'success'
end end
def locking_enabled? def locking_enabled?
......
...@@ -18,10 +18,44 @@ module HasStatus ...@@ -18,10 +18,44 @@ module HasStatus
UnknownStatusError = Class.new(StandardError) UnknownStatusError = Class.new(StandardError)
class_methods do class_methods do
def status def legacy_status_sql
Gitlab::Ci::Status::GroupedStatuses scope_relevant = respond_to?(:exclude_ignored) ? exclude_ignored : all
.new(all) scope_warnings = respond_to?(:failed_but_allowed) ? failed_but_allowed : none
.one[:status]
builds = scope_relevant.select('count(*)').to_sql
created = scope_relevant.created.select('count(*)').to_sql
success = scope_relevant.success.select('count(*)').to_sql
manual = scope_relevant.manual.select('count(*)').to_sql
scheduled = scope_relevant.scheduled.select('count(*)').to_sql
preparing = scope_relevant.preparing.select('count(*)').to_sql
pending = scope_relevant.pending.select('count(*)').to_sql
running = scope_relevant.running.select('count(*)').to_sql
skipped = scope_relevant.skipped.select('count(*)').to_sql
canceled = scope_relevant.canceled.select('count(*)').to_sql
warnings = scope_warnings.select('count(*) > 0').to_sql.presence || 'false'
Arel.sql(
"(CASE
WHEN (#{builds})=(#{skipped}) AND (#{warnings}) THEN 'success'
WHEN (#{builds})=(#{skipped}) THEN 'skipped'
WHEN (#{builds})=(#{success}) THEN 'success'
WHEN (#{builds})=(#{created}) THEN 'created'
WHEN (#{builds})=(#{preparing}) THEN 'preparing'
WHEN (#{builds})=(#{success})+(#{skipped}) THEN 'success'
WHEN (#{builds})=(#{success})+(#{skipped})+(#{canceled}) THEN 'canceled'
WHEN (#{builds})=(#{created})+(#{skipped})+(#{pending}) THEN 'pending'
WHEN (#{running})+(#{pending})>0 THEN 'running'
WHEN (#{manual})>0 THEN 'manual'
WHEN (#{scheduled})>0 THEN 'scheduled'
WHEN (#{preparing})>0 THEN 'preparing'
WHEN (#{created})>0 THEN 'running'
ELSE 'failed'
END)"
)
end
def legacy_status
all.pluck(legacy_status_sql).first
end end
def started_at def started_at
......
...@@ -5,36 +5,39 @@ module Gitlab ...@@ -5,36 +5,39 @@ module Gitlab
module Status module Status
class CompositeStatus class CompositeStatus
def initialize(all_statuses) def initialize(all_statuses)
@status_set = build_status_set(all_statuses) @warnings = false
@status_set = Set.new
build_status_set(all_statuses)
end end
def status def status
case case
when only?(:skipped, :warning) when only_of?(:skipped) && warnings?
:success :success
when only?(:skipped) when only_of?(:skipped)
:skipped :skipped
when only?(:success) when only_of?(:success)
:skipped :success
when only?(:created) when only_of?(:created)
:created :created
when only?(:preparing) when only_of?(:preparing)
:preparing :preparing
when only?(:success, :skipped) when only_of?(:success, :skipped)
:success :success
when only?(:success, :skipped, :canceled) when only_of?(:success, :skipped, :canceled)
:canceled :canceled
when only?(:created, :skipped, :pending) when only_of?(:created, :skipped, :pending)
:pending :pending
when include?(:running, :pending) when any_of?(:running, :pending)
:running :running
when include?(:manual) when any_of?(:manual)
:manual :manual
when include?(:scheduled) when any_of?(:scheduled)
:scheduled :scheduled
when include?(:preparing) when any_of?(:preparing)
:preparing :preparing
when include?(:created) when any_of?(:created)
:running :running
else else
:failed :failed
...@@ -42,31 +45,28 @@ module Gitlab ...@@ -42,31 +45,28 @@ module Gitlab
end end
def warnings? def warnings?
include?(:warning) @warnings
end end
private private
def include?(*names) def any_of?(*names)
names.any? { |name| @status_set.include?(name) } names.any? { |name| @status_set.include?(name) }
end end
def only?(*names) def only_of?(*names)
matching = names.count { |name| @status_set.include?(name) } == @status_set.size matching = names.count { |name| @status_set.include?(name) }
matching == @status_set.size
end end
def build_status_set(all_statuses) def build_status_set(all_statuses)
status_set = Set.new
all_statuses.each do |status| all_statuses.each do |status|
if status[:allow_failure] && HasStatus::WARNING_STATUSES.include?(status[:status]) if status[:allow_failure] && HasStatus::WARNING_STATUSES.include?(status[:status])
status_set.add(:warning) @warnings = true
else else
status_set.add(status[:status].to_sym) @status_set.add(status[:status].to_sym)
end end
end end
status_set
end end
end end
end end
......
...@@ -3,12 +3,13 @@ ...@@ -3,12 +3,13 @@
require 'spec_helper' require 'spec_helper'
describe HasStatus do describe HasStatus do
describe '.status' do describe '.legacy_status' do
subject { CommitStatus.status } subject { CommitStatus.legacy_status }
shared_examples 'build status summary' do shared_examples 'build status summary' do
context 'all successful' do context 'all successful' do
let!(:statuses) { Array.new(2) { create(type, status: :success) } } let!(:statuses) { Array.new(2) { create(type, status: :success) } }
it { is_expected.to eq 'success' } it { is_expected.to eq 'success' }
end end
...@@ -372,8 +373,8 @@ describe HasStatus do ...@@ -372,8 +373,8 @@ describe HasStatus do
end end
end end
describe '.status_sql' do describe '.legacy_status_sql' do
subject { Ci::Build.status_sql } subject { Ci::Build.legacy_status_sql }
it 'returns SQL' do it 'returns SQL' do
puts subject puts subject
......
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