Create a GitHub PR and return the PR URL.
(
branch_name: str,
title: str,
body: str,
base: str = "main",
)
| 410 | # --------------------------------------------------------------------------- |
| 411 | |
| 412 | def create_github_pr( |
| 413 | branch_name: str, |
| 414 | title: str, |
| 415 | body: str, |
| 416 | base: str = "main", |
| 417 | ) -> Optional[str]: |
| 418 | """Create a GitHub PR and return the PR URL.""" |
| 419 | token = os.environ.get("GITHUB_TOKEN") |
| 420 | repo = os.environ.get("GITHUB_REPO", "RASAAS/docmcp-knowledge") |
| 421 | |
| 422 | if not token: |
| 423 | print("WARNING: GITHUB_TOKEN not set. Skipping PR creation.") |
| 424 | return None |
| 425 | |
| 426 | url = f"https://api.github.com/repos/{repo}/pulls" |
| 427 | headers = { |
| 428 | "Authorization": f"Bearer {token}", |
| 429 | "Accept": "application/vnd.github+json", |
| 430 | "X-GitHub-Api-Version": "2022-11-28", |
| 431 | } |
| 432 | payload = { |
| 433 | "title": title, |
| 434 | "body": body, |
| 435 | "head": branch_name, |
| 436 | "base": base, |
| 437 | "draft": True, |
| 438 | } |
| 439 | |
| 440 | try: |
| 441 | resp = requests.post(url, headers=headers, json=payload, timeout=30) |
| 442 | if resp.status_code == 201: |
| 443 | pr_url = resp.json().get("html_url", "") |
| 444 | print(f"PR created: {pr_url}") |
| 445 | return pr_url |
| 446 | else: |
| 447 | print(f"ERROR: GitHub API returned {resp.status_code}: {resp.text[:200]}") |
| 448 | return None |
| 449 | except Exception as e: |
| 450 | print(f"ERROR: Failed to create PR: {e}") |
| 451 | return None |
| 452 | |
| 453 | |
| 454 | # --------------------------------------------------------------------------- |