Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 2a380c8

Browse files
committed
Improve PR info
Side effect change cause I was reviewing the PR with bot
1 parent c69a51b commit 2a380c8

File tree

2 files changed

+95
-16
lines changed

2 files changed

+95
-16
lines changed

lib/ai_bot/tools/github_pull_request_diff.rb

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,27 +47,65 @@ def invoke
4747
api_url = "https://api.github.com/repos/#{repo}/pulls/#{pull_id}"
4848
@url = "https://github.com/#{repo}/pull/#{pull_id}"
4949

50-
body = nil
50+
pr_info = nil
51+
diff_body = nil
5152
response_code = "unknown error"
5253

5354
send_http_request(
5455
api_url,
5556
headers: {
56-
"Accept" => "application/vnd.github.v3.diff",
57+
"Accept" => "application/json",
5758
},
5859
authenticate_github: true,
5960
) do |response|
6061
response_code = response.code
61-
body = read_response_body(response)
62+
pr_info = JSON.parse(read_response_body(response)) if response_code == "200"
6263
end
6364

6465
if response_code == "200"
65-
diff = body
66+
send_http_request(
67+
api_url,
68+
headers: {
69+
"Accept" => "application/vnd.github.v3.diff",
70+
},
71+
authenticate_github: true,
72+
) do |response|
73+
response_code = response.code
74+
diff_body = read_response_body(response)
75+
end
76+
end
77+
78+
if response_code == "200" && pr_info && diff_body
79+
diff = diff_body
6680
diff = self.class.sort_and_shorten_diff(diff)
6781
diff = truncate(diff, max_length: 20_000, percent_length: 0.3, llm: llm)
68-
{ diff: diff }
82+
83+
source_repo = pr_info.dig("head", "repo", "full_name")
84+
source_branch = pr_info.dig("head", "ref")
85+
source_sha = pr_info.dig("head", "sha")
86+
87+
{
88+
diff: diff,
89+
pr_info: {
90+
title: pr_info["title"],
91+
state: pr_info["state"],
92+
source: {
93+
repo: source_repo,
94+
branch: source_branch,
95+
sha: source_sha,
96+
url: "https://github.com/#{source_repo}/tree/#{source_branch}",
97+
},
98+
target: {
99+
repo: pr_info["base"]["repo"]["full_name"],
100+
branch: pr_info["base"]["ref"],
101+
},
102+
author: pr_info["user"]["login"],
103+
created_at: pr_info["created_at"],
104+
updated_at: pr_info["updated_at"],
105+
},
106+
}
69107
else
70-
{ error: "Failed to retrieve the diff. Status code: #{response_code}" }
108+
{ error: "Failed to retrieve the PR information. Status code: #{response_code}" }
71109
end
72110
end
73111

spec/lib/modules/ai_bot/tools/github_pull_request_diff_spec.rb

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,47 @@
4949
let(:repo) { "discourse/discourse-automation" }
5050
let(:pull_id) { 253 }
5151
let(:diff) { <<~DIFF }
52-
diff --git a/lib/discourse_automation/automation.rb b/lib/discourse_automation/automation.rb
53-
index 3e3e3e3..4f4f4f4 100644
54-
--- a/lib/discourse_automation/automation.rb
55-
+++ b/lib/discourse_automation/automation.rb
56-
@@ -1,3 +1,3 @@
57-
-module DiscourseAutomation
58-
DIFF
52+
diff --git a/lib/discourse_automation/automation.rb b/lib/discourse_automation/automation.rb
53+
index 3e3e3e3..4f4f4f4 100644
54+
--- a/lib/discourse_automation/automation.rb
55+
+++ b/lib/discourse_automation/automation.rb
56+
@@ -1,3 +1,3 @@
57+
-module DiscourseAutomation
58+
DIFF
59+
60+
let(:pr_info) do
61+
{
62+
"title" => "Test PR",
63+
"state" => "open",
64+
"user" => {
65+
"login" => "test-user",
66+
},
67+
"created_at" => "2023-01-01T00:00:00Z",
68+
"updated_at" => "2023-01-02T00:00:00Z",
69+
"head" => {
70+
"repo" => {
71+
"full_name" => "test/repo",
72+
},
73+
"ref" => "feature-branch",
74+
"sha" => "abc123",
75+
},
76+
"base" => {
77+
"repo" => {
78+
"full_name" => "main/repo",
79+
},
80+
"ref" => "main",
81+
},
82+
}
83+
end
84+
85+
it "retrieves both PR info and diff" do
86+
stub_request(:get, "https://api.github.com/repos/#{repo}/pulls/#{pull_id}").with(
87+
headers: {
88+
"Accept" => "application/json",
89+
"User-Agent" => DiscourseAi::AiBot::USER_AGENT,
90+
},
91+
).to_return(status: 200, body: pr_info.to_json)
5992

60-
it "retrieves the diff for the pull request" do
6193
stub_request(:get, "https://api.github.com/repos/#{repo}/pulls/#{pull_id}").with(
6294
headers: {
6395
"Accept" => "application/vnd.github.v3.diff",
@@ -67,12 +99,21 @@
6799

68100
result = tool.invoke
69101
expect(result[:diff]).to eq(diff)
102+
expect(result[:pr_info]).to include(title: "Test PR", state: "open", author: "test-user")
70103
expect(result[:error]).to be_nil
71104
end
72105

73106
it "uses the github access token if present" do
74107
SiteSetting.ai_bot_github_access_token = "ABC"
75108

109+
stub_request(:get, "https://api.github.com/repos/#{repo}/pulls/#{pull_id}").with(
110+
headers: {
111+
"Accept" => "application/json",
112+
"User-Agent" => DiscourseAi::AiBot::USER_AGENT,
113+
"Authorization" => "Bearer ABC",
114+
},
115+
).to_return(status: 200, body: pr_info.to_json)
116+
76117
stub_request(:get, "https://api.github.com/repos/#{repo}/pulls/#{pull_id}").with(
77118
headers: {
78119
"Accept" => "application/vnd.github.v3.diff",
@@ -94,14 +135,14 @@
94135
it "returns an error message" do
95136
stub_request(:get, "https://api.github.com/repos/#{repo}/pulls/#{pull_id}").with(
96137
headers: {
97-
"Accept" => "application/vnd.github.v3.diff",
138+
"Accept" => "application/json",
98139
"User-Agent" => DiscourseAi::AiBot::USER_AGENT,
99140
},
100141
).to_return(status: 404)
101142

102143
result = tool.invoke
103144
expect(result[:diff]).to be_nil
104-
expect(result[:error]).to include("Failed to retrieve the diff")
145+
expect(result[:error]).to include("Failed to retrieve the PR information")
105146
end
106147
end
107148
end

0 commit comments

Comments
 (0)