diff options
author | Kamil Trzciński <ayufan@ayufan.eu> | 2017-11-07 12:52:52 +0000 |
---|---|---|
committer | Kamil Trzciński <ayufan@ayufan.eu> | 2017-11-07 12:52:52 +0000 |
commit | ad526918d360cbf5d8ab3efeb0eddf482b9e6e77 (patch) | |
tree | 2d40de788985057716f7cd3d7f04ff7e574fb7ff /app/services | |
parent | 5c136f1d717b037ed8a5c88273ed3ed760b0f1d3 (diff) | |
parent | 0d8b695569010ef958176c8ebf635e56d27d5d41 (diff) | |
download | gitlab-ce-ad526918d360cbf5d8ab3efeb0eddf482b9e6e77.tar.gz |
Merge branch 'fix/gb/ensure-that-job-belongs-to-stage' into 'master'
Make sure that every job has a stage assigned
Closes #37979
See merge request gitlab-org/gitlab-ce!14724
Diffstat (limited to 'app/services')
-rw-r--r-- | app/services/ci/ensure_stage_service.rb | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/app/services/ci/ensure_stage_service.rb b/app/services/ci/ensure_stage_service.rb new file mode 100644 index 00000000000..dc2f49e8db1 --- /dev/null +++ b/app/services/ci/ensure_stage_service.rb @@ -0,0 +1,39 @@ +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 |