summaryrefslogtreecommitdiff
path: root/tooling
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2022-11-28 21:09:36 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2022-11-28 21:09:36 +0000
commite5940143fe1fdb95ed7c3dc6f0a4efdfaaf876c0 (patch)
tree006afb964e1f39ccc5429d8cc91793f6cab4ddf4 /tooling
parent953180403c1798ba42d396742e0691d5772da3a5 (diff)
downloadgitlab-ce-e5940143fe1fdb95ed7c3dc6f0a4efdfaaf876c0.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'tooling')
-rw-r--r--tooling/danger/product_intelligence.rb67
1 files changed, 63 insertions, 4 deletions
diff --git a/tooling/danger/product_intelligence.rb b/tooling/danger/product_intelligence.rb
index 621a7b509b0..58e327408a1 100644
--- a/tooling/danger/product_intelligence.rb
+++ b/tooling/danger/product_intelligence.rb
@@ -4,15 +4,21 @@
module Tooling
module Danger
module ProductIntelligence
+ METRIC_DIRS = %w[lib/gitlab/usage/metrics/instrumentations ee/lib/gitlab/usage/metrics/instrumentations].freeze
APPROVED_LABEL = 'product intelligence::approved'
REVIEW_LABEL = 'product intelligence::review pending'
CHANGED_FILES_MESSAGE = <<~MSG
- 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/).
+ 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/).
- 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).
+ 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).
- %<changed_files>s
+ %<changed_files>s
+
+ MSG
+
+ CHANGED_SCOPE_MESSAGE = <<~MSG
+ The following metrics could be affected by the modified scopes and require ~"product intelligence" review:
MSG
@@ -33,8 +39,61 @@ module Tooling
helper.labels_to_add.concat(labels_to_add) unless labels_to_add.empty?
end
+ 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
+
private
+ 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
+
def missing_labels
return [] unless helper.ci?