Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: allow Github Helper access to commits (#36999)

Sam0d4cf7863353
diff --git a/plugins/discourse-ai/config/locales/server.en.yml b/plugins/discourse-ai/config/locales/server.en.yml
index db3ddbe74ef..96a80d9fa98 100644
--- a/plugins/discourse-ai/config/locales/server.en.yml
+++ b/plugins/discourse-ai/config/locales/server.en.yml
@@ -517,7 +517,7 @@ en:
github_search_files: "GitHub search files"
github_search_code: "GitHub code search"
github_file_content: "GitHub file content"
- github_pull_request_diff: "GitHub pull request diff"
+ github_diff: "GitHub diff"
random_picker: "Random Picker"
categories: "List categories"
flag_post: "Flag post"
@@ -546,7 +546,7 @@ en:
github_search_code: "Search for code in a GitHub repository"
github_search_files: "Search for files in a GitHub repository"
github_file_content: "Retrieve content of files from a GitHub repository"
- github_pull_request_diff: "Retrieve a GitHub pull request diff"
+ github_diff: "Retrieve a GitHub pull request or commit diff"
random_picker: "Pick a random number or a random element of a list"
categories: "List all publicly visible categories on the forum"
flag_post: "Flag the current post for staff review"
@@ -573,7 +573,7 @@ en:
web_browser: "Reading <a href='%{url}'>%{url}</a>"
github_search_files: "Searched for '%{keywords}' in %{repo}/%{branch}"
github_search_code: "Searched for '%{query}' in %{repo}"
- github_pull_request_diff: "<a href='%{url}'>%{repo} %{pull_id}</a>"
+ github_diff: "<a href='%{url}'>%{repo} %{ref}</a>"
github_file_content: "Retrieved content of %{file_paths} from %{repo_name}@%{branch}"
random_picker: "Picking from %{options}, picked: %{result}"
flag_post: "Flagging post %{post_id} (verdict: %{verdict})"
diff --git a/plugins/discourse-ai/db/migrate/20260108002529_rename_github_pull_request_diff_to_github_diff.rb b/plugins/discourse-ai/db/migrate/20260108002529_rename_github_pull_request_diff_to_github_diff.rb
new file mode 100644
index 00000000000..b65baadb8a0
--- /dev/null
+++ b/plugins/discourse-ai/db/migrate/20260108002529_rename_github_pull_request_diff_to_github_diff.rb
@@ -0,0 +1,19 @@
+# frozen_string_literal: true
+
+class RenameGithubPullRequestDiffToGithubDiff < ActiveRecord::Migration[8.0]
+ def up
+ execute <<~SQL
+ UPDATE ai_personas
+ SET tools = REPLACE(tools::text, '"GithubPullRequestDiff"', '"GithubDiff"')::json
+ WHERE tools::text LIKE '%GithubPullRequestDiff%'
+ SQL
+ end
+
+ def down
+ execute <<~SQL
+ UPDATE ai_personas
+ SET tools = REPLACE(tools::text, '"GithubDiff"', '"GithubPullRequestDiff"')::json
+ WHERE tools::text LIKE '%GithubDiff%'
+ SQL
+ end
+end
diff --git a/plugins/discourse-ai/lib/personas/github_helper.rb b/plugins/discourse-ai/lib/personas/github_helper.rb
index 2c08b30b851..e7823bfb17b 100644
--- a/plugins/discourse-ai/lib/personas/github_helper.rb
+++ b/plugins/discourse-ai/lib/personas/github_helper.rb
@@ -6,7 +6,7 @@ module DiscourseAi
def tools
[
Tools::GithubFileContent,
- Tools::GithubPullRequestDiff,
+ Tools::GithubDiff,
Tools::GithubSearchCode,
Tools::GithubSearchFiles,
]
diff --git a/plugins/discourse-ai/lib/personas/persona.rb b/plugins/discourse-ai/lib/personas/persona.rb
index ff0fa7565e7..129473333a7 100644
--- a/plugins/discourse-ai/lib/personas/persona.rb
+++ b/plugins/discourse-ai/lib/personas/persona.rb
@@ -120,7 +120,7 @@ module DiscourseAi
Tools::RandomPicker,
Tools::DiscourseMetaSearch,
Tools::GithubFileContent,
- Tools::GithubPullRequestDiff,
+ Tools::GithubDiff,
Tools::GithubSearchFiles,
Tools::WebBrowser,
Tools::JavascriptEvaluator,
diff --git a/plugins/discourse-ai/lib/personas/tools/github_pull_request_diff.rb b/plugins/discourse-ai/lib/personas/tools/github_diff.rb
similarity index 52%
rename from plugins/discourse-ai/lib/personas/tools/github_pull_request_diff.rb
rename to plugins/discourse-ai/lib/personas/tools/github_diff.rb
index afbe51f9219..818b5a7ee97 100644
--- a/plugins/discourse-ai/lib/personas/tools/github_pull_request_diff.rb
+++ b/plugins/discourse-ai/lib/personas/tools/github_diff.rb
@@ -3,13 +3,13 @@
module DiscourseAi
module Personas
module Tools
- class GithubPullRequestDiff < Tool
+ class GithubDiff < Tool
LARGE_OBJECT_THRESHOLD = 30_000
def self.signature
{
name: name,
- description: "Retrieves the diff for a GitHub pull request",
+ description: "Retrieves the diff for a GitHub pull request or commit",
parameters: [
{
name: "repo",
@@ -19,16 +19,22 @@ module DiscourseAi
},
{
name: "pull_id",
- description: "The ID of the pull request",
+ description: "The pull request number (use this OR sha, not both)",
type: "integer",
- required: true,
+ required: false,
+ },
+ {
+ name: "sha",
+ description: "The commit SHA (use this OR pull_id, not both)",
+ type: "string",
+ required: false,
},
],
}
end
def self.name
- "github_pull_request_diff"
+ "github_diff"
end
def repo
@@ -39,83 +45,30 @@ module DiscourseAi
parameters[:pull_id]
end
+ def sha
+ parameters[:sha]
+ end
+
def url
@url
end
def invoke
- api_url = "https://api.github.com/repos/#{repo}/pulls/#{pull_id}"
- @url = "https://github.com/#{repo}/pull/#{pull_id}"
-
- pr_info = nil
- diff_body = nil
- response_code = "unknown error"
-
- send_http_request(
- api_url,
- headers: {
- "Accept" => "application/json",
- },
- authenticate_github: true,
- ) do |response|
- response_code = response.code
- pr_info = JSON.parse(read_response_body(response)) if response_code == "200"
- end
-
- if response_code == "200"
- send_http_request(
- api_url,
- headers: {
- "Accept" => "application/vnd.github.v3.diff",
- },
- authenticate_github: true,
- ) do |response|
- response_code = response.code
- diff_body = read_response_body(response)
- end
- end
-
- if response_code == "200" && pr_info && diff_body
- diff = diff_body
- diff = self.class.sort_and_shorten_diff(diff)
- diff = truncate(diff, max_length: 20_000, percent_length: 0.3, llm: llm)
-
- source_repo = pr_info.dig("head", "repo", "full_name")
- source_branch = pr_info.dig("head", "ref")
- source_sha = pr_info.dig("head", "sha")
+ return { error: "Must provide either pull_id or sha" } if pull_id.blank? && sha.blank?
- {
- diff: diff,
- pr_info: {
- title: pr_info["title"],
- state: pr_info["state"],
- source: {
- repo: source_repo,
- branch: source_branch,
- sha: source_sha,
- url: "https://github.com/#{source_repo}/tree/#{source_branch}",
- },
- target: {
- repo: pr_info["base"]["repo"]["full_name"],
- branch: pr_info["base"]["ref"],
- },
- author: pr_info["user"]["login"],
- created_at: pr_info["created_at"],
- updated_at: pr_info["updated_at"],
- },
- }
- else
- { error: "Failed to retrieve the PR information. Status code: #{response_code}" }
- end
+ # Prioritize sha if present (LLMs sometimes pass both)
+ sha.present? ? fetch_commit : fetch_pull_request
end
def description_args
- { repo: repo, pull_id: pull_id, url: url }
+ if sha.present?
+ { repo: repo, ref: sha, url: url }
+ else
+ { repo: repo, ref: pull_id, url: url }
+ end
end
def self.sort_and_shorten_diff(diff, threshold: LARGE_OBJECT_THRESHOLD)
- # This regex matches the start of a new file in the diff,
- # capturing the file paths for later use.
file_start_regex = /^diff --git.*/
prev_start = -1
@@ -124,7 +77,7 @@ module DiscourseAi
split = []
diff.scan(file_start_regex) do |match|
- match_start = $~.offset(0)[0] # Get the start position of this match
+ match_start = $~.offset(0)[0]
if prev_start != -1
full_diff = diff[prev_start...match_start]
@@ -149,6 +102,105 @@ module DiscourseAi
end
.join("\n")
end
+
+ private
+
+ def fetch_pull_request
+ api_url = "https://api.github.com/repos/#{repo}/pulls/#{pull_id}"
+ @url = "https://github.com/#{repo}/pull/#{pull_id}"
+
+ fetch_diff(api_url, "PR") do |info, diff|
+ source_repo = info.dig("head", "repo", "full_name")
+ source_branch = info.dig("head", "ref")
+ source_sha = info.dig("head", "sha")
+
+ {
+ type: "pull_request",
+ diff: diff,
+ pr_info: {
+ title: info["title"],
+ state: info["state"],
+ source: {
+ repo: source_repo,
+ branch: source_branch,
+ sha: source_sha,
+ url: "https://github.com/#{source_repo}/tree/#{source_branch}",
+ },
+ target: {
+ repo: info["base"]["repo"]["full_name"],
+ branch: info["base"]["ref"],
+ },
+ author: info["user"]["login"],
+ created_at: info["created_at"],
+ updated_at: info["updated_at"],
+ },
+ }
+ end
+ end
+
+ def fetch_commit
+ api_url = "https://api.github.com/repos/#{repo}/commits/#{sha}"
+ @url = "https://github.com/#{repo}/commit/#{sha}"
+
+ fetch_diff(api_url, "commit") do |info, diff|
+ {
+ type: "commit",
+ diff: diff,
+ commit_info: {
+ sha: info["sha"],
+ message: info.dig("commit", "message"),
+ author: info.dig("commit", "author", "name"),
+ author_login: info.dig("author", "login"),
+ date: info.dig("commit", "author", "date"),
+ url: @url,
+ stats: {
+ additions: info.dig("stats", "additions"),
+ deletions: info.dig("stats", "deletions"),
+ total: info.dig("stats", "total"),
+ },
+ files_changed: info["files"]&.length || 0,
+ },
+ }
+ end
+ end
+
+ def fetch_diff(api_url, type)
+ info = nil
+ diff_body = nil
+ response_code = "unknown error"
+
+ send_http_request(
+ api_url,
+ headers: {
+ "Accept" => "application/json",
+ },
+ authenticate_github: true,
+ ) do |response|
+ response_code = response.code
+ info = JSON.parse(read_response_body(response)) if response_code == "200"
+ end
+
+ if response_code == "200"
+ send_http_request(
+ api_url,
+ headers: {
+ "Accept" => "application/vnd.github.v3.diff",
+ },
+ authenticate_github: true,
+ ) do |response|
+ response_code = response.code
+ diff_body = read_response_body(response)
+ end
+ end
+
+ if response_code == "200" && info && diff_body
+ diff = self.class.sort_and_shorten_diff(diff_body)
+ diff = truncate(diff, max_length: 20_000, percent_length: 0.3, llm: llm)
+ yield(info, diff)
+ else
+ { error: "Failed to retrieve the #{type} information. Status code: #{response_code}" }
+ end
+ end
end
end
end
diff --git a/plugins/discourse-ai/spec/lib/personas/tools/github_diff_spec.rb b/plugins/discourse-ai/spec/lib/personas/tools/github_diff_spec.rb
new file mode 100644
index 00000000000..eb1754a1302
--- /dev/null
+++ b/plugins/discourse-ai/spec/lib/personas/tools/github_diff_spec.rb
@@ -0,0 +1,277 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseAi::Personas::Tools::GithubDiff do
+ let(:bot_user) { Fabricate(:user) }
+ fab!(:llm_model)
+ let(:llm) { DiscourseAi::Completions::Llm.proxy(llm_model) }
+
+ before { enable_current_plugin }
+
+ context "with #sort_and_shorten_diff" do
+ it "sorts and shortens the diff without dropping data" do
+ diff = <<~DIFF
+ diff --git a/src/lib.rs b/src/lib.rs
+ index b466edd..66b068f 100644
+ --- a/src/lib.rs
+ +++ b/src/lib.rs
+ this is a longer diff
+ this is a longer diff
+ this is a longer diff
+ diff --git a/tests/test_encoding.py b/tests/test_encoding.py
+ index 27b2192..9f31319 100644
+ --- a/tests/test_encoding.py
+ +++ b/tests/test_encoding.py
+ short diff
+ DIFF
+
+ sorted_diff = described_class.sort_and_shorten_diff(diff)
+
+ expect(sorted_diff).to eq(<<~DIFF)
+ diff --git a/tests/test_encoding.py b/tests/test_encoding.py
+ index 27b2192..9f31319 100644
+ --- a/tests/test_encoding.py
+ +++ b/tests/test_encoding.py
+ short diff
+
+ diff --git a/src/lib.rs b/src/lib.rs
+ index b466edd..66b068f 100644
+ --- a/src/lib.rs
+ +++ b/src/lib.rs
+ this is a longer diff
+ this is a longer diff
+ this is a longer diff
+ DIFF
+ end
+ end
+
+ context "with parameter validation" do
+ let(:repo) { "owner/repo" }
+
+ it "prioritizes sha when both pull_id and sha provided" do
+ tool =
+ described_class.new(
+ { repo: repo, pull_id: 123, sha: "abc123" },
+ bot_user: bot_user,
+ llm: llm,
+ )
+
+ stub_request(:get, "https://api.github.com/repos/#{repo}/commits/abc123").with(
+ headers: {
+ "Accept" => "application/json",
+ },
+ ).to_return(status: 404)
+
+ result = tool.invoke
+ expect(result[:error]).to include("commit")
+ end
+
+ it "returns error when neither pull_id nor sha provided" do
+ tool = described_class.new({ repo: repo }, bot_user: bot_user, llm: llm)
+ result = tool.invoke
+ expect(result[:error]).to eq("Must provide either pull_id or sha")
+ end
+ end
+
+ context "with a pull request" do
+ let(:repo) { "discourse/discourse-automation" }
+ let(:pull_id) { 253 }
+ let(:tool) do
+ described_class.new({ repo: repo, pull_id: pull_id }, bot_user: bot_user, llm: llm)
+ end
+ let(:diff) { <<~DIFF }
+ diff --git a/lib/discourse_automation/automation.rb b/lib/discourse_automation/automation.rb
+ index 3e3e3e3..4f4f4f4 100644
+ --- a/lib/discourse_automation/automation.rb
+ +++ b/lib/discourse_automation/automation.rb
+ @@ -1,3 +1,3 @@
+ -module DiscourseAutomation
+ DIFF
+
+ let(:pr_info) do
+ {
+ "title" => "Test PR",
+ "state" => "open",
+ "user" => {
+ "login" => "test-user",
+ },
+ "created_at" => "2023-01-01T00:00:00Z",
+ "updated_at" => "2023-01-02T00:00:00Z",
+ "head" => {
+ "repo" => {
+ "full_name" => "test/repo",
+ },
+ "ref" => "feature-branch",
+ "sha" => "abc123",
+ },
+ "base" => {
+ "repo" => {
+ "full_name" => "main/repo",
+ },
+ "ref" => "main",
+ },
+ }
+ end
+
+ it "retrieves both PR info and diff" do
+ stub_request(:get, "https://api.github.com/repos/#{repo}/pulls/#{pull_id}").with(
+ headers: {
+ "Accept" => "application/json",
+ "User-Agent" => DiscourseAi::AiBot::USER_AGENT,
+ },
+ ).to_return(status: 200, body: pr_info.to_json)
+
+ stub_request(:get, "https://api.github.com/repos/#{repo}/pulls/#{pull_id}").with(
+ headers: {
+ "Accept" => "application/vnd.github.v3.diff",
+ "User-Agent" => DiscourseAi::AiBot::USER_AGENT,
+ },
+ ).to_return(status: 200, body: diff)
+
+ result = tool.invoke
+ expect(result[:type]).to eq("pull_request")
+ expect(result[:diff]).to eq(diff)
+ expect(result[:pr_info]).to include(title: "Test PR", state: "open", author: "test-user")
+ expect(result[:error]).to be_nil
+ end
+
+ it "uses the github access token if present" do
+ SiteSetting.ai_bot_github_access_token = "ABC"
+
+ stub_request(:get, "https://api.github.com/repos/#{repo}/pulls/#{pull_id}").with(
+ headers: {
+ "Accept" => "application/json",
+ "User-Agent" => DiscourseAi::AiBot::USER_AGENT,
+ "Authorization" => "Bearer ABC",
+ },
+ ).to_return(status: 200, body: pr_info.to_json)
+
+ stub_request(:get, "https://api.github.com/repos/#{repo}/pulls/#{pull_id}").with(
+ headers: {
+ "Accept" => "application/vnd.github.v3.diff",
+ "User-Agent" => DiscourseAi::AiBot::USER_AGENT,
+ "Authorization" => "Bearer ABC",
+ },
+ ).to_return(status: 200, body: diff)
+
+ result = tool.invoke
+ expect(result[:diff]).to eq(diff)
+ expect(result[:error]).to be_nil
+ end
+
+ it "returns an error for invalid pull request" do
+ stub_request(:get, "https://api.github.com/repos/#{repo}/pulls/#{pull_id}").with(
+ headers: {
+ "Accept" => "application/json",
+ "User-Agent" => DiscourseAi::AiBot::USER_AGENT,
+ },
+ ).to_return(status: 404)
+
+ result = tool.invoke
+ expect(result[:diff]).to be_nil
+ expect(result[:error]).to include("Failed to retrieve the PR information")
+ end
+ end
+
+ context "with a commit" do
+ let(:repo) { "owner/repo" }
+ let(:sha) { "abc123def456" }
+ let(:tool) { described_class.new({ repo: repo, sha: sha }, bot_user: bot_user, llm: llm) }
+ let(:diff) { <<~DIFF }
+ diff --git a/lib/feature.rb b/lib/feature.rb
+ index 1234567..89abcdef 100644
+ --- a/lib/feature.rb
+ +++ b/lib/feature.rb
+ @@ -10,3 +10,5 @@
+ +def new_method
+ +end
+ DIFF
+
+ let(:commit_info) do
+ {
+ "sha" => sha,
+ "commit" => {
+ "message" => "Fix bug in feature X\n\nThis fixes an issue with...",
+ "author" => {
+ "name" => "Test Author",
+ "date" => "2023-01-01T00:00:00Z",
+ },
+ },
+ "author" => {
+ "login" => "test-user",
+ },
+ "stats" => {
+ "additions" => 10,
+ "deletions" => 5,
+ "total" => 15,
+ },
+ "files" => [{ "filename" => "lib/feature.rb" }, { "filename" => "spec/feature_spec.rb" }],
+ }
+ end
+
+ it "retrieves commit info and diff" do
+ stub_request(:get, "https://api.github.com/repos/#{repo}/commits/#{sha}").with(
+ headers: {
+ "Accept" => "application/json",
+ "User-Agent" => DiscourseAi::AiBot::USER_AGENT,
+ },
+ ).to_return(status: 200, body: commit_info.to_json)
+
+ stub_request(:get, "https://api.github.com/repos/#{repo}/commits/#{sha}").with(
+ headers: {
+ "Accept" => "application/vnd.github.v3.diff",
+ "User-Agent" => DiscourseAi::AiBot::USER_AGENT,
+ },
+ ).to_return(status: 200, body: diff)
+
+ result = tool.invoke
+ expect(result[:type]).to eq("commit")
+ expect(result[:diff]).to eq(diff)
+ expect(result[:commit_info]).to include(
+ sha: sha,
+ message: "Fix bug in feature X\n\nThis fixes an issue with...",
+ author: "Test Author",
+ author_login: "test-user",
+ files_changed: 2,
+ )
+ expect(result[:commit_info][:stats]).to include(additions: 10, deletions: 5, total: 15)
+ expect(result[:error]).to be_nil
+ end
+
+ it "uses the github access token if present" do
+ SiteSetting.ai_bot_github_access_token = "ABC"
+
+ stub_request(:get, "https://api.github.com/repos/#{repo}/commits/#{sha}").with(
+ headers: {
+ "Accept" => "application/json",
+ "User-Agent" => DiscourseAi::AiBot::USER_AGENT,
+ "Authorization" => "Bearer ABC",
+ },
+ ).to_return(status: 200, body: commit_info.to_json)
+
+ stub_request(:get, "https://api.github.com/repos/#{repo}/commits/#{sha}").with(
+ headers: {
+ "Accept" => "application/vnd.github.v3.diff",
+ "User-Agent" => DiscourseAi::AiBot::USER_AGENT,
+ "Authorization" => "Bearer ABC",
+ },
+ ).to_return(status: 200, body: diff)
+
+ result = tool.invoke
+ expect(result[:diff]).to eq(diff)
+ expect(result[:error]).to be_nil
+ end
+
+ it "returns an error for invalid commit" do
+ stub_request(:get, "https://api.github.com/repos/#{repo}/commits/#{sha}").with(
+ headers: {
+ "Accept" => "application/json",
+ "User-Agent" => DiscourseAi::AiBot::USER_AGENT,
+ },
+ ).to_return(status: 404)
+
+ result = tool.invoke
+ expect(result[:diff]).to be_nil
+ expect(result[:error]).to include("Failed to retrieve the commit information")
+ end
+ end
+end
diff --git a/plugins/discourse-ai/spec/lib/personas/tools/github_pull_request_diff_spec.rb b/plugins/discourse-ai/spec/lib/personas/tools/github_pull_request_diff_spec.rb
deleted file mode 100644
index 253f59114e1..00000000000
--- a/plugins/discourse-ai/spec/lib/personas/tools/github_pull_request_diff_spec.rb
+++ /dev/null
@@ -1,148 +0,0 @@
-# frozen_string_literal: true
-
-RSpec.describe DiscourseAi::Personas::Tools::GithubPullRequestDiff do
- let(:bot_user) { Fabricate(:user) }
- fab!(:llm_model)
- let(:llm) { DiscourseAi::Completions::Llm.proxy(llm_model) }
- let(:tool) { described_class.new({ repo: repo, pull_id: pull_id }, bot_user: bot_user, llm: llm) }
-
- before { enable_current_plugin }
-
- context "with #sort_and_shorten_diff" do
- it "sorts and shortens the diff without dropping data" do
- diff = <<~DIFF
- diff --git a/src/lib.rs b/src/lib.rs
- index b466edd..66b068f 100644
- --- a/src/lib.rs
- +++ b/src/lib.rs
- this is a longer diff
- this is a longer diff
- this is a longer diff
- diff --git a/tests/test_encoding.py b/tests/test_encoding.py
- index 27b2192..9f31319 100644
- --- a/tests/test_encoding.py
- +++ b/tests/test_encoding.py
- short diff
- DIFF
-
- sorted_diff = described_class.sort_and_shorten_diff(diff)
-
- expect(sorted_diff).to eq(<<~DIFF)
- diff --git a/tests/test_encoding.py b/tests/test_encoding.py
- index 27b2192..9f31319 100644
- --- a/tests/test_encoding.py
- +++ b/tests/test_encoding.py
- short diff
-
- diff --git a/src/lib.rs b/src/lib.rs
- index b466edd..66b068f 100644
- --- a/src/lib.rs
- +++ b/src/lib.rs
- this is a longer diff
- this is a longer diff
- this is a longer diff
- DIFF
- end
- end
-
- context "with a valid pull request" do
- let(:repo) { "discourse/discourse-automation" }
- let(:pull_id) { 253 }
- let(:diff) { <<~DIFF }
- diff --git a/lib/discourse_automation/automation.rb b/lib/discourse_automation/automation.rb
- index 3e3e3e3..4f4f4f4 100644
- --- a/lib/discourse_automation/automation.rb
- +++ b/lib/discourse_automation/automation.rb
- @@ -1,3 +1,3 @@
- -module DiscourseAutomation
- DIFF
-
- let(:pr_info) do
- {
- "title" => "Test PR",
- "state" => "open",
- "user" => {
- "login" => "test-user",
- },
- "created_at" => "2023-01-01T00:00:00Z",
- "updated_at" => "2023-01-02T00:00:00Z",
- "head" => {
- "repo" => {
- "full_name" => "test/repo",
- },
- "ref" => "feature-branch",
- "sha" => "abc123",
- },
- "base" => {
- "repo" => {
- "full_name" => "main/repo",
- },
- "ref" => "main",
- },
- }
- end
-
- it "retrieves both PR info and diff" do
- stub_request(:get, "https://api.github.com/repos/#{repo}/pulls/#{pull_id}").with(
- headers: {
- "Accept" => "application/json",
- "User-Agent" => DiscourseAi::AiBot::USER_AGENT,
- },
- ).to_return(status: 200, body: pr_info.to_json)
-
- stub_request(:get, "https://api.github.com/repos/#{repo}/pulls/#{pull_id}").with(
- headers: {
- "Accept" => "application/vnd.github.v3.diff",
- "User-Agent" => DiscourseAi::AiBot::USER_AGENT,
- },
- ).to_return(status: 200, body: diff)
-
- result = tool.invoke
- expect(result[:diff]).to eq(diff)
- expect(result[:pr_info]).to include(title: "Test PR", state: "open", author: "test-user")
- expect(result[:error]).to be_nil
- end
-
- it "uses the github access token if present" do
- SiteSetting.ai_bot_github_access_token = "ABC"
-
- stub_request(:get, "https://api.github.com/repos/#{repo}/pulls/#{pull_id}").with(
- headers: {
- "Accept" => "application/json",
- "User-Agent" => DiscourseAi::AiBot::USER_AGENT,
- "Authorization" => "Bearer ABC",
- },
- ).to_return(status: 200, body: pr_info.to_json)
-
- stub_request(:get, "https://api.github.com/repos/#{repo}/pulls/#{pull_id}").with(
- headers: {
- "Accept" => "application/vnd.github.v3.diff",
- "User-Agent" => DiscourseAi::AiBot::USER_AGENT,
- "Authorization" => "Bearer ABC",
- },
- ).to_return(status: 200, body: diff)
-
- result = tool.invoke
- expect(result[:diff]).to eq(diff)
- expect(result[:error]).to be_nil
- end
- end
-
- context "with an invalid pull request" do
- let(:repo) { "invalid/repo" }
- let(:pull_id) { 999 }
-
- it "returns an error message" do
- stub_request(:get, "https://api.github.com/repos/#{repo}/pulls/#{pull_id}").with(
- headers: {
- "Accept" => "application/json",
- "User-Agent" => DiscourseAi::AiBot::USER_AGENT,
- },
- ).to_return(status: 404)
-
- result = tool.invoke
- expect(result[:diff]).to be_nil
- expect(result[:error]).to include("Failed to retrieve the PR information")
- end
- end
-end