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

Remove HasStatus.status

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