Commit aa290573 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Add a new service for creating detached CI/CD jobs

parent e7333324
...@@ -50,7 +50,9 @@ class CommitStatus < ActiveRecord::Base ...@@ -50,7 +50,9 @@ class CommitStatus < ActiveRecord::Base
## ##
# We still create some CommitStatuses outside of CreatePipelineService. # We still create some CommitStatuses outside of CreatePipelineService.
# #
# These are pages deployments and external statuses. # These are pages deployments and external statuses. We now handle these
# using CreateJobService, but we still need to ensure that a job has a
# stage assigned. TODO, In future releases we will add model validation.
# #
before_create unless: :importing? do before_create unless: :importing? do
Ci::EnsureStageService.new(project, user).execute(self) do |stage| Ci::EnsureStageService.new(project, user).execute(self) do |stage|
......
module Ci
class CreateJobService < BaseService
def execute(subject = nil)
(subject || yield).tap do |subject|
Ci::EnsureStageService.new(project, current_user)
.execute(subject)
subject.save!
end
end
end
end
...@@ -58,6 +58,7 @@ module Projects ...@@ -58,6 +58,7 @@ module Projects
end end
def create_status def create_status
Ci::CreateJobService.new(project, build.user).execute do
GenericCommitStatus.new( GenericCommitStatus.new(
project: project, project: project,
pipeline: build.pipeline, pipeline: build.pipeline,
...@@ -67,6 +68,7 @@ module Projects ...@@ -67,6 +68,7 @@ module Projects
name: 'pages:deploy' name: 'pages:deploy'
) )
end end
end
def extract_archive!(temp_path) def extract_archive!(temp_path)
if artifacts.ends_with?('.tar.gz') || artifacts.ends_with?('.tgz') if artifacts.ends_with?('.tar.gz') || artifacts.ends_with?('.tgz')
......
...@@ -69,6 +69,7 @@ module API ...@@ -69,6 +69,7 @@ module API
name = params[:name] || params[:context] || 'default' name = params[:name] || params[:context] || 'default'
pipeline = @project.pipeline_for(ref, commit.sha) pipeline = @project.pipeline_for(ref, commit.sha)
unless pipeline unless pipeline
pipeline = @project.pipelines.create!( pipeline = @project.pipelines.create!(
source: :external, source: :external,
...@@ -90,7 +91,14 @@ module API ...@@ -90,7 +91,14 @@ module API
optional_attributes = optional_attributes =
attributes_for_keys(%w[target_url description coverage]) attributes_for_keys(%w[target_url description coverage])
status.update(optional_attributes) if optional_attributes.any? status.assign_attributes(optional_attributes) if optional_attributes.any?
if status.new_record?
Ci::CreateJobService.new(@project, current_user).execute(status)
else
status.save!
end
render_validation_error!(status) if status.invalid? render_validation_error!(status) if status.invalid?
begin begin
......
require 'spec_helper'
describe Ci::CreateJobService, '#execute' do
set(:project) { create(:project, :repository) }
let(:user) { create(:admin) }
let(:status) { build(:ci_build) }
let(:service) { described_class.new(project, user) }
it 'persists job object instantiated in the block' do
expect(service.execute { status }).to be_persisted
end
it 'persists a job instance passed as an argument' do
expect(service.execute(status)).to be_persisted
end
it 'ensures that a job has a stage assigned' do
expect(service.execute(status).stage_id).to be_present
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