From cafdd9256a771be5e5b3e2ecdf4b611a9242a50b Mon Sep 17 00:00:00 2001
From: Jonas Larsen <jlarsen@gitlab.com>
Date: Thu, 27 Apr 2023 05:47:09 +0000
Subject: [PATCH] Product Intelligence check also looks at deleted files

Product Intelligence would like to review MRs which removes
metrics or other files that related to Product Intelligence.
---
 spec/tooling/danger/product_intelligence_spec.rb | 13 ++++++++++++-
 tooling/danger/product_intelligence.rb           |  4 ++--
 2 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/spec/tooling/danger/product_intelligence_spec.rb b/spec/tooling/danger/product_intelligence_spec.rb
index c4cd0e5bfb6e..bc6b9debfe0c 100644
--- a/spec/tooling/danger/product_intelligence_spec.rb
+++ b/spec/tooling/danger/product_intelligence_spec.rb
@@ -32,10 +32,12 @@
     let(:approved_label) { 'product intelligence::approved' }
     let(:changed_files) { ['metrics/counts_7d/test_metric.yml'] }
     let(:changed_lines) { ['+tier: ee'] }
+    let(:fake_changes) { instance_double(Gitlab::Dangerfiles::Changes, files: changed_files) }
 
     before do
+      allow(fake_changes).to receive(:by_category).with(:product_intelligence).and_return(fake_changes)
+      allow(fake_helper).to receive(:changes).and_return(fake_changes)
       allow(fake_helper).to receive(:all_changed_files).and_return(changed_files)
-      allow(fake_helper).to receive(:changes_by_category).and_return(product_intelligence: changed_files, database: ['other_files.yml'])
       allow(fake_helper).to receive(:markdown_list).with(changed_files).and_return(markdown_formatted_list)
     end
 
@@ -61,6 +63,15 @@
 
         expect(labels_to_add).to match_array [previous_label_to_add, review_pending_label]
       end
+
+      it 'receives all the changed files by calling the correct helper method', :aggregate_failures do
+        expect(fake_helper).not_to receive(:changes_by_category)
+        expect(fake_helper).to receive(:changes)
+        expect(fake_changes).to receive(:by_category).with(:product_intelligence)
+        expect(fake_changes).to receive(:files)
+
+        subject
+      end
     end
 
     context 'with growth experiment label' do
diff --git a/tooling/danger/product_intelligence.rb b/tooling/danger/product_intelligence.rb
index d25f966504f2..00dc31b9bb98 100644
--- a/tooling/danger/product_intelligence.rb
+++ b/tooling/danger/product_intelligence.rb
@@ -33,8 +33,8 @@ module ProductIntelligence
       ].freeze
 
       def check!
-        # exit if not matching files or if no product intelligence labels
-        product_intelligence_paths_to_review = helper.changes_by_category[:product_intelligence]
+        product_intelligence_paths_to_review = helper.changes.by_category(:product_intelligence).files
+
         labels_to_add = missing_labels
 
         return if product_intelligence_paths_to_review.empty? || skip_review?
-- 
GitLab