From a03b9bcf25da5f21c3a02daec9c737231a3cf555 Mon Sep 17 00:00:00 2001
From: Tetiana Chupryna <tchupryna@gitlab.com>
Date: Fri, 1 Nov 2024 13:45:55 +0000
Subject: [PATCH] Add feature flag for removing CI tool

---
 .../ci_editor_tool_removed.yml                |  9 ++++++++
 ee/lib/gitlab/llm/completions/chat.rb         |  4 ++++
 .../lib/gitlab/llm/completions/chat_spec.rb   | 23 +++++++++++++++++++
 3 files changed, 36 insertions(+)
 create mode 100644 ee/config/feature_flags/gitlab_com_derisk/ci_editor_tool_removed.yml

diff --git a/ee/config/feature_flags/gitlab_com_derisk/ci_editor_tool_removed.yml b/ee/config/feature_flags/gitlab_com_derisk/ci_editor_tool_removed.yml
new file mode 100644
index 0000000000000..2f5302e62026d
--- /dev/null
+++ b/ee/config/feature_flags/gitlab_com_derisk/ci_editor_tool_removed.yml
@@ -0,0 +1,9 @@
+---
+name: ci_editor_tool_removed
+feature_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/495684
+introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/171017
+rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/501273
+milestone: '17.6'
+group: group::duo chat
+type: gitlab_com_derisk
+default_enabled: false
diff --git a/ee/lib/gitlab/llm/completions/chat.rb b/ee/lib/gitlab/llm/completions/chat.rb
index e44051623ed0b..e5e396be18ca2 100644
--- a/ee/lib/gitlab/llm/completions/chat.rb
+++ b/ee/lib/gitlab/llm/completions/chat.rb
@@ -178,6 +178,10 @@ def push_feature_flags
           Gitlab::AiGateway.push_feature_flag(:ai_commit_reader_for_chat, user)
           Gitlab::AiGateway.push_feature_flag(:expanded_ai_logging, user)
           Gitlab::AiGateway.push_feature_flag(:ai_build_reader_for_chat, user)
+
+          return unless Feature.enabled?(:ci_editor_tool_removed, user)
+
+          Gitlab::AiGateway.push_feature_flag(:ci_editor_tool_removed, user)
         end
       end
     end
diff --git a/ee/spec/lib/gitlab/llm/completions/chat_spec.rb b/ee/spec/lib/gitlab/llm/completions/chat_spec.rb
index 241fa78201306..06d84ec2948df 100644
--- a/ee/spec/lib/gitlab/llm/completions/chat_spec.rb
+++ b/ee/spec/lib/gitlab/llm/completions/chat_spec.rb
@@ -218,6 +218,10 @@
       stub_saas_features(duo_chat_categorize_question: true)
       stub_feature_flags(ai_commit_reader_for_chat: false)
       stub_feature_flags(ai_build_reader_for_chat: false)
+      # This flag is used only on AI Gateway and we need to stub it to
+      # prevent Rubocop from marking this flag as unused
+      # and raising an error
+      stub_feature_flags(ci_editor_tool_removed: false)
     end
 
     context 'when resource is an issue' do
@@ -338,6 +342,25 @@
       end
     end
 
+    context 'with ci editor toll disabled' do
+      before do
+        stub_feature_flags(ci_editor_tool_removed: true)
+        allow(ai_request).to receive(:request)
+        allow(::Gitlab::AiGateway).to receive(:push_feature_flag)
+      end
+
+      it 'pushes feature flag to AI Gateway' do
+        allow_next_instance_of(::Gitlab::Duo::Chat::ReactExecutor) do |instance|
+          allow(instance).to receive(:execute).and_return(answer)
+        end
+
+        expect(::Gitlab::AiGateway).to receive(:push_feature_flag)
+          .with(:ci_editor_tool_removed, user).and_return(:ci_editor_tool_removed)
+
+        subject
+      end
+    end
+
     context 'when message is a slash command' do
       shared_examples_for 'slash command execution' do
         let(:executor) { instance_double(Gitlab::Llm::Chain::Tools::ExplainCode::Executor) }
-- 
GitLab