Skip to content
Snippets Groups Projects
Verified Commit 05c713c7 authored by Eva Kadlecová's avatar Eva Kadlecová :two: Committed by GitLab
Browse files

Include commit and project ids in Tanuki bot

parent c9fc8975
No related branches found
No related tags found
1 merge request!164075Include commit and project ids in Tanuki bot
......@@ -262,3 +262,5 @@ def append_info_to_payload(payload)
payload[:metadata]['meta.diffs_files_count'] = @diffs.size
end
end
Projects::CommitController.prepend_mod_with('Projects::CommitController')
# frozen_string_literal: true
module EE
module Projects
module CommitController
extend ActiveSupport::Concern
prepended do
before_action only: [:show] do
set_application_context!
end
end
def set_application_context!
::Gitlab::ApplicationContext.push(ai_resource: commit.try(:to_global_id))
end
end
end
end
- return unless ::Gitlab::Llm::TanukiBot.enabled_for?(user: current_user, container: nil)
- resource_id = Gitlab::ApplicationContext.current_context_attribute(:ai_resource).presence
- resource_id = Gitlab::Llm::TanukiBot.resource_id
- project_id = Gitlab::Llm::TanukiBot.project_id
#js-tanuki-bot-chat-app{ data: { user_id: current_user.to_global_id, resource_id: resource_id } }
#js-tanuki-bot-chat-app{ data: { user_id: current_user.to_global_id, resource_id: resource_id, project_id: project_id } }
......@@ -36,6 +36,15 @@ def self.chat_enabled?(user)
true
end
def self.resource_id
Gitlab::ApplicationContext.current_context_attribute(:ai_resource).presence
end
def self.project_id
project_path = Gitlab::ApplicationContext.current_context_attribute(:project).presence
Project.find_by_full_path(project_path).try(:to_global_id) if project_path
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Projects::CommitController, feature_category: :source_code_management do
let_it_be(:project) { create(:project, :repository) }
let_it_be(:user) { create(:user) }
let(:commit) { project.commit("master") }
before_all do
project.add_maintainer(user)
end
describe 'GET show' do
before do
sign_in(user)
end
def go(extra_params = {})
params = {
namespace_id: project.namespace,
project_id: project
}
get :show, params: params.merge(extra_params)
end
it 'sets the ApplicationContext with an ai_resource key' do
go(id: commit.id)
expect(Gitlab::ApplicationContext.current).to include('meta.ai_resource' => commit.try(:to_global_id))
end
end
end
......@@ -133,4 +133,54 @@
end
end
end
describe '.resource_id' do
let(:issue) { build_stubbed(:issue) }
context 'with current context including resource_id' do
before do
Gitlab::ApplicationContext.push(ai_resource: issue.to_global_id)
end
it 'returns the ai_resource from the current context' do
expect(described_class.resource_id).to eq(issue.to_global_id)
end
end
context 'with current context not including resource_id' do
it 'returns nil when ai_resource is not present in the context' do
expect(described_class.resource_id).to be_nil
end
end
end
describe '.project_id' do
let_it_be(:project) { create(:project) }
context 'with current context including project_id' do
before do
::Gitlab::ApplicationContext.push(project: project)
end
it 'returns the global ID of the project when found' do
expect(described_class.project_id).to eq(project.to_global_id)
end
end
context 'when project is not found' do
before do
::Gitlab::ApplicationContext.push(project: 'non_existent_project')
end
it 'returns nil' do
expect(described_class.project_id).to be_nil
end
end
context 'when project is not present in the context' do
it 'returns nil' do
expect(described_class.project_id).to be_nil
end
end
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment