Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

feat(blocks): output full PR object from GithubReadPullRequestBlock (#14295)

Reinier van der Leer775039b2971c
diff --git a/autogpt_platform/backend/backend/blocks/github/pull_requests.py b/autogpt_platform/backend/backend/blocks/github/pull_requests.py
index 6b83180a3e..e8d040642a 100644
--- a/autogpt_platform/backend/backend/blocks/github/pull_requests.py
+++ b/autogpt_platform/backend/backend/blocks/github/pull_requests.py
@@ -259,6 +259,24 @@ class GithubMakePullRequestBlock(Block):
yield "error", str(e)
+TEST_PR_PAYLOAD = {
+ "title": "Title of the pull request",
+ "body": "This is the body of the pull request.",
+ "user": {"login": "username"},
+ "draft": False,
+ "mergeable": True,
+ "mergeable_state": "clean",
+ "head": {
+ "ref": "feature-branch",
+ "repo": {"full_name": "someone/repo"},
+ },
+ "base": {
+ "ref": "main",
+ "repo": {"full_name": "owner/repo"},
+ },
+}
+
+
class GithubReadPullRequestBlock(Block):
class Input(BlockSchemaInput):
credentials: GithubCredentialsInput = GithubCredentialsField("repo")
@@ -277,6 +295,20 @@ class GithubReadPullRequestBlock(Block):
body: str = SchemaField(description="Body of the pull request")
author: str = SchemaField(description="User who created the pull request")
changes: str = SchemaField(description="Changes made in the pull request")
+ pull_request: dict = SchemaField(
+ description="The full pull request object from the API, including "
+ "head/base refs (with fork repo and branch name for cross-repo PRs), "
+ "mergeability (mergeable, mergeable_state, rebaseable), draft state, "
+ "review/comment counts, labels, milestone, and diff/commit stats."
+ )
+ head_branch: str = SchemaField(
+ description="Name of the branch the PR is created from (the head "
+ "branch). For cross-repo PRs, this branch lives in the fork — see "
+ "pull_request.head.repo for the fork's URL."
+ )
+ target_branch: str = SchemaField(
+ description="Name of the branch the PR targets (the base branch)"
+ )
error: str = SchemaField(
description="Error message if reading the pull request failed"
)
@@ -284,7 +316,8 @@ class GithubReadPullRequestBlock(Block):
def __init__(self):
super().__init__(
id="bf94b2a4-1a30-4600-a783-a8a44ee31301",
- description="This block reads the body, title, user, and changes of a specified GitHub pull request.",
+ description="This block reads the body, title, user, changes, and full "
+ "raw object of a specified GitHub pull request.",
categories={BlockCategory.DEVELOPER_TOOLS},
input_schema=GithubReadPullRequestBlock.Input,
output_schema=GithubReadPullRequestBlock.Output,
@@ -298,29 +331,57 @@ class GithubReadPullRequestBlock(Block):
("title", "Title of the pull request"),
("body", "This is the body of the pull request."),
("author", "username"),
+ ("pull_request", TEST_PR_PAYLOAD),
+ ("head_branch", "feature-branch"),
+ ("target_branch", "main"),
("changes", "List of changes made in the pull request."),
],
test_mock={
- "read_pr": lambda *args, **kwargs: (
- "Title of the pull request",
- "This is the body of the pull request.",
- "username",
- ),
+ "read_pr": lambda *args, **kwargs: TEST_PR_PAYLOAD,
"read_pr_changes": lambda *args, **kwargs: "List of changes made in the pull request.",
},
)
+ async def run(
+ self,
+ input_data: Input,
+ *,
+ credentials: GithubCredentials,
+ **kwargs,
+ ) -> BlockOutput:
+ pull_request = await self.read_pr(
+ credentials,
+ input_data.pr_url,
+ )
+ title, body, author = self._pr_summary(pull_request)
+ yield "title", title
+ yield "body", body
+ yield "author", author
+ yield "pull_request", pull_request
+ yield "head_branch", pull_request.get("head", {}).get("ref", "")
+ yield "target_branch", pull_request.get("base", {}).get("ref", "")
+
+ if input_data.include_pr_changes:
+ changes = await self.read_pr_changes(
+ credentials,
+ input_data.pr_url,
+ )
+ yield "changes", changes
+
@staticmethod
- async def read_pr(
- credentials: GithubCredentials, pr_url: str
- ) -> tuple[str, str, str]:
+ async def read_pr(credentials: GithubCredentials, pr_url: str) -> dict:
api = get_api(credentials)
- issue_url = pr_url.replace("/pull/", "/issues/")
- response = await api.get(issue_url)
- data = response.json()
- title = data.get("title", "No title found")
- body = data.get("body", "No body content found")
- author = data.get("user", {}).get("login", "Unknown author")
+ pr_api_url = prepare_pr_api_url(pr_url=pr_url, path="").rstrip("/")
+ response = await api.get(pr_api_url)
+ return response.json()
+
+ @staticmethod
+ def _pr_summary(pull_request: dict) -> tuple[str, str, str]:
+ # GitHub returns an explicit `"body": null` for PRs with no description,
+ # so `.get(..., default)` alone won't catch it — `or` does.
+ title = pull_request.get("title") or "No title found"
+ body = pull_request.get("body") or "No body content found"
+ author = (pull_request.get("user") or {}).get("login") or "Unknown author"
return title, body, author
@staticmethod
@@ -352,28 +413,6 @@ class GithubReadPullRequestBlock(Block):
changes.append(patch_header + diff)
return "\n\n".join(changes)
- async def run(
- self,
- input_data: Input,
- *,
- credentials: GithubCredentials,
- **kwargs,
- ) -> BlockOutput:
- title, body, author = await self.read_pr(
- credentials,
- input_data.pr_url,
- )
- yield "title", title
- yield "body", body
- yield "author", author
-
- if input_data.include_pr_changes:
- changes = await self.read_pr_changes(
- credentials,
- input_data.pr_url,
- )
- yield "changes", changes
-
class GithubAssignPRReviewerBlock(Block):
class Input(BlockSchemaInput):
diff --git a/autogpt_platform/backend/backend/blocks/github/test_github_blocks.py b/autogpt_platform/backend/backend/blocks/github/test_github_blocks.py
index 9ab7ab2eac..2bb26a3435 100644
--- a/autogpt_platform/backend/backend/blocks/github/test_github_blocks.py
+++ b/autogpt_platform/backend/backend/blocks/github/test_github_blocks.py
@@ -41,9 +41,11 @@ from backend.blocks.github.notifications import (
_to_notification_item,
)
from backend.blocks.github.pull_requests import (
+ TEST_PR_PAYLOAD,
GithubListPRReviewersBlock,
GithubListPullRequestsBlock,
GithubMergePullRequestBlock,
+ GithubReadPullRequestBlock,
prepare_pr_api_url,
)
from backend.blocks.github.repo import (
@@ -542,6 +544,46 @@ class TestListReviewers:
assert len(_list_reviewers([_review(None, "APPROVED")])) == 1
+# ── read_pr tests ──
+
+
+PR_ENDPOINT_URL = "https://github.com/owner/repo/pulls/1"
+
+
+class TestReadPr:
+ def test_hits_the_pulls_endpoint_not_the_issues_endpoint(self):
+ api = _FakeApi({PR_ENDPOINT_URL: {}})
+ with mock.patch.object(pull_requests, "get_api", lambda *a, **kw: api):
+ asyncio.run(GithubReadPullRequestBlock.read_pr(TEST_CREDENTIALS, PR_URL))
+ assert api.calls[0][1] == PR_ENDPOINT_URL
+
+ def test_returns_the_raw_response_body(self):
+ api = _FakeApi({PR_ENDPOINT_URL: TEST_PR_PAYLOAD})
+ with mock.patch.object(pull_requests, "get_api", lambda *a, **kw: api):
+ result = asyncio.run(
+ GithubReadPullRequestBlock.read_pr(TEST_CREDENTIALS, PR_URL)
+ )
+ assert result == TEST_PR_PAYLOAD
+
+
+class TestPrSummary:
+ def test_null_title_falls_back_instead_of_yielding_none(self):
+ pr = {**TEST_PR_PAYLOAD, "title": None}
+ assert GithubReadPullRequestBlock._pr_summary(pr)[0] == "No title found"
+
+ def test_null_body_falls_back_instead_of_yielding_none(self):
+ # GitHub returns an explicit "body": null for PRs with no description.
+ pr = {**TEST_PR_PAYLOAD, "body": None}
+ assert GithubReadPullRequestBlock._pr_summary(pr)[1] == (
+ "No body content found"
+ )
+
+ def test_null_user_falls_back_instead_of_raising(self):
+ # Reviews by deleted accounts come back with a null user.
+ pr = {**TEST_PR_PAYLOAD, "user": None}
+ assert GithubReadPullRequestBlock._pr_summary(pr)[2] == "Unknown author"
+
+
# ── get_paginated tests ──
# Every list block delegates its fetching here, and every block test mocks the
# list method wholesale, so the pagination logic is exercised directly.
diff --git a/docs/integrations/README.md b/docs/integrations/README.md
index e97262cfd2..1d1dde57b1 100644
--- a/docs/integrations/README.md
+++ b/docs/integrations/README.md
@@ -549,7 +549,7 @@ Below is a comprehensive list of all available blocks, categorized by their prim
| [Github Read File](block-integrations/github/repo_files.md#github-read-file) | This block reads the content of a specified file from a GitHub repository |
| [Github Read Folder](block-integrations/github/repo_files.md#github-read-folder) | This block reads the content of a specified folder from a GitHub repository |
| [Github Read Issue](block-integrations/github/issues.md#github-read-issue) | A block that retrieves information about a specific GitHub issue, including its title, body content, and creator |
-| [Github Read Pull Request](block-integrations/github/pull_requests.md#github-read-pull-request) | This block reads the body, title, user, and changes of a specified GitHub pull request |
+| [Github Read Pull Request](block-integrations/github/pull_requests.md#github-read-pull-request) | This block reads the body, title, user, changes, and full raw object of a specified GitHub pull request |
| [Github Release Trigger](block-integrations/github/triggers.md#github-release-trigger) | This block triggers on GitHub release events |
| [Github Remove Label](block-integrations/github/issues.md#github-remove-label) | A block that removes a label from a GitHub issue or pull request |
| [Github Resolve Review Discussion](block-integrations/github/reviews.md#github-resolve-review-discussion) | This block resolves or unresolves a review discussion thread on a GitHub pull request |
diff --git a/docs/integrations/block-integrations/github/pull_requests.md b/docs/integrations/block-integrations/github/pull_requests.md
index 218671141b..127040f596 100644
--- a/docs/integrations/block-integrations/github/pull_requests.md
+++ b/docs/integrations/block-integrations/github/pull_requests.md
@@ -206,13 +206,13 @@ You can optionally provide a custom commit title and message for merge and squas
## Github Read Pull Request
### What it is
-This block reads the body, title, user, and changes of a specified GitHub pull request.
+This block reads the body, title, user, changes, and full raw object of a specified GitHub pull request.
### How it works
<!-- MANUAL: how_it_works -->
-This block reads the details of a GitHub pull request including its title, description, author, and optionally the code diff. It fetches this information via the GitHub API using your credentials.
+This block fetches the pull request object from the `pulls/{number}` GitHub API endpoint and emits it unmodified as `pull_request`, while also pulling out `title`, `body`, `author`, `head_branch`, and `target_branch` as discrete outputs for convenience — if any of those fields are missing from the response, they fall back to placeholder strings (`"No title found"`, etc., or an empty string for the branch names) rather than failing the block. Because this uses the pulls endpoint rather than the issues endpoint, a fine-grained personal access token needs `Pull requests: read`; the default OAuth `repo` scope already covers this.
-When include_pr_changes is enabled, the block also retrieves the full diff of all changes in the PR, which can be useful for code review automation or analysis.
+When `include_pr_changes` is enabled, the block makes a second call to fetch the diff of every changed file and joins them into one `changes` string, prefixed with `--- <old path>` / `+++ <new path>` headers similar to a unified diff — useful for feeding a PR's full changeset to code review automation.
<!-- END MANUAL -->
### Inputs
@@ -231,6 +231,9 @@ When include_pr_changes is enabled, the block also retrieves the full diff of al
| body | Body of the pull request | str |
| author | User who created the pull request | str |
| changes | Changes made in the pull request | str |
+| pull_request | The full pull request object from the API, including head/base refs (with fork repo and branch name for cross-repo PRs), mergeability (mergeable, mergeable_state, rebaseable), draft state, review/comment counts, labels, milestone, and diff/commit stats. | Dict[str, Any] |
+| head_branch | Name of the branch the PR is created from (the head branch). For cross-repo PRs, this branch lives in the fork — see pull_request.head.repo for the fork's URL. | str |
+| target_branch | Name of the branch the PR targets (the base branch) | str |
### Possible use case
<!-- MANUAL: use_case -->