Use struct instead of hash

This commit is contained in:
Lin Jen-Shin 2017-07-20 20:17:42 +08:00
parent c9c715cd55
commit e9862a9900
5 changed files with 20 additions and 19 deletions

View file

@ -1,13 +1,14 @@
module Ci
module CreateTriggerRequestService
Result = Struct.new(:trigger_request, :pipeline)
def self.execute(project, trigger, ref, variables = nil)
trigger_request = trigger.trigger_requests.create(variables: variables)
pipeline = Ci::CreatePipelineService.new(project, trigger.owner, ref: ref)
.execute(:trigger, ignore_skip_ci: true, trigger_request: trigger_request)
{ trigger_request: trigger_request,
pipeline: pipeline }
Result.new(trigger_request, pipeline)
end
end
end

View file

@ -28,7 +28,7 @@ module API
# create request and trigger builds
result = Ci::CreateTriggerRequestService.execute(project, trigger, params[:ref].to_s, variables)
pipeline = result[:pipeline]
pipeline = result.pipeline
if pipeline.persisted?
present pipeline, with: Entities::Pipeline

View file

@ -29,10 +29,10 @@ module API
# create request and trigger builds
result = Ci::CreateTriggerRequestService.execute(project, trigger, params[:ref].to_s, variables)
pipeline = result[:pipeline]
pipeline = result.pipeline
if pipeline.persisted?
present result[:trigger_request], with: ::API::V3::Entities::TriggerRequest
present result.trigger_request, with: ::API::V3::Entities::TriggerRequest
else
render_validation_error!(pipeline)
end

View file

@ -25,10 +25,10 @@ module Ci
# create request and trigger builds
result = Ci::CreateTriggerRequestService.execute(project, trigger, params[:ref], variables)
pipeline = result[:pipeline]
pipeline = result.pipeline
if pipeline.persisted?
present result[:trigger_request], with: Entities::TriggerRequest
present result.trigger_request, with: Entities::TriggerRequest
else
render_validation_error!(pipeline)
end

View file

@ -17,26 +17,26 @@ describe Ci::CreateTriggerRequestService, services: true do
subject { service.execute(project, trigger, 'master') }
context 'without owner' do
it { expect(subject[:trigger_request]).to be_kind_of(Ci::TriggerRequest) }
it { expect(subject[:trigger_request].builds.first).to be_kind_of(Ci::Build) }
it { expect(subject[:pipeline]).to be_kind_of(Ci::Pipeline) }
it { expect(subject[:pipeline]).to be_trigger }
it { expect(subject.trigger_request).to be_kind_of(Ci::TriggerRequest) }
it { expect(subject.trigger_request.builds.first).to be_kind_of(Ci::Build) }
it { expect(subject.pipeline).to be_kind_of(Ci::Pipeline) }
it { expect(subject.pipeline).to be_trigger }
end
context 'with owner' do
it { expect(subject[:trigger_request]).to be_kind_of(Ci::TriggerRequest) }
it { expect(subject[:trigger_request].builds.first).to be_kind_of(Ci::Build) }
it { expect(subject[:trigger_request].builds.first.user).to eq(owner) }
it { expect(subject[:pipeline]).to be_kind_of(Ci::Pipeline) }
it { expect(subject[:pipeline]).to be_trigger }
it { expect(subject[:pipeline].user).to eq(owner) }
it { expect(subject.trigger_request).to be_kind_of(Ci::TriggerRequest) }
it { expect(subject.trigger_request.builds.first).to be_kind_of(Ci::Build) }
it { expect(subject.trigger_request.builds.first.user).to eq(owner) }
it { expect(subject.pipeline).to be_kind_of(Ci::Pipeline) }
it { expect(subject.pipeline).to be_trigger }
it { expect(subject.pipeline.user).to eq(owner) }
end
end
context 'no commit for ref' do
subject { service.execute(project, trigger, 'other-branch') }
it { expect(subject[:pipeline]).not_to be_persisted }
it { expect(subject.pipeline).not_to be_persisted }
end
context 'no builds created' do
@ -46,7 +46,7 @@ describe Ci::CreateTriggerRequestService, services: true do
stub_ci_pipeline_yaml_file('script: { only: [develop], script: hello World }')
end
it { expect(subject[:pipeline]).not_to be_persisted }
it { expect(subject.pipeline).not_to be_persisted }
end
end
end