Commit a17c90b2 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Use enumerated status in persisted stage class

parent f9228f6b
module Ci module Ci
class Stage < ActiveRecord::Base class Stage < ActiveRecord::Base
extend Ci::Model extend Ci::Model
include HasStatus
enumerated_status!
belongs_to :project belongs_to :project
belongs_to :pipeline belongs_to :pipeline
......
...@@ -8,6 +8,8 @@ module HasStatus ...@@ -8,6 +8,8 @@ module HasStatus
ACTIVE_STATUSES = %w[pending running].freeze ACTIVE_STATUSES = %w[pending running].freeze
COMPLETED_STATUSES = %w[success failed canceled skipped].freeze COMPLETED_STATUSES = %w[success failed canceled skipped].freeze
ORDERED_STATUSES = %w[failed pending running manual canceled success skipped created].freeze ORDERED_STATUSES = %w[failed pending running manual canceled success skipped created].freeze
STATUSES_ENUM = { created: 0, pending: 1, running: 2, success: 3,
failed: 4, canceled: 5, skipped: 6, manual: 7 }
class_methods do class_methods do
def status_sql def status_sql
...@@ -54,6 +56,14 @@ module HasStatus ...@@ -54,6 +56,14 @@ module HasStatus
def all_state_names def all_state_names
state_machines.values.flat_map(&:states).flat_map { |s| s.map(&:name) } state_machines.values.flat_map(&:states).flat_map { |s| s.map(&:name) }
end end
private
def enumerated_status!
@status_strategy = :enumerator
enum status: HasStatus::STATUSES_ENUM
end
end end
included do included do
......
...@@ -15,4 +15,10 @@ FactoryGirl.define do ...@@ -15,4 +15,10 @@ FactoryGirl.define do
warnings: warnings) warnings: warnings)
end end
end end
factory :ci_stage_entity, class: Ci::Stage do
project factory: :empty_project
pipeline factory: :ci_empty_pipeline
status 'pending'
end
end end
...@@ -9,7 +9,6 @@ describe MigrateStagesStatuses, :migration do ...@@ -9,7 +9,6 @@ describe MigrateStagesStatuses, :migration do
STATUSES = { created: 0, pending: 1, running: 2, success: 3, STATUSES = { created: 0, pending: 1, running: 2, success: 3,
failed: 4, canceled: 5, skipped: 6, manual: 7 } failed: 4, canceled: 5, skipped: 6, manual: 7 }
STAGES = { test: 1, build: 2, deploy: 3}
before do before do
projects.create!(id: 1, name: 'gitlab1', path: 'gitlab1') projects.create!(id: 1, name: 'gitlab1', path: 'gitlab1')
...@@ -42,8 +41,10 @@ describe MigrateStagesStatuses, :migration do ...@@ -42,8 +41,10 @@ describe MigrateStagesStatuses, :migration do
end end
def create_job(project:, pipeline:, stage:, status:, **opts) def create_job(project:, pipeline:, stage:, status:, **opts)
stages = { test: 1, build: 2, deploy: 3}
jobs.create!(project_id: project, commit_id: pipeline, jobs.create!(project_id: project, commit_id: pipeline,
stage_idx: STAGES[stage.to_sym], stage: stage, stage_idx: stages[stage.to_sym], stage: stage,
status: status, **opts) status: status, **opts)
end end
end end
require 'spec_helper'
describe Ci::Stage, :models do
describe '#status' do
context 'when stage is pending' do
let(:stage) { create(:ci_stage_entity, status: 'pending') }
it 'has a correct status value' do
expect(stage.status).to eq 'pending'
end
end
context 'when stage is success' do
let(:stage) { create(:ci_stage_entity, status: 'success') }
it 'has a correct status value' do
expect(stage.status).to eq 'success'
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