fix(backend): Improve GitHub PR URL validation and API URL generation (#10317)

### Changes 🏗️

Fixes
[AUTOGPT-SERVER-4EN](https://sentry.io/organizations/significant-gravitas/issues/6731949478/).
The issue was that: Issue URL passed to PR file reader, regex failed,
leading to issue API call, returning object iterated as keys, causing
AttributeError.

- Refactor `prepare_pr_api_url` to improve validation of GitHub PR URLs.
- Update regex to specifically target github.com URLs.
- Raise ValueError with a descriptive message for invalid URLs.
- Correctly construct the API URL using the extracted repository path.

This fix was generated by Seer in Sentry, triggered automatically. 👁️
Run ID: 265077

Not quite right? [Click here to continue debugging with
Seer.](https://sentry.io/organizations/significant-gravitas/issues/6731949478/?seerDrawer=true)

### Checklist 📋

#### For code changes:
- [x] I have clearly listed my changes in the PR description
- [x] I have made a test plan
- [x] I have tested my changes according to the test plan:
  <!-- Put your test plan here: -->
  - [x] Test plan:
- [x] Provide an invalid GitHub PR URL and verify that a ValueError is
raised with a descriptive message.
- [x] Provide a valid GitHub PR URL and verify that the API URL is
correctly constructed.

---------

Co-authored-by: seer-by-sentry[bot] <157164994+seer-by-sentry[bot]@users.noreply.github.com>
Co-authored-by: Nicholas Tindle <nicholas.tindle@agpt.co>
Co-authored-by: Bently <Github@bentlybro.com>
This commit is contained in:
seer-by-sentry[bot]
2025-09-05 06:35:45 +00:00
committed by GitHub
parent dcf26bd3d4
commit 9cd186a2f3

View File

@@ -553,11 +553,12 @@ class GithubListPRReviewersBlock(Block):
def prepare_pr_api_url(pr_url: str, path: str) -> str:
# Pattern to capture the base repository URL and the pull request number
pattern = r"^(?:https?://)?([^/]+/[^/]+/[^/]+)/pull/(\d+)"
pattern = r"^(?:https?://)?github\.com/([^/]+/[^/]+)/pull/(\d+)"
match = re.match(pattern, pr_url)
if not match:
return pr_url
raise ValueError(
f"Invalid GitHub PR URL: {pr_url}. URL must be a valid pull request URL, e.g., https://github.com/owner/repo/pull/123"
)
base_url, pr_number = match.groups()
return f"{base_url}/pulls/{pr_number}/{path}"
repo_path, pr_number = match.groups()
return f"{repo_path}/pulls/{pr_number}/{path}"