debian-mirror-gitlab/tooling/danger/product_intelligence.rb

133 lines
4.8 KiB
Ruby
Raw Normal View History

2021-09-04 01:27:46 +05:30
# frozen_string_literal: true
# rubocop:disable Style/SignalException
module Tooling
module Danger
module ProductIntelligence
2023-03-04 22:38:38 +05:30
METRIC_DIRS = %w[lib/gitlab/usage/metrics/instrumentations ee/lib/gitlab/usage/metrics/instrumentations].freeze
2022-01-26 12:08:38 +05:30
APPROVED_LABEL = 'product intelligence::approved'
REVIEW_LABEL = 'product intelligence::review pending'
2022-06-21 17:19:12 +05:30
CHANGED_FILES_MESSAGE = <<~MSG
2023-03-04 22:38:38 +05:30
For the following files, a review from the [Data team and Product Intelligence team](https://gitlab.com/groups/gitlab-org/analytics-section/product-intelligence/engineers/-/group_members?with_inherited_permissions=exclude) is recommended
Please check the ~"product intelligence" [Service Ping guide](https://docs.gitlab.com/ee/development/service_ping/) or the [Snowplow guide](https://docs.gitlab.com/ee/development/snowplow/).
2022-06-21 17:19:12 +05:30
2023-03-04 22:38:38 +05:30
For MR review guidelines, see the [Service Ping review guidelines](https://docs.gitlab.com/ee/development/service_ping/review_guidelines.html) or the [Snowplow review guidelines](https://docs.gitlab.com/ee/development/snowplow/review_guidelines.html).
2022-06-21 17:19:12 +05:30
2023-03-04 22:38:38 +05:30
%<changed_files>s
MSG
CHANGED_SCOPE_MESSAGE = <<~MSG
The following metrics could be affected by the modified scopes and require ~"product intelligence" review:
2022-06-21 17:19:12 +05:30
MSG
2022-01-26 12:08:38 +05:30
2023-04-23 21:23:45 +05:30
CHANGED_USAGE_DATA_MESSAGE = <<~MSG
Notice that implementing metrics directly in usage_data.rb has been deprecated. ([Deprecated Usage Metrics](https://docs.gitlab.com/ee/development/service_ping/usage_data.html#usage-data-metrics-guide))
Please use [Instrumentation Classes](https://docs.gitlab.com/ee/development/service_ping/metrics_instrumentation.html) instead.
MSG
2021-09-04 01:27:46 +05:30
WORKFLOW_LABELS = [
2022-01-26 12:08:38 +05:30
APPROVED_LABEL,
REVIEW_LABEL
2021-09-04 01:27:46 +05:30
].freeze
2022-06-21 17:19:12 +05:30
def check!
# exit if not matching files or if no product intelligence labels
product_intelligence_paths_to_review = helper.changes_by_category[:product_intelligence]
labels_to_add = missing_labels
return if product_intelligence_paths_to_review.empty? || skip_review?
warn format(CHANGED_FILES_MESSAGE, changed_files: helper.markdown_list(product_intelligence_paths_to_review)) unless has_approved_label?
helper.labels_to_add.concat(labels_to_add) unless labels_to_add.empty?
end
2023-03-04 22:38:38 +05:30
def check_affected_scopes!
metric_scope_list = metric_scope_affected
return if metric_scope_list.empty?
warn CHANGED_SCOPE_MESSAGE + convert_to_table(metric_scope_list)
helper.labels_to_add.concat(missing_labels) unless missing_labels.empty?
end
2023-04-23 21:23:45 +05:30
def check_usage_data_insertions!
usage_data_changes = helper.changed_lines("lib/gitlab/usage_data.rb")
return if usage_data_changes.none? { |change| change.start_with?("+") }
warn format(CHANGED_USAGE_DATA_MESSAGE)
end
2022-06-21 17:19:12 +05:30
private
2023-03-04 22:38:38 +05:30
def convert_to_table(items)
message = "Scope | Affected files |\n"
message += "--- | ----- |\n"
items.each_key do |scope|
affected_files = items[scope]
message += "`#{scope}`| `#{affected_files[0]}` |\n"
affected_files[1..]&.each do |file_name|
message += " | `#{file_name}` |\n"
end
end
message
end
def metric_scope_affected
select_models(helper.modified_files).each_with_object(Hash.new { |h, k| h[k] = [] }) do |file_name, matched_files|
helper.changed_lines(file_name).each do |mod_line, _i|
next unless mod_line =~ /^\+\s+scope :\w+/
affected_scope = mod_line.match(/:\w+/)
next if affected_scope.nil?
affected_class = File.basename(file_name, '.rb').split('_').map(&:capitalize).join
scope_name = "#{affected_class}.#{affected_scope[0][1..]}"
each_metric do |metric_def|
next unless File.read(metric_def).include?("relation { #{scope_name}")
matched_files[scope_name].push(metric_def)
end
end
end
end
def select_models(files)
files.select do |f|
f.start_with?('app/models/', 'ee/app/models/')
end
end
def each_metric(&block)
METRIC_DIRS.each do |dir|
Dir.glob(File.join(dir, '*.rb')).each(&block)
end
end
2021-09-04 01:27:46 +05:30
def missing_labels
2022-01-26 12:08:38 +05:30
return [] unless helper.ci?
2021-09-04 01:27:46 +05:30
labels = []
labels << 'product intelligence' unless helper.mr_has_labels?('product intelligence')
2022-01-26 12:08:38 +05:30
labels << REVIEW_LABEL unless has_workflow_labels?
2021-09-04 01:27:46 +05:30
labels
end
2022-01-26 12:08:38 +05:30
def has_approved_label?
helper.mr_labels.include?(APPROVED_LABEL)
end
def skip_review?
helper.mr_has_labels?('growth experiment')
end
2021-12-11 22:18:48 +05:30
def has_workflow_labels?
(WORKFLOW_LABELS & helper.mr_labels).any?
2021-09-04 01:27:46 +05:30
end
end
end
end