summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrzegorz Bizon <grzesiek.bizon@gmail.com>2017-07-04 15:31:15 +0200
committerGrzegorz Bizon <grzesiek.bizon@gmail.com>2017-07-04 15:31:15 +0200
commita17c90b2a7331a7427813684b04095b55c4b3cc1 (patch)
treead76404a74e856a37bd5a01d95ea61f0155053d0
parentf9228f6bf46f1d1caa4c62b80b8bd6ec883d33ae (diff)
downloadgitlab-ce-a17c90b2a7331a7427813684b04095b55c4b3cc1.tar.gz
Use enumerated status in persisted stage class
-rw-r--r--app/models/ci/stage.rb3
-rw-r--r--app/models/concerns/has_status.rb10
-rw-r--r--spec/factories/ci/stages.rb6
-rw-r--r--spec/migrations/migrate_stages_statuses_spec.rb5
-rw-r--r--spec/models/ci/stage_spec.rb21
5 files changed, 43 insertions, 2 deletions
diff --git a/app/models/ci/stage.rb b/app/models/ci/stage.rb
index 59570924c8d..0c7f8c7f485 100644
--- a/app/models/ci/stage.rb
+++ b/app/models/ci/stage.rb
@@ -1,6 +1,9 @@
module Ci
class Stage < ActiveRecord::Base
extend Ci::Model
+ include HasStatus
+
+ enumerated_status!
belongs_to :project
belongs_to :pipeline
diff --git a/app/models/concerns/has_status.rb b/app/models/concerns/has_status.rb
index 32af5566135..235196cae13 100644
--- a/app/models/concerns/has_status.rb
+++ b/app/models/concerns/has_status.rb
@@ -8,6 +8,8 @@ module HasStatus
ACTIVE_STATUSES = %w[pending running].freeze
COMPLETED_STATUSES = %w[success failed canceled skipped].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
def status_sql
@@ -54,6 +56,14 @@ module HasStatus
def all_state_names
state_machines.values.flat_map(&:states).flat_map { |s| s.map(&:name) }
end
+
+ private
+
+ def enumerated_status!
+ @status_strategy = :enumerator
+
+ enum status: HasStatus::STATUSES_ENUM
+ end
end
included do
diff --git a/spec/factories/ci/stages.rb b/spec/factories/ci/stages.rb
index d3c8bf9d54f..ee8ac85c92e 100644
--- a/spec/factories/ci/stages.rb
+++ b/spec/factories/ci/stages.rb
@@ -15,4 +15,10 @@ FactoryGirl.define do
warnings: warnings)
end
end
+
+ factory :ci_stage_entity, class: Ci::Stage do
+ project factory: :empty_project
+ pipeline factory: :ci_empty_pipeline
+ status 'pending'
+ end
end
diff --git a/spec/migrations/migrate_stages_statuses_spec.rb b/spec/migrations/migrate_stages_statuses_spec.rb
index 81bc38fea10..478ddad262a 100644
--- a/spec/migrations/migrate_stages_statuses_spec.rb
+++ b/spec/migrations/migrate_stages_statuses_spec.rb
@@ -9,7 +9,6 @@ describe MigrateStagesStatuses, :migration do
STATUSES = { created: 0, pending: 1, running: 2, success: 3,
failed: 4, canceled: 5, skipped: 6, manual: 7 }
- STAGES = { test: 1, build: 2, deploy: 3}
before do
projects.create!(id: 1, name: 'gitlab1', path: 'gitlab1')
@@ -42,8 +41,10 @@ describe MigrateStagesStatuses, :migration do
end
def create_job(project:, pipeline:, stage:, status:, **opts)
+ stages = { test: 1, build: 2, deploy: 3}
+
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)
end
end
diff --git a/spec/models/ci/stage_spec.rb b/spec/models/ci/stage_spec.rb
new file mode 100644
index 00000000000..911c468ff1a
--- /dev/null
+++ b/spec/models/ci/stage_spec.rb
@@ -0,0 +1,21 @@
+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