Skip to content

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

FIX: null dereference in GithubDiff for deleted forks/users (#37548)

Sam775cb5f236f4
diff --git a/plugins/discourse-ai/lib/personas/tools/github_diff.rb b/plugins/discourse-ai/lib/personas/tools/github_diff.rb
index 818b5a7ee97..989684647de 100644
--- a/plugins/discourse-ai/lib/personas/tools/github_diff.rb
+++ b/plugins/discourse-ai/lib/personas/tools/github_diff.rb
@@ -113,6 +113,8 @@ module DiscourseAi
source_repo = info.dig("head", "repo", "full_name")
source_branch = info.dig("head", "ref")
source_sha = info.dig("head", "sha")
+ target_repo = info.dig("base", "repo", "full_name")
+ target_branch = info.dig("base", "ref")
{
type: "pull_request",
@@ -127,10 +129,10 @@ module DiscourseAi
url: "https://github.com/#{source_repo}/tree/#{source_branch}",
},
target: {
- repo: info["base"]["repo"]["full_name"],
- branch: info["base"]["ref"],
+ repo: target_repo,
+ branch: target_branch,
},
- author: info["user"]["login"],
+ author: info.dig("user", "login"),
created_at: info["created_at"],
updated_at: info["updated_at"],
},
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
index eb1754a1302..61d7849e393 100644
--- a/plugins/discourse-ai/spec/lib/personas/tools/github_diff_spec.rb
+++ b/plugins/discourse-ai/spec/lib/personas/tools/github_diff_spec.rb
@@ -170,6 +170,47 @@ RSpec.describe DiscourseAi::Personas::Tools::GithubDiff do
expect(result[:diff]).to be_nil
expect(result[:error]).to include("Failed to retrieve the PR information")
end
+
+ it "handles PRs from deleted forks gracefully" do
+ pr_info_deleted_fork = {
+ "title" => "Test PR from deleted fork",
+ "state" => "closed",
+ "user" => nil,
+ "created_at" => "2023-01-01T00:00:00Z",
+ "updated_at" => "2023-01-02T00:00:00Z",
+ "head" => {
+ "repo" => nil,
+ "ref" => "feature-branch",
+ "sha" => "abc123",
+ },
+ "base" => {
+ "repo" => nil,
+ "ref" => "main",
+ },
+ }
+
+ 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_deleted_fork.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][:title]).to eq("Test PR from deleted fork")
+ expect(result[:pr_info][:author]).to be_nil
+ expect(result[:pr_info][:target][:repo]).to be_nil
+ expect(result[:error]).to be_nil
+ end
end
context "with a commit" do