Commit a05bc477 authored by Lin Jen-Shin's avatar Lin Jen-Shin

Use hash to return multiple objects

parent d035d735
module Ci module Ci
class CreateTriggerRequestService module CreateTriggerRequestService
def execute(project, trigger, ref, variables = nil) def self.execute(project, trigger, ref, variables = nil)
trigger_request = trigger.trigger_requests.create(variables: variables) trigger_request = trigger.trigger_requests.create(variables: variables)
pipeline = Ci::CreatePipelineService.new(project, trigger.owner, ref: ref) pipeline = Ci::CreatePipelineService.new(project, trigger.owner, ref: ref)
.execute(:trigger, ignore_skip_ci: true, trigger_request: trigger_request) .execute(:trigger, ignore_skip_ci: true, trigger_request: trigger_request)
trigger_request.pipeline = pipeline { trigger_request: trigger_request,
trigger_request pipeline: pipeline }
end end
end end
end end
...@@ -27,8 +27,8 @@ module API ...@@ -27,8 +27,8 @@ module API
end end
# create request and trigger builds # create request and trigger builds
trigger_request = Ci::CreateTriggerRequestService.new.execute(project, trigger, params[:ref].to_s, variables) result = Ci::CreateTriggerRequestService.execute(project, trigger, params[:ref].to_s, variables)
pipeline = trigger_request.pipeline pipeline = result[:pipeline]
if pipeline.persisted? if pipeline.persisted?
present pipeline, with: Entities::Pipeline present pipeline, with: Entities::Pipeline
......
...@@ -28,11 +28,11 @@ module API ...@@ -28,11 +28,11 @@ module API
end end
# create request and trigger builds # create request and trigger builds
trigger_request = Ci::CreateTriggerRequestService.new.execute(project, trigger, params[:ref].to_s, variables) result = Ci::CreateTriggerRequestService.execute(project, trigger, params[:ref].to_s, variables)
pipeline = trigger_request.pipeline pipeline = result[:pipeline]
if pipeline.persisted? if pipeline.persisted?
present trigger_request, with: ::API::V3::Entities::TriggerRequest present result[:trigger_request], with: ::API::V3::Entities::TriggerRequest
else else
render_validation_error!(pipeline) render_validation_error!(pipeline)
end end
......
...@@ -24,11 +24,11 @@ module Ci ...@@ -24,11 +24,11 @@ module Ci
end end
# create request and trigger builds # create request and trigger builds
trigger_request = Ci::CreateTriggerRequestService.new.execute(project, trigger, params[:ref], variables) result = Ci::CreateTriggerRequestService.execute(project, trigger, params[:ref], variables)
pipeline = trigger_request.pipeline pipeline = result[:pipeline]
if pipeline.persisted? if pipeline.persisted?
present trigger_request, with: Entities::TriggerRequest present result[:trigger_request], with: Entities::TriggerRequest
else else
render_validation_error!(pipeline) render_validation_error!(pipeline)
end end
......
require 'spec_helper' require 'spec_helper'
describe Ci::CreateTriggerRequestService, services: true do describe Ci::CreateTriggerRequestService, services: true do
let(:service) { described_class.new } let(:service) { described_class }
let(:project) { create(:project, :repository) } let(:project) { create(:project, :repository) }
let(:trigger) { create(:ci_trigger, project: project, owner: owner) } let(:trigger) { create(:ci_trigger, project: project, owner: owner) }
let(:owner) { create(:user) } let(:owner) { create(:user) }
...@@ -17,26 +17,26 @@ describe Ci::CreateTriggerRequestService, services: true do ...@@ -17,26 +17,26 @@ describe Ci::CreateTriggerRequestService, services: true do
subject { service.execute(project, trigger, 'master') } subject { service.execute(project, trigger, 'master') }
context 'without owner' do context 'without owner' do
it { expect(subject).to be_kind_of(Ci::TriggerRequest) } it { expect(subject[:trigger_request]).to be_kind_of(Ci::TriggerRequest) }
it { expect(subject.pipeline).to be_kind_of(Ci::Pipeline) } it { expect(subject[:trigger_request].builds.first).to be_kind_of(Ci::Build) }
it { expect(subject.pipeline).to be_trigger } it { expect(subject[:pipeline]).to be_kind_of(Ci::Pipeline) }
it { expect(subject.builds.first).to be_kind_of(Ci::Build) } it { expect(subject[:pipeline]).to be_trigger }
end end
context 'with owner' do context 'with owner' do
it { expect(subject).to be_kind_of(Ci::TriggerRequest) } it { expect(subject[:trigger_request]).to be_kind_of(Ci::TriggerRequest) }
it { expect(subject.pipeline).to be_kind_of(Ci::Pipeline) } it { expect(subject[:trigger_request].builds.first).to be_kind_of(Ci::Build) }
it { expect(subject.pipeline).to be_trigger } it { expect(subject[:trigger_request].builds.first.user).to eq(owner) }
it { expect(subject.pipeline.user).to eq(owner) } it { expect(subject[:pipeline]).to be_kind_of(Ci::Pipeline) }
it { expect(subject.builds.first).to be_kind_of(Ci::Build) } it { expect(subject[:pipeline]).to be_trigger }
it { expect(subject.builds.first.user).to eq(owner) } it { expect(subject[:pipeline].user).to eq(owner) }
end end
end end
context 'no commit for ref' do context 'no commit for ref' do
subject { service.execute(project, trigger, 'other-branch') } subject { service.execute(project, trigger, 'other-branch') }
it { expect(subject.pipeline).not_to be_persisted } it { expect(subject[:pipeline]).not_to be_persisted }
end end
context 'no builds created' do context 'no builds created' do
...@@ -46,7 +46,7 @@ describe Ci::CreateTriggerRequestService, services: true do ...@@ -46,7 +46,7 @@ describe Ci::CreateTriggerRequestService, services: true do
stub_ci_pipeline_yaml_file('script: { only: [develop], script: hello World }') stub_ci_pipeline_yaml_file('script: { only: [develop], script: hello World }')
end end
it { expect(subject.pipeline).not_to be_persisted } it { expect(subject[:pipeline]).not_to be_persisted }
end end
end end
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