diff --git a/lib/gitlab/changelog/config.rb b/lib/gitlab/changelog/config.rb index 4b147c00cf894fdb93a1a7dab88f42812103b0be..42392d3a529776783a99115758620614ab3a6604 100644 --- a/lib/gitlab/changelog/config.rb +++ b/lib/gitlab/changelog/config.rb @@ -38,17 +38,13 @@ class Config def self.from_git(project, user = nil, path = nil) config_path = path.presence || DEFAULT_FILE_PATH - yaml = project.repository.changelog_config('HEAD', config_path) + config_yaml = project.repository.changelog_config('HEAD', config_path) + config_hash = YAML.safe_load(config_yaml) if config_yaml.present? + return new(project) if config_hash.nil? - if yaml.present? - begin - from_hash(project, YAML.safe_load(yaml), user) - rescue Psych::Exception - raise Error, "#{config_path} does not contain valid YAML" - end - else - new(project) - end + from_hash(project, config_hash, user) + rescue Psych::Exception + raise Error, "#{config_path} does not contain valid YAML" end def self.from_hash(project, hash, user = nil) diff --git a/spec/lib/gitlab/changelog/config_spec.rb b/spec/lib/gitlab/changelog/config_spec.rb index 1a7050f47abc8c256a8fa8a38a73dce1cc431f85..fe9f844f51c47095dd2ad4e3ff1ec3ae04d0bf8f 100644 --- a/spec/lib/gitlab/changelog/config_spec.rb +++ b/spec/lib/gitlab/changelog/config_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -RSpec.describe Gitlab::Changelog::Config do +RSpec.describe Gitlab::Changelog::Config, feature_category: :source_code_management do include ProjectForksHelper let(:project) { build_stubbed(:project) } @@ -57,17 +57,34 @@ end end - context 'when changelog is empty' do - it 'returns the default configuration' do - allow(project.repository) - .to receive(:changelog_config) - .and_return("") + context 'when changelog config is empty' do + subject(:from_git) { described_class.from_git(project) } + + shared_examples 'all attributes match the default config' do + let(:default_config) { described_class.new(project) } + let(:attributes) { %i[date_format categories template tag_regex always_credit_user_ids] } + + it 'does not modify any attributes from the default config' do + attributes.each do |attribute| + expect(from_git.public_send(attribute)).to eql(default_config.public_send(attribute)) + end + end + end + + before do + allow(project.repository).to receive(:changelog_config).and_return(changelog_config_content) + end + + context 'when changelog config does not contain a header' do + let(:changelog_config_content) { "" } + + it_behaves_like 'all attributes match the default config' + end - expect(described_class) - .to receive(:new) - .with(project) + context 'when changelog config contains a header' do + let(:changelog_config_content) { "---\n\n" } - described_class.from_git(project) + it_behaves_like 'all attributes match the default config' end end end