Commit c8f5df38 authored by Alina Mihaila's avatar Alina Mihaila Committed by Alex Kalderimis

Add Datateam Danger plugin

 - Move implementation into a danger plugin
 - Add tests
parent 507613fd
# frozen_string_literal: true
DATA_WAREHOUSE_LABELS = [
"Data Warehouse::Impact Check",
"Data Warehouse::Impacted",
"Data Warehouse::Not Impacted"
].freeze
FILE_PATH_REGEX = %r{((ee|jh)/)?config/metrics(/.+\.yml)}.freeze
PERFORMANCE_INDICATOR_REGEX = %r{gmau|smau|paid_gmau|umau}.freeze
CHANGED_SCHEMA_MESSAGE = <<~MSG
Notification to the Data Team about changes to files with possible impact on Data Warehouse, add label `Data Warehouse::Impact Check`.
/label ~"Data Warehouse::Impact Check"
The following files require a review:
MSG
db_schema_updated = !git.modified_files.grep(%r{\Adb/structure\.sql}).empty?
metrics_definitions_files = git.modified_files.grep(FILE_PATH_REGEX)
data_warehouse_impact_files = metrics_definitions_files.select do |file|
helper.changed_lines(file).grep(PERFORMANCE_INDICATOR_REGEX).any?
end.compact
data_warehouse_impact_files << 'db/structure.sql' if db_schema_updated
no_data_warehouse_labels = (gitlab.mr_labels & DATA_WAREHOUSE_LABELS).empty?
markdown(CHANGED_SCHEMA_MESSAGE + helper.markdown_list(data_warehouse_impact_files)) if data_warehouse_impact_files.any? && no_data_warehouse_labels
markdown(datateam.build_message) if datateam.impacted?
# frozen_string_literal: true
require_relative '../../tooling/danger/datateam'
module Danger
class Datateam < ::Danger::Plugin
include Tooling::Danger::Datateam
end
end
# frozen_string_literal: true
require 'rspec-parameterized'
require 'gitlab-dangerfiles'
require 'gitlab/dangerfiles/spec_helper'
require 'pry'
require_relative '../../../tooling/danger/datateam'
RSpec.describe Tooling::Danger::Datateam do
include_context "with dangerfile"
let(:fake_danger) { DangerSpecHelper.fake_danger.include(described_class) }
let(:datateam) { fake_danger.new(helper: fake_helper) }
describe 'data team danger' do
using RSpec::Parameterized::TableSyntax
where do
{
'with structure.sql changes and no Data Warehouse::Impact Check label' => {
modified_files: %w(db/structure.sql app/models/user.rb),
changed_lines: ['+group_id bigint NOT NULL'],
mr_labels: [],
impacted: true,
impacted_files: %w(db/structure.sql)
},
'with structure.sql changes and Data Warehouse::Impact Check label' => {
modified_files: %w(db/structure.sql),
changed_lines: ['+group_id bigint NOT NULL)'],
mr_labels: ['Data Warehouse::Impact Check'],
impacted: false,
impacted_files: %w(db/structure.sql)
},
'with user model changes' => {
modified_files: %w(app/models/users.rb),
changed_lines: ['+has_one :namespace'],
mr_labels: [],
impacted: false,
impacted_files: []
},
'with perfomance indicator changes and no Data Warehouse::Impact Check label' => {
modified_files: %w(config/metrics/20210216182127_user_secret_detection_jobs.yml app/models/user.rb),
changed_lines: ['+-gmau'],
mr_labels: [],
impacted: true,
impacted_files: %w(config/metrics/20210216182127_user_secret_detection_jobs.yml)
},
'with perfomance indicator changes and Data Warehouse::Impact Check label' => {
modified_files: %w(config/metrics/20210216182127_user_secret_detection_jobs.yml),
changed_lines: ['+-gmau'],
mr_labels: ['Data Warehouse::Impact Check'],
impacted: false,
impacted_files: %w(config/metrics/20210216182127_user_secret_detection_jobs.yml)
},
'with metric file changes and no performance indicator changes' => {
modified_files: %w(config/metrics/20210216182127_user_secret_detection_jobs.yml),
changed_lines: ['-product_stage: growth'],
mr_labels: [],
impacted: false,
impacted_files: []
},
'with metric file changes and no performance indicator changes and other label' => {
modified_files: %w(config/metrics/20210216182127_user_secret_detection_jobs.yml),
changed_lines: ['-product_stage: growth'],
mr_labels: ['type::tooling'],
impacted: false,
impacted_files: []
},
'with performance indicator changes and other label' => {
modified_files: %w(config/metrics/20210216182127_user_secret_detection_jobs.yml app/models/user.rb),
changed_lines: ['+-gmau'],
mr_labels: ['type::tooling'],
impacted: true,
impacted_files: %w(config/metrics/20210216182127_user_secret_detection_jobs.yml)
},
'with performance indicator changes, Data Warehouse::Impact Check and other label' => {
modified_files: %w(config/metrics/20210216182127_user_secret_detection_jobs.yml app/models/user.rb),
changed_lines: ['+-gmau'],
mr_labels: ['type::tooling', 'Data Warehouse::Impact Check'],
impacted: false,
impacted_files: %w(config/metrics/20210216182127_user_secret_detection_jobs.yml)
},
'with performance indicator changes and other labels' => {
modified_files: %w(config/metrics/20210216182127_user_secret_detection_jobs.yml app/models/user.rb),
changed_lines: ['+-gmau'],
mr_labels: ['type::tooling', 'Data Warehouse::Impacted'],
impacted: false,
impacted_files: %w(config/metrics/20210216182127_user_secret_detection_jobs.yml)
}
}
end
with_them do
before do
allow(fake_helper).to receive(:modified_files).and_return(modified_files)
allow(fake_helper).to receive(:changed_lines).and_return(changed_lines)
allow(fake_helper).to receive(:mr_labels).and_return(mr_labels)
allow(fake_helper).to receive(:markdown_list).with(impacted_files).and_return(impacted_files.map { |item| "* `#{item}`" }.join("\n"))
end
it :aggregate_failures do
expect(datateam.impacted?).to be(impacted)
expect(datateam.build_message).to match_expected_message
end
end
end
def match_expected_message
return be_nil unless impacted
start_with(described_class::CHANGED_SCHEMA_MESSAGE).and(include(*impacted_files))
end
end
......@@ -269,7 +269,7 @@ RSpec.describe Tooling::Danger::ProjectHelper do
describe '.local_warning_message' do
it 'returns an informational message with rules that can run' do
expect(described_class.local_warning_message).to eq('==> Only the following Danger rules can be run locally: changelog, ci_config, database, documentation, duplicate_yarn_dependencies, eslint, gitaly, pajamas, pipeline, prettier, product_intelligence, utility_css, vue_shared_documentation')
expect(described_class.local_warning_message).to eq('==> Only the following Danger rules can be run locally: changelog, ci_config, database, documentation, duplicate_yarn_dependencies, eslint, gitaly, pajamas, pipeline, prettier, product_intelligence, utility_css, vue_shared_documentation, datateam')
end
end
......
# frozen_string_literal: true
module Tooling
module Danger
module Datateam
CHANGED_SCHEMA_MESSAGE = <<~MSG
Notification to the Data Team about changes to files with possible impact on Data Warehouse, add label `Data Warehouse::Impact Check`.
/label ~"Data Warehouse::Impact Check"
The following files require a review:
MSG
DATA_WAREHOUSE_SCOPE = 'Data Warehouse::'
FILE_PATH_REGEX = %r{((ee|jh)/)?config/metrics(/.+\.yml)}.freeze
PERFORMANCE_INDICATOR_REGEX = %r{gmau|smau|paid_gmau|umau}.freeze
DATABASE_REGEX = %r{\Adb/structure\.sql}.freeze
STRUCTURE_SQL_FILE = %w(db/structure.sql).freeze
def build_message
return unless impacted?
CHANGED_SCHEMA_MESSAGE + helper.markdown_list(data_warehouse_impact_files)
end
def impacted?
!labelled_as_datawarehouse? && data_warehouse_impact_files.any?
end
private
def data_warehouse_impact_files
@impacted_files ||= (performance_indicator_changed_files + database_changed_files)
end
def labelled_as_datawarehouse?
helper.mr_labels.any? { |label| label.start_with?(DATA_WAREHOUSE_SCOPE) }
end
def performance_indicator_changed_files
metrics_definitions_files = helper.modified_files.grep(FILE_PATH_REGEX)
metrics_definitions_files.select do |file|
helper.changed_lines(file).any? { |change| change =~ PERFORMANCE_INDICATOR_REGEX }
end.compact
end
def database_changes?
!helper.modified_files.grep(DATABASE_REGEX).empty?
end
def database_changed_files
helper.modified_files & STRUCTURE_SQL_FILE
end
end
end
end
......@@ -17,6 +17,7 @@ module Tooling
product_intelligence
utility_css
vue_shared_documentation
datateam
].freeze
CI_ONLY_RULES ||= %w[
......
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