Commit 48d1de76 authored by Rémy Coutable's avatar Rémy Coutable

Merge branch '10589-default-insight-config' into 'master'

Add a default insights config

See merge request gitlab-org/gitlab-ee!10458
parents 48a7daf9 eedfdfb7
......@@ -20,6 +20,10 @@ the project that holds your `.gitlab/insights.yml` configuration file:
![group insights configuration](img/insights_group_configuration.png)
If no configuration was set, a [default configuration file](
https://gitlab.com/gitlab-org/gitlab-ee/blob/master/ee/fixtures/insights/ee/fixtures/insights/default.yml)
will be used.
See the [Project's Insights documentation](../../project/insights/index.md) for
more details about the `.gitlab/insights.yml` configuration file.
......
......@@ -28,6 +28,11 @@ NOTE: **Note:**
Once the configuration file is created, you can also
[use it for your project's group](../../group/insights/index.md#configure-your-insights).
NOTE: **Note:**
If the project doesn't have any configuration file, it'll try to use
the group configuration if possible. If the group doesn't have any
configuration, the default configuration will be used.
## Permissions
If you have access to view a project, then you have access to view their
......
......@@ -4,25 +4,57 @@ module InsightsFeature
extend ActiveSupport::Concern
include ::Gitlab::Utils::StrongMemoize
DEFAULT_INSIGHT_CONFIG = 'ee/fixtures/insights/default.yml'
def insights_available?
beta_feature_available?(:insights)
end
def insights_config
def insights_config(follow_group: true)
case self
when Group
insight&.project&.insights_config
# When there's a config file, we use it regardless it's valid or not
if insight&.project&.project_insights_config_yaml
insight.project.insights_config(follow_group: false)
else # When there's nothing, then we use the default
default_insights_config
end
when Project
return if repository.empty?
insights_config_yml = repository.insights_config_for(repository.root_ref)
return unless insights_config_yml
yaml = project_insights_config_yaml
strong_memoize(:insights_config) do
::Gitlab::Config::Loader::Yaml.new(insights_config_yml).load!
rescue Gitlab::Config::Loader::FormatError
nil
# When there's a config file, we use it regardless it's valid or not
if yaml
strong_memoize(:insights_config) do
::Gitlab::Config::Loader::Yaml.new(yaml).load!
rescue Gitlab::Config::Loader::FormatError
nil
end
# When we're following the group and there's a group then we use it
elsif follow_group && group
group.insights_config
# A project might not have a group, then we just use the default
else
default_insights_config
end
end
end
def default_insights_config
strong_memoize(:default_insights_config) do
yaml = File.read(Rails.root.join(DEFAULT_INSIGHT_CONFIG).to_s)
::Gitlab::Config::Loader::Yaml.new(yaml).load!
rescue Gitlab::Config::Loader::FormatError
nil
end
end
protected
def project_insights_config_yaml
strong_memoize(:project_insights_config_yaml) do
next if repository.empty?
repository.insights_config_for(repository.root_ref)
end
end
end
weeklyMerged:
title: Weekly merged merge requests
type: bar
query:
issuable_type: merge_request
issuable_state: merged
group_by: week
monthlyMerged:
title: Monthly merged merge requests
type: bar
query:
issuable_type: merge_request
issuable_state: merged
group_by: month
monthlyByPriority:
title: Monthly issues by priority
type: stacked-bar
query:
issuable_type: issue
collection_labels:
- priority::1
- priority::2
- priority::3
- priority::4
group_by: month
monthlyBySeverity:
title: Monthly issues by severity
type: stacked-bar
query:
issuable_type: issue
collection_labels:
- severity::1
- severity::2
- severity::3
- severity::4
group_by: month
......@@ -403,8 +403,8 @@ describe Group do
describe "#insights_config" do
context 'when group has no Insights project configured' do
it 'returns nil' do
expect(group.insights_config).to be_nil
it 'returns the default config' do
expect(group.insights_config).to eq(group.default_insights_config)
end
end
......@@ -414,8 +414,8 @@ describe Group do
group.create_insight!(project: project)
end
it 'returns nil' do
expect(group.insights_config).to be_nil
it 'returns the default config' do
expect(group.insights_config).to eq(group.default_insights_config)
end
end
......
......@@ -1737,8 +1737,48 @@ describe Project do
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
let(:project) { create(:project) }
it 'returns the project default config' do
expect(project.insights_config).to eq(project.default_insights_config)
end
context 'when the project is inside a group' do
let(:group) { create(:group) }
let(:project) { create(:project, group: group) }
context 'when the group has no Insights config' do
it 'returns the group default config' do
expect(project.insights_config).to eq(group.default_insights_config)
end
end
context 'when the group has an Insights config from another project' do
let(:config_project) do
create(:project, :custom_repo, files: { ::Gitlab::Insights::CONFIG_FILE_PATH => insights_file_content })
end
before do
group.create_insight!(project: config_project)
end
context 'with a valid config file' do
let(:insights_file_content) { 'key: monthlyBugsCreated' }
it 'returns the group config data from the other project' do
expect(project.insights_config).to eq(config_project.insights_config)
expect(project.insights_config).to eq(group.insights_config)
end
end
context 'with an invalid config file' do
let(:insights_file_content) { ': foo bar' }
it 'returns nil' do
expect(project.insights_config).to be_nil
end
end
end
end
end
......@@ -1755,14 +1795,46 @@ describe Project do
expect(insights_config).to eq(key: 'monthlyBugsCreated')
end
context 'when the project is inside a group having another config' do
let(:config_project) do
create(:project, :custom_repo, files: { ::Gitlab::Insights::CONFIG_FILE_PATH => ': foo bar' })
end
before do
project.group = create(:group)
project.group.create_insight!(project: config_project)
end
it 'returns the project insights config data' do
insights_config = project.insights_config
expect(insights_config).to eq(key: 'monthlyBugsCreated')
end
end
end
context 'with an invalid config file' do
let(:insights_file_content) { ': foo bar' }
it 'returns the insights config data' do
it 'returns nil' do
expect(project.insights_config).to be_nil
end
context 'when the project is inside a group having another config' do
let(:config_project) do
create(:project, :custom_repo, files: { ::Gitlab::Insights::CONFIG_FILE_PATH => 'key: monthlyBugsCreated' })
end
before do
project.group = create(:group)
project.group.create_insight!(project: config_project)
end
it 'returns nil' do
expect(project.insights_config).to be_nil
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