Commit 10253707 authored by Kamil Trzcinski's avatar Kamil Trzcinski

Fix CiStatus implementation and tests

parent 4d72ca39
...@@ -51,8 +51,6 @@ class CommitStatus < ActiveRecord::Base ...@@ -51,8 +51,6 @@ class CommitStatus < ActiveRecord::Base
scope :ordered, -> { order(:ref, :stage_idx, :name) } scope :ordered, -> { order(:ref, :stage_idx, :name) }
scope :ignored, -> { where(allow_failure: true, status: [:failed, :canceled]) } scope :ignored, -> { where(allow_failure: true, status: [:failed, :canceled]) }
AVAILABLE_STATUSES = ['pending', 'running', 'success', 'failed', 'canceled']
state_machine :status, initial: :pending do state_machine :status, initial: :pending do
event :run do event :run do
transition pending: :running transition pending: :running
......
module CiStatus module CiStatus
extend ActiveSupport::Concern extend ActiveSupport::Concern
module ClassMethods AVAILABLE_STATUSES = %w(pending running success failed canceled skipped)
class_methods do
def status_sql def status_sql
builds = all.select('count(id)').to_sql builds = all.select('count(*)').to_sql
success = all.success.select('count(id)').to_sql success = all.success.select('count(*)').to_sql
ignored = all.failed.where(allow_failure: true).select('count(id)').to_sql if all.try(:ignored) ignored = all.ignored.select('count(*)').to_sql if all.try(:ignored)
ignored ||= '0' ignored ||= '0'
pending = all.pending.select('count(id)').to_sql pending = all.pending.select('count(*)').to_sql
running = all.running.select('count(id)').to_sql running = all.running.select('count(*)').to_sql
canceled = all.canceled.select('count(id)').to_sql canceled = all.canceled.select('count(*)').to_sql
deduce_status = "(CASE deduce_status = "(CASE
WHEN (#{builds})=0 THEN 'skipped' WHEN (#{builds})=0 THEN 'skipped'
...@@ -24,7 +26,7 @@ module CiStatus ...@@ -24,7 +26,7 @@ module CiStatus
end end
def status def status
pluck(self.status_sql).first all.pluck(self.status_sql).first
end end
def duration def duration
...@@ -34,7 +36,7 @@ module CiStatus ...@@ -34,7 +36,7 @@ module CiStatus
end end
included do included do
validates :status, inclusion: { in: %w(pending running failed success canceled skipped) } validates :status, inclusion: { in: AVAILABLE_STATUSES }
state_machine :status, initial: :pending do state_machine :status, initial: :pending do
state :pending, value: 'pending' state :pending, value: 'pending'
......
...@@ -8,7 +8,7 @@ describe CiStatus do ...@@ -8,7 +8,7 @@ describe CiStatus do
describe '.status' do describe '.status' do
before do before do
allow(@object).to receive(:all).and_return(statuses) allow(@object).to receive(:all).and_return(CommitStatus.where(id: statuses))
end end
subject { @object.status } subject { @object.status }
......
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