Skip to content
代码片段 群组 项目
未验证 提交 07a43b99 编辑于 作者: Vijay Hawoldar's avatar Vijay Hawoldar 提交者: GitLab
浏览文件

Merge branch 'add-summarize-mr-AIGW-service' into 'master'

No related branches found
No related tags found
无相关合并请求
# frozen_string_literal: true
module Gitlab
module Llm
module AiGateway
module Completions
class SummarizeNewMergeRequest < Base
extend ::Gitlab::Utils::Override
include Gitlab::Utils::StrongMemoize
CHARACTER_LIMIT = 2000
override :inputs
def inputs
{ extracted_diff: extracted_diff }
end
private
def extracted_diff
Gitlab::Llm::Utils::MergeRequestTool.extract_diff(
source_project: source_project,
source_branch: options[:source_branch],
target_project: resource,
target_branch: options[:target_branch],
character_limit: CHARACTER_LIMIT
)
end
strong_memoize_attr :extracted_diff
override :valid?
def valid?
super && extracted_diff.present?
end
def source_project
return resource unless options[:source_project_id]
source_project = Project.find_by_id(options[:source_project_id])
return source_project if source_project.present? && user.can?(:create_merge_request_from, source_project)
resource
end
end
end
end
end
end
...@@ -73,6 +73,7 @@ class AiFeaturesCatalogue ...@@ -73,6 +73,7 @@ class AiFeaturesCatalogue
}, },
summarize_new_merge_request: { summarize_new_merge_request: {
service_class: ::Gitlab::Llm::VertexAi::Completions::SummarizeNewMergeRequest, service_class: ::Gitlab::Llm::VertexAi::Completions::SummarizeNewMergeRequest,
aigw_service_class: ::Gitlab::Llm::AiGateway::Completions::SummarizeNewMergeRequest,
prompt_class: ::Gitlab::Llm::Templates::SummarizeNewMergeRequest, prompt_class: ::Gitlab::Llm::Templates::SummarizeNewMergeRequest,
feature_category: :code_review_workflow, feature_category: :code_review_workflow,
execute_method: ::Llm::SummarizeNewMergeRequestService, execute_method: ::Llm::SummarizeNewMergeRequestService,
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Llm::AiGateway::Completions::SummarizeNewMergeRequest, feature_category: :code_review_workflow do
let_it_be(:user) { create(:user) }
let_it_be(:project) { create(:project, :repository) }
let(:prompt_class) { Gitlab::Llm::Templates::SummarizeNewMergeRequest }
let(:prompt_message) do
build(:ai_message, :summarize_new_merge_request, user: user, resource: project, request_id: 'uuid')
end
let(:example_answer) { { "response" => "AI generated merge request summary" } }
let(:example_response) { instance_double(HTTParty::Response, body: example_answer.to_json, success?: true) }
subject(:summarize_new_merge_request) { described_class.new(prompt_message, prompt_class, options).execute }
describe '#execute' do
shared_examples 'makes AI request and publishes response' do
it 'makes AI request and publishes response' do
extracted_diff = Gitlab::Llm::Utils::MergeRequestTool.extract_diff(
source_project: options[:source_project] || project,
source_branch: options[:source_branch],
target_project: project,
target_branch: options[:target_branch],
character_limit: described_class::CHARACTER_LIMIT
)
expect_next_instance_of(Gitlab::Llm::AiGateway::Client) do |client|
expect(client)
.to receive(:complete)
.with(
url: "#{Gitlab::AiGateway.url}/v1/prompts/summarize_new_merge_request",
body: { 'inputs' => { extracted_diff: extracted_diff } }
)
.and_return(example_response)
end
expect(::Gitlab::Llm::GraphqlSubscriptionResponseService).to receive(:new).and_call_original
expect(summarize_new_merge_request[:ai_message].content).to eq(example_answer)
end
end
context 'with valid source branch and project' do
let(:options) do
{
source_branch: 'feature',
target_branch: project.default_branch,
source_project: project
}
end
it_behaves_like 'makes AI request and publishes response'
end
context 'when extracted diff is blank' do
let(:options) do
{
source_branch: 'does-not-exist',
target_branch: project.default_branch,
source_project: project
}
end
it 'does not make an AI request and returns nil' do
expect(Gitlab::Llm::AiGateway::Client).not_to receive(:new)
expect(summarize_new_merge_request).to be_nil
end
end
context 'when source_project_id is invalid' do
let(:options) do
{
source_branch: 'feature',
target_branch: project.default_branch,
source_project_id: non_existing_record_id
}
end
it_behaves_like 'makes AI request and publishes response'
end
end
end
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册