Commit bc2868e2 authored by Nathan Friend's avatar Nathan Friend Committed by Gabriel Mazetto

Handle milestones: nil in releases create service

This commit updates `Releases::CreateService` to handle cases where
`#execute` is passed a Hash with `milestones: nil`. In this case, the
service should behave as if `milestones: []` was passed.
parent 0e8c176d
...@@ -40,12 +40,11 @@ module Mutations ...@@ -40,12 +40,11 @@ module Mutations
authorize :create_release authorize :create_release
def resolve(project_path:, milestones: nil, assets: nil, **scalars) def resolve(project_path:, assets: nil, **scalars)
project = authorized_find!(full_path: project_path) project = authorized_find!(full_path: project_path)
params = { params = {
**scalars, **scalars,
milestones: milestones.presence || [],
assets: assets.to_h assets: assets.to_h
}.with_indifferent_access }.with_indifferent_access
......
...@@ -78,7 +78,7 @@ module Releases ...@@ -78,7 +78,7 @@ module Releases
end end
def param_for_milestone_titles_provided? def param_for_milestone_titles_provided?
params.key?(:milestones) !!params[:milestones]
end end
def execute_hooks(release, action = 'create') def execute_hooks(release, action = 'create')
......
...@@ -167,28 +167,47 @@ RSpec.describe Releases::CreateService do ...@@ -167,28 +167,47 @@ RSpec.describe Releases::CreateService do
end end
end end
context 'when no milestone is passed in' do context 'no milestone association behavior' do
it 'creates a release without a milestone tied to it' do let(:title_1) { 'v1.0' }
expect(params.key?(:milestones)).to be_falsey let(:title_2) { 'v1.0-rc' }
let!(:milestone_1) { create(:milestone, :active, project: project, title: title_1) }
let!(:milestone_2) { create(:milestone, :active, project: project, title: title_2) }
service.execute context 'when no milestones parameter is passed' do
release = project.releases.last it 'creates a release without a milestone tied to it' do
expect(service.param_for_milestone_titles_provided?).to be_falsey
expect(release.milestones).to be_empty service.execute
release = project.releases.last
expect(release.milestones).to be_empty
end
it 'does not create any new MilestoneRelease object' do
expect { service.execute }.not_to change { MilestoneRelease.count }
end
end end
it 'does not create any new MilestoneRelease object' do context 'when an empty array is passed as the milestones parameter' do
expect { service.execute }.not_to change { MilestoneRelease.count } it 'creates a release without a milestone tied to it' do
service = described_class.new(project, user, params.merge!({ milestones: [] }))
service.execute
release = project.releases.last
expect(release.milestones).to be_empty
end
end end
end
context 'when an empty value is passed as a milestone' do context 'when nil is passed as the milestones parameter' do
it 'creates a release without a milestone tied to it' do it 'creates a release without a milestone tied to it' do
service = described_class.new(project, user, params.merge!({ milestones: [] })) expect(service.param_for_milestone_titles_provided?).to be_falsey
service.execute
release = project.releases.last
expect(release.milestones).to be_empty service = described_class.new(project, user, params.merge!({ milestones: nil }))
service.execute
release = project.releases.last
expect(release.milestones).to be_empty
end
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