Commit f433fee0 authored by Shinya Maeda's avatar Shinya Maeda

Revert extra validation for duplication between same keys on a submit

parent 6c5c525a
...@@ -33,8 +33,7 @@ class Projects::PipelineSchedulesController < Projects::ApplicationController ...@@ -33,8 +33,7 @@ class Projects::PipelineSchedulesController < Projects::ApplicationController
end end
def update def update
if Ci::CreatePipelineScheduleService if schedule.update(schedule_params)
.new(@project, current_user, schedule_params).update(schedule)
redirect_to namespace_project_pipeline_schedules_path(@project.namespace.becomes(Namespace), @project) redirect_to namespace_project_pipeline_schedules_path(@project.namespace.becomes(Namespace), @project)
else else
render :edit render :edit
......
module Ci module Ci
class CreatePipelineScheduleService < BaseService class CreatePipelineScheduleService < BaseService
def execute def execute
pipeline_schedule = project.pipeline_schedules.build(pipeline_schedule_params) project.pipeline_schedules.create(pipeline_schedule_params)
if variable_keys_duplicated?
pipeline_schedule.errors.add('variables.key', "keys are duplicated")
return pipeline_schedule
end
pipeline_schedule.save
pipeline_schedule
end
def update(pipeline_schedule)
if variable_keys_duplicated?
pipeline_schedule.errors.add('variables.key', "keys are duplicated")
return false
end
pipeline_schedule.update(pipeline_schedule_params)
end end
private private
def pipeline_schedule_params def pipeline_schedule_params
@pipeline_schedule_params ||= params.merge(owner: current_user) params.merge(owner: current_user)
end
def variable_keys_duplicated?
attributes = pipeline_schedule_params['variables_attributes']
return false unless attributes.is_a?(Array)
attributes.map { |v| v['key'] }.uniq.length != attributes.length
end end
end end
end end
...@@ -142,21 +142,22 @@ describe Projects::PipelineSchedulesController do ...@@ -142,21 +142,22 @@ describe Projects::PipelineSchedulesController do
end end
end end
context 'when variables_attributes has two variables and duplicted' do # This test no longer passes, since we removed a custom validation
let(:schedule) do # context 'when variables_attributes has two variables and duplicted' do
basic_param.merge({ # let(:schedule) do
variables_attributes: [ { key: 'AAA', value: 'AAA123' }, { key: 'AAA', value: 'BBB123' } ] # basic_param.merge({
}) # variables_attributes: [ { key: 'AAA', value: 'AAA123' }, { key: 'AAA', value: 'BBB123' } ]
end # })
# end
it 'returns an error that the keys of variable are duplicated' do # it 'returns an error that the keys of variable are duplicated' do
expect { post :create, namespace_id: project.namespace.to_param, project_id: project, schedule: schedule } # expect { post :create, namespace_id: project.namespace.to_param, project_id: project, schedule: schedule }
.to change { Ci::PipelineSchedule.count }.by(0) # .to change { Ci::PipelineSchedule.count }.by(0)
.and change { Ci::PipelineScheduleVariable.count }.by(0) # .and change { Ci::PipelineScheduleVariable.count }.by(0)
expect(assigns(:schedule).errors['variables.key']).not_to be_empty # expect(assigns(:schedule).errors['variables.key']).not_to be_empty
end # end
end # end
end end
describe 'security' do describe 'security' do
...@@ -260,20 +261,21 @@ describe Projects::PipelineSchedulesController do ...@@ -260,20 +261,21 @@ describe Projects::PipelineSchedulesController do
end end
end end
context 'when params include two duplicated variables' do # This test no longer passes, since we removed a custom validation
let(:schedule) do # context 'when params include two duplicated variables' do
basic_param.merge({ # let(:schedule) do
variables_attributes: [ { key: 'AAA', value: 'AAA123' }, { key: 'AAA', value: 'BBB123' } ] # basic_param.merge({
}) # variables_attributes: [ { key: 'AAA', value: 'AAA123' }, { key: 'AAA', value: 'BBB123' } ]
end # })
# end
it 'returns an error that variables are duplciated' do # it 'returns an error that variables are duplciated' do
put :update, namespace_id: project.namespace.to_param, # put :update, namespace_id: project.namespace.to_param,
project_id: project, id: pipeline_schedule, schedule: schedule # project_id: project, id: pipeline_schedule, schedule: schedule
expect(assigns(:schedule).errors['variables.key']).not_to be_empty # expect(assigns(:schedule).errors['variables.key']).not_to be_empty
end # end
end # end
end end
context 'when a pipeline schedule has one variable' do context 'when a pipeline schedule has one variable' do
......
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