Commit 8b158075 authored by Nick Thomas's avatar Nick Thomas

Merge branch...

Merge branch '39505-extend-the-ability-to-clone-a-dashboard-so-it-would-copy-the-custom-metrics' into 'master'

Extend the ability to clone a dashboard so it would copy the custom metrics

See merge request gitlab-org/gitlab!21923
parents 50d7444c 849120fe
...@@ -8,8 +8,18 @@ module Metrics ...@@ -8,8 +8,18 @@ module Metrics
ALLOWED_FILE_TYPE = '.yml' ALLOWED_FILE_TYPE = '.yml'
USER_DASHBOARDS_DIR = ::Metrics::Dashboard::ProjectDashboardService::DASHBOARD_ROOT USER_DASHBOARDS_DIR = ::Metrics::Dashboard::ProjectDashboardService::DASHBOARD_ROOT
def self.allowed_dashboard_templates class << self
@allowed_dashboard_templates ||= Set[::Metrics::Dashboard::SystemDashboardService::DASHBOARD_PATH].freeze def allowed_dashboard_templates
@allowed_dashboard_templates ||= Set[::Metrics::Dashboard::SystemDashboardService::DASHBOARD_PATH].freeze
end
def sequences
@sequences ||= {
::Metrics::Dashboard::SystemDashboardService::DASHBOARD_PATH => [::Gitlab::Metrics::Dashboard::Stages::CommonMetricsInserter,
::Gitlab::Metrics::Dashboard::Stages::ProjectMetricsInserter,
::Gitlab::Metrics::Dashboard::Stages::Sorter].freeze
}.freeze
end
end end
def execute def execute
...@@ -92,7 +102,9 @@ module Metrics ...@@ -92,7 +102,9 @@ module Metrics
end end
def new_dashboard_content def new_dashboard_content
File.read(Rails.root.join(dashboard_template)) ::Gitlab::Metrics::Dashboard::Processor
.new(project, raw_dashboard, sequence, {})
.process.deep_stringify_keys.to_yaml
end end
def repository def repository
...@@ -106,6 +118,14 @@ module Metrics ...@@ -106,6 +118,14 @@ module Metrics
result result
end end
end end
def raw_dashboard
YAML.safe_load(File.read(Rails.root.join(dashboard_template)))
end
def sequence
self.class.sequences[dashboard_template]
end
end end
end end
end end
......
---
title: Extends 'Duplicate dashboard' feature, by including custom metrics added to
GitLab-defined dashboards.
merge_request: 21923
author:
type: added
...@@ -15,6 +15,12 @@ module EE ...@@ -15,6 +15,12 @@ module EE
def allowed_dashboard_templates def allowed_dashboard_templates
@allowed_dashboard_templates ||= (Set[::Metrics::Dashboard::ClusterDashboardService::DASHBOARD_PATH] + super).freeze @allowed_dashboard_templates ||= (Set[::Metrics::Dashboard::ClusterDashboardService::DASHBOARD_PATH] + super).freeze
end end
override :sequences
def sequences
super.merge(::Metrics::Dashboard::ClusterDashboardService::DASHBOARD_PATH => [::Gitlab::Metrics::Dashboard::Stages::CommonMetricsInserter,
::Gitlab::Metrics::Dashboard::Stages::Sorter].freeze)
end
end end
end end
end end
......
...@@ -3,11 +3,15 @@ ...@@ -3,11 +3,15 @@
require 'spec_helper' require 'spec_helper'
describe Metrics::Dashboard::CloneDashboardService, :use_clean_rails_memory_store_caching do describe Metrics::Dashboard::CloneDashboardService, :use_clean_rails_memory_store_caching do
STAGES = ::Gitlab::Metrics::Dashboard::Stages
set(:user) { create(:user) } set(:user) { create(:user) }
set(:project) { create(:project, :repository) } set(:project) { create(:project, :repository) }
set(:environment) { create(:environment, project: project) } set(:environment) { create(:environment, project: project) }
describe '#execute' do describe '#execute' do
subject(:service_call) { described_class.new(project, user, params).execute }
context 'with rights to push to the repository' do context 'with rights to push to the repository' do
before do before do
project.add_maintainer(user) project.add_maintainer(user)
...@@ -17,37 +21,18 @@ describe Metrics::Dashboard::CloneDashboardService, :use_clean_rails_memory_stor ...@@ -17,37 +21,18 @@ describe Metrics::Dashboard::CloneDashboardService, :use_clean_rails_memory_stor
let(:commit_message) { 'test' } let(:commit_message) { 'test' }
let(:branch) { "#{Time.current.to_i}_dashboard_new_branch" } let(:branch) { "#{Time.current.to_i}_dashboard_new_branch" }
let(:file_name) { 'custom_dashboard.yml' } let(:file_name) { 'custom_dashboard.yml' }
let(:file_content_hash) { YAML.safe_load(File.read(dashboard)) }
[::Metrics::Dashboard::SystemDashboardService::DASHBOARD_PATH, ::Metrics::Dashboard::ClusterDashboardService::DASHBOARD_PATH].each do |dashboard_template| let(:params) do
context "dashboard template #{dashboard_template}" do {
let(:dashboard) { dashboard_template } dashboard: dashboard,
let(:params) do file_name: file_name,
{ commit_message: commit_message,
dashboard: dashboard, branch: branch
file_name: file_name, }
commit_message: commit_message,
branch: branch
}
end
it 'delegates commit creation to Files::CreateService', :aggregate_failures do
dashboard_attrs = {
commit_message: commit_message,
branch_name: branch,
start_branch: 'master',
encoding: 'text',
file_path: '.gitlab/dashboards/custom_dashboard.yml',
file_content: File.read(dashboard)
}
service_instance = instance_double(::Files::CreateService)
expect(::Files::CreateService).to receive(:new).with(project, user, dashboard_attrs).and_return(service_instance)
expect(service_instance).to receive(:execute).and_return(status: :success)
described_class.new(project, user, params).execute
end
end
end end
it_behaves_like 'valid dashboard cloning process', ::Metrics::Dashboard::SystemDashboardService::DASHBOARD_PATH, [STAGES::CommonMetricsInserter, STAGES::ProjectMetricsInserter, STAGES::Sorter]
it_behaves_like 'valid dashboard cloning process', ::Metrics::Dashboard::ClusterDashboardService::DASHBOARD_PATH, [STAGES::CommonMetricsInserter, STAGES::Sorter]
end end
end end
end end
......
...@@ -5,6 +5,8 @@ require 'spec_helper' ...@@ -5,6 +5,8 @@ require 'spec_helper'
describe Metrics::Dashboard::CloneDashboardService, :use_clean_rails_memory_store_caching do describe Metrics::Dashboard::CloneDashboardService, :use_clean_rails_memory_store_caching do
include MetricsDashboardHelpers include MetricsDashboardHelpers
STAGES = ::Gitlab::Metrics::Dashboard::Stages
set(:user) { create(:user) } set(:user) { create(:user) }
set(:project) { create(:project, :repository) } set(:project) { create(:project, :repository) }
set(:environment) { create(:environment, project: project) } set(:environment) { create(:environment, project: project) }
...@@ -16,6 +18,7 @@ describe Metrics::Dashboard::CloneDashboardService, :use_clean_rails_memory_stor ...@@ -16,6 +18,7 @@ describe Metrics::Dashboard::CloneDashboardService, :use_clean_rails_memory_stor
let(:branch) { "dashboard_new_branch" } let(:branch) { "dashboard_new_branch" }
let(:dashboard) { 'config/prometheus/common_metrics.yml' } let(:dashboard) { 'config/prometheus/common_metrics.yml' }
let(:file_name) { 'custom_dashboard.yml' } let(:file_name) { 'custom_dashboard.yml' }
let(:file_content_hash) { YAML.safe_load(File.read(dashboard)) }
let(:params) do let(:params) do
{ {
dashboard: dashboard, dashboard: dashboard,
...@@ -25,17 +28,6 @@ describe Metrics::Dashboard::CloneDashboardService, :use_clean_rails_memory_stor ...@@ -25,17 +28,6 @@ describe Metrics::Dashboard::CloneDashboardService, :use_clean_rails_memory_stor
} }
end end
let(:dashboard_attrs) do
{
commit_message: commit_message,
branch_name: branch,
start_branch: project.default_branch,
encoding: 'text',
file_path: ".gitlab/dashboards/#{file_name}",
file_content: File.read(dashboard)
}
end
context 'user does not have push right to repository' do context 'user does not have push right to repository' do
it_behaves_like 'misconfigured dashboard service response', :forbidden, %q(You can't commit to this project) it_behaves_like 'misconfigured dashboard service response', :forbidden, %q(You can't commit to this project)
end end
...@@ -72,11 +64,12 @@ describe Metrics::Dashboard::CloneDashboardService, :use_clean_rails_memory_stor ...@@ -72,11 +64,12 @@ describe Metrics::Dashboard::CloneDashboardService, :use_clean_rails_memory_stor
start_branch: project.default_branch, start_branch: project.default_branch,
encoding: 'text', encoding: 'text',
file_path: ".gitlab/dashboards/custom_dashboard.yml", file_path: ".gitlab/dashboards/custom_dashboard.yml",
file_content: File.read(dashboard) file_content: file_content_hash.to_yaml
} }
end end
it 'strips target file name to safe value', :aggregate_failures do it 'strips target file name to safe value', :aggregate_failures do
allow(::Gitlab::Metrics::Dashboard::Processor).to receive(:new).and_return(double(process: file_content_hash))
service_instance = instance_double(::Files::CreateService) service_instance = instance_double(::Files::CreateService)
expect(::Files::CreateService).to receive(:new).with(project, user, dashboard_attrs).and_return(service_instance) expect(::Files::CreateService).to receive(:new).with(project, user, dashboard_attrs).and_return(service_instance)
expect(service_instance).to receive(:execute).and_return(status: :success) expect(service_instance).to receive(:execute).and_return(status: :success)
...@@ -86,14 +79,12 @@ describe Metrics::Dashboard::CloneDashboardService, :use_clean_rails_memory_stor ...@@ -86,14 +79,12 @@ describe Metrics::Dashboard::CloneDashboardService, :use_clean_rails_memory_stor
end end
context 'valid parameters' do context 'valid parameters' do
it 'delegates commit creation to Files::CreateService', :aggregate_failures do before do
service_instance = instance_double(::Files::CreateService) allow(::Gitlab::Metrics::Dashboard::Processor).to receive(:new).and_return(double(process: file_content_hash))
expect(::Files::CreateService).to receive(:new).with(project, user, dashboard_attrs).and_return(service_instance)
expect(service_instance).to receive(:execute).and_return(status: :success)
service_call
end end
it_behaves_like 'valid dashboard cloning process', ::Metrics::Dashboard::SystemDashboardService::DASHBOARD_PATH, [STAGES::CommonMetricsInserter, STAGES::ProjectMetricsInserter, STAGES::Sorter]
context 'selected branch already exists' do context 'selected branch already exists' do
let(:branch) { 'existing_branch' } let(:branch) { 'existing_branch' }
......
...@@ -50,3 +50,38 @@ RSpec.shared_examples 'raises error for users with insufficient permissions' do ...@@ -50,3 +50,38 @@ RSpec.shared_examples 'raises error for users with insufficient permissions' do
it_behaves_like 'misconfigured dashboard service response', :unauthorized it_behaves_like 'misconfigured dashboard service response', :unauthorized
end end
end end
RSpec.shared_examples 'valid dashboard cloning process' do |dashboard_template, sequence|
context "dashboard template: #{dashboard_template}" do
let(:dashboard) { dashboard_template }
let(:dashboard_attrs) do
{
commit_message: commit_message,
branch_name: branch,
start_branch: project.default_branch,
encoding: 'text',
file_path: ".gitlab/dashboards/#{file_name}",
file_content: file_content_hash.to_yaml
}
end
it 'delegates commit creation to Files::CreateService', :aggregate_failures do
service_instance = instance_double(::Files::CreateService)
expect(::Files::CreateService).to receive(:new).with(project, user, dashboard_attrs).and_return(service_instance)
expect(service_instance).to receive(:execute).and_return(status: :success)
service_call
end
context 'user has defined custom metrics' do
it 'uses external service to includes them into new file content', :aggregate_failures do
service_instance = double(::Gitlab::Metrics::Dashboard::Processor)
expect(::Gitlab::Metrics::Dashboard::Processor).to receive(:new).with(project, file_content_hash, sequence, {}).and_return(service_instance)
expect(service_instance).to receive(:process).and_return(file_content_hash)
expect(::Files::CreateService).to receive(:new).with(project, user, dashboard_attrs).and_return(double(execute: { status: :success }))
service_call
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