Commit 164b1df5 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Extract ensure stage service from commit status class

parent e2828a60
......@@ -50,13 +50,9 @@ class CommitStatus < ActiveRecord::Base
#
# These are pages deployments and external statuses.
#
before_create do |status|
next if status.stage_id.present? || importing?
ensure_pipeline_stage! do |stage|
status.run_after_commit do
StageUpdateWorker.perform_async(stage.id)
end
before_create unless: :importing? do
Ci::EnsureStageService.new(project, user).execute(self) do |stage|
self.run_after_commit { StageUpdateWorker.perform_async(stage.id) }
end
end
......@@ -188,24 +184,4 @@ class CommitStatus < ActiveRecord::Base
v =~ /\d+/ ? v.to_i : v
end
end
private
def ensure_pipeline_stage!
(find_stage || create_stage!).tap do |stage|
self.stage_id = stage.id
yield stage
end
end
def find_stage
pipeline.stages.find_by(name: stage)
end
def create_stage!
Ci::Stage.create!(name: stage,
pipeline: pipeline,
project: project)
end
end
module Ci
##
# We call this service everytime we persist a CI/CD job.
#
# In most cases a job should already have a stage assigned, but in cases it
# doesn't have we need to either find existing one or create a brand new
# stage.
#
class EnsureStageService < BaseService
def execute(build)
@build = build
return if build.stage_id.present?
return if build.invalid?
ensure_stage.tap do |stage|
build.stage_id = stage.id
yield stage if block_given?
end
end
private
def ensure_stage
find_stage || create_stage
end
def find_stage
@build.pipeline.stages.find_by(name: @build.stage)
end
def create_stage
Ci::Stage.create!(name: @build.stage,
pipeline: @build.pipeline,
project: @build.project)
end
end
end
FactoryGirl.define do
factory :commit_status, class: CommitStatus do
name 'default'
stage 'test'
status 'success'
description 'commit status'
pipeline factory: :ci_pipeline_with_one_job
......
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