Commit 1fa095f9 authored by Kerri Miller's avatar Kerri Miller Committed by Igor Drozdov

Add :default_branch_name as permitted attribute

parent c9a519bb
......@@ -241,7 +241,8 @@ class GroupsController < Groups::ApplicationController
:two_factor_grace_period,
:project_creation_level,
:subgroup_creation_level,
:default_branch_protection
:default_branch_protection,
:default_branch_name
]
end
......
......@@ -3,7 +3,19 @@
class NamespaceSetting < ApplicationRecord
belongs_to :namespace, inverse_of: :namespace_settings
validate :default_branch_name_content
NAMESPACE_SETTINGS_PARAMS = [:default_branch_name].freeze
self.primary_key = :namespace_id
def default_branch_name_content
return if default_branch_name.nil?
if default_branch_name.blank?
errors.add(:default_branch_name, "can not be an empty string")
end
end
end
NamespaceSetting.prepend_if_ee('EE::NamespaceSetting')
......@@ -23,6 +23,8 @@ module Groups
before_assignment_hook(group, params)
handle_namespace_settings
group.assign_attributes(params)
begin
......@@ -40,6 +42,18 @@ module Groups
private
def handle_namespace_settings
settings_params = params.slice(*::NamespaceSetting::NAMESPACE_SETTINGS_PARAMS)
return if settings_params.empty?
::NamespaceSetting::NAMESPACE_SETTINGS_PARAMS.each do |nsp|
params.delete(nsp)
end
::NamespaceSettings::UpdateService.new(current_user, group, settings_params).execute
end
def valid_path_change_with_npm_packages?
return true unless group.packages_feature_enabled?
return true if params[:path].blank?
......
......@@ -9,16 +9,14 @@ module EE
override :execute
def execute
super
unless can_update_prevent_forking?
group.errors.add(
:prevent_forking_outside_group,
s_('GroupSettings|Prevent forking setting was not saved')
)
return
end
super
end
private
......
......@@ -551,6 +551,37 @@ RSpec.describe GroupsController, factory_default: :keep do
end
end
context "updating default_branch_name" do
let(:example_branch_name) { "example_branch_name" }
subject(:update_action) do
put :update,
params: {
id: group.to_param,
group: { default_branch_name: example_branch_name }
}
end
it "updates the attribute" do
expect { subject }
.to change { group.namespace_settings.reload.default_branch_name }
.from(nil)
.to(example_branch_name)
expect(response).to have_gitlab_http_status(:found)
end
context "to empty string" do
let(:example_branch_name) { '' }
it "does not update the attribute" do
subject
expect(group.namespace_settings.reload.default_branch_name).not_to eq('')
end
end
end
context 'when there is a conflicting group path' do
let!(:conflict_group) { create(:group, path: SecureRandom.hex(12) ) }
let!(:old_name) { group.name }
......
......@@ -3,5 +3,45 @@
require 'spec_helper'
RSpec.describe NamespaceSetting, type: :model do
# Relationships
#
it { is_expected.to belong_to(:namespace) }
describe "validations" do
describe "#default_branch_name_content" do
let_it_be(:group) { create(:group) }
let(:namespace_settings) { group.namespace_settings }
shared_examples "doesn't return an error" do
it "doesn't return an error" do
expect(namespace_settings.valid?).to be_truthy
expect(namespace_settings.errors.full_messages).to be_empty
end
end
context "when not set" do
it_behaves_like "doesn't return an error"
end
context "when set" do
before do
namespace_settings.default_branch_name = "example_branch_name"
end
it_behaves_like "doesn't return an error"
end
context "when an empty string" do
before do
namespace_settings.default_branch_name = ''
end
it "returns an error" do
expect(namespace_settings.valid?).to be_falsey
expect(namespace_settings.errors.full_messages).not_to be_empty
end
end
end
end
end
......@@ -33,5 +33,16 @@ RSpec.describe NamespaceSettings::UpdateService do
end.not_to change { NamespaceSetting.count }
end
end
context "updating :default_branch_name" do
let(:example_branch_name) { "example_branch_name" }
let(:settings) { { default_branch_name: example_branch_name } }
it "changes settings" do
expect { service.execute }
.to change { group.namespace_settings.default_branch_name }
.from(nil).to(example_branch_name)
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