summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrzegorz Bizon <grzesiek.bizon@gmail.com>2018-01-24 14:26:03 +0100
committerGrzegorz Bizon <grzesiek.bizon@gmail.com>2018-01-24 14:26:03 +0100
commitaa290573aece6408ad8bb98c7a04257202be5ff8 (patch)
treeb37c17415ce71178ba4907878a1e024f56c9dba0
parente73333242ac42a1020319f5d491daf0647af4c54 (diff)
downloadgitlab-ce-aa290573aece6408ad8bb98c7a04257202be5ff8.tar.gz
Add a new service for creating detached CI/CD jobs
-rw-r--r--app/models/commit_status.rb4
-rw-r--r--app/services/ci/create_job_service.rb12
-rw-r--r--app/services/projects/update_pages_service.rb18
-rw-r--r--lib/api/commit_statuses.rb10
-rw-r--r--spec/services/ci/create_job_service_spec.rb20
5 files changed, 54 insertions, 10 deletions
diff --git a/app/models/commit_status.rb b/app/models/commit_status.rb
index c0263c0b4e2..f010b0ce9db 100644
--- a/app/models/commit_status.rb
+++ b/app/models/commit_status.rb
@@ -50,7 +50,9 @@ class CommitStatus < ActiveRecord::Base
##
# 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
Ci::EnsureStageService.new(project, user).execute(self) do |stage|
diff --git a/app/services/ci/create_job_service.rb b/app/services/ci/create_job_service.rb
new file mode 100644
index 00000000000..683c3557cd0
--- /dev/null
+++ b/app/services/ci/create_job_service.rb
@@ -0,0 +1,12 @@
+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
diff --git a/app/services/projects/update_pages_service.rb b/app/services/projects/update_pages_service.rb
index a773222bf17..63d2b6c76e6 100644
--- a/app/services/projects/update_pages_service.rb
+++ b/app/services/projects/update_pages_service.rb
@@ -58,14 +58,16 @@ module Projects
end
def create_status
- GenericCommitStatus.new(
- project: project,
- pipeline: build.pipeline,
- user: build.user,
- ref: build.ref,
- stage: 'deploy',
- name: 'pages:deploy'
- )
+ Ci::CreateJobService.new(project, build.user).execute do
+ GenericCommitStatus.new(
+ project: project,
+ pipeline: build.pipeline,
+ user: build.user,
+ ref: build.ref,
+ stage: 'deploy',
+ name: 'pages:deploy'
+ )
+ end
end
def extract_archive!(temp_path)
diff --git a/lib/api/commit_statuses.rb b/lib/api/commit_statuses.rb
index 829eef18795..76cb9a8c808 100644
--- a/lib/api/commit_statuses.rb
+++ b/lib/api/commit_statuses.rb
@@ -69,6 +69,7 @@ module API
name = params[:name] || params[:context] || 'default'
pipeline = @project.pipeline_for(ref, commit.sha)
+
unless pipeline
pipeline = @project.pipelines.create!(
source: :external,
@@ -90,7 +91,14 @@ module API
optional_attributes =
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?
begin
diff --git a/spec/services/ci/create_job_service_spec.rb b/spec/services/ci/create_job_service_spec.rb
new file mode 100644
index 00000000000..a7a8032ba28
--- /dev/null
+++ b/spec/services/ci/create_job_service_spec.rb
@@ -0,0 +1,20 @@
+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