Commit 0d136be6 authored by Rémy Coutable's avatar Rémy Coutable

Introduce a new InsightsFeature#insights_config method

Signed-off-by: default avatarRémy Coutable <remy@rymai.me>
parent b89d7c4d
......@@ -12,4 +12,19 @@ module InsightsFeature
def insights_available?
::Feature.enabled?(:insights, self) && feature_available?(:insights)
end
def insights_config
return self.insight.project.insights_config if self.respond_to?(:insight) && self.insight
return unless self.respond_to?(:repository)
return if self.repository.empty?
insights_config_yml = self.repository.insights_config_for(self.repository.root_ref)
return unless insights_config_yml
strong_memoize(:insights_config) do
::Gitlab::Config::Loader::Yaml.new(insights_config_yml).load!
end
rescue Gitlab::Config::Loader::FormatError
nil
end
end
......@@ -400,4 +400,48 @@ describe Group do
let(:entity) { group }
end
end
describe "#insights_config" do
context 'when group has no Insights project configured' do
it 'returns nil' do
expect(group.insights_config).to be_nil
end
end
context 'when group has an Insights project configured without a config file' do
before do
project = create(:project, group: group)
group.create_insight!(project: project)
end
it 'returns nil' do
expect(group.insights_config).to be_nil
end
end
context 'when group has an Insights project configured' do
before do
project = create(:project, :custom_repo, group: group, files: { ::Gitlab::Insights::CONFIG_FILE_PATH => insights_file_content })
group.create_insight!(project: project)
end
context 'with a valid config file' do
let(:insights_file_content) { 'key: monthlyBugsCreated' }
it 'returns the insights config data' do
insights_config = group.insights_config
expect(insights_config).to eq(key: 'monthlyBugsCreated')
end
end
context 'with an invalid config file' do
let(:insights_file_content) { ': foo bar' }
it 'returns the insights config data' do
expect(group.insights_config).to be_nil
end
end
end
end
end
......@@ -1735,6 +1735,38 @@ describe Project do
end
end
describe "#insights_config" do
context 'when project has no Insights config file' do
it 'returns nil' do
expect(create(:project).insights_config).to be_nil
end
end
context 'when project has an Insights config file' do
let(:project) do
create(:project, :custom_repo, files: { ::Gitlab::Insights::CONFIG_FILE_PATH => insights_file_content })
end
context 'with a valid config file' do
let(:insights_file_content) { 'key: monthlyBugsCreated' }
it 'returns the insights config data' do
insights_config = project.insights_config
expect(insights_config).to eq(key: 'monthlyBugsCreated')
end
end
context 'with an invalid config file' do
let(:insights_file_content) { ': foo bar' }
it 'returns the insights config data' do
expect(project.insights_config).to be_nil
end
end
end
end
# Despite stubbing the current node as the primary or secondary, the
# behaviour for EE::Project#lfs_http_url_to_repo() is to call
# Project#lfs_http_url_to_repo() which does not have a Geo context.
......
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