Retrieves the content of a file from a GitHub repository using the GitHub API. Supports both public GitHub (github.com) and GitHub Enterprise (custom domains). Args: repo_url (str): The URL of the GitHub repository (e.g., "https://github.com/username
(repo_url: str, file_path: str, access_token: str = None)
| 458 | return db |
| 459 | |
| 460 | def get_github_file_content(repo_url: str, file_path: str, access_token: str = None) -> str: |
| 461 | """ |
| 462 | Retrieves the content of a file from a GitHub repository using the GitHub API. |
| 463 | Supports both public GitHub (github.com) and GitHub Enterprise (custom domains). |
| 464 | |
| 465 | Args: |
| 466 | repo_url (str): The URL of the GitHub repository |
| 467 | (e.g., "https://github.com/username/repo" or "https://github.company.com/username/repo") |
| 468 | file_path (str): The path to the file within the repository (e.g., "src/main.py") |
| 469 | access_token (str, optional): GitHub personal access token for private repositories |
| 470 | |
| 471 | Returns: |
| 472 | str: The content of the file as a string |
| 473 | |
| 474 | Raises: |
| 475 | ValueError: If the file cannot be fetched or if the URL is not a valid GitHub URL |
| 476 | """ |
| 477 | try: |
| 478 | # Parse the repository URL to support both github.com and enterprise GitHub |
| 479 | parsed_url = urlparse(repo_url) |
| 480 | if not parsed_url.scheme or not parsed_url.netloc: |
| 481 | raise ValueError("Not a valid GitHub repository URL") |
| 482 | |
| 483 | # Check if it's a GitHub-like URL structure |
| 484 | path_parts = parsed_url.path.strip('/').split('/') |
| 485 | if len(path_parts) < 2: |
| 486 | raise ValueError("Invalid GitHub URL format - expected format: https://domain/owner/repo") |
| 487 | |
| 488 | owner = path_parts[-2] |
| 489 | repo = path_parts[-1].replace(".git", "") |
| 490 | |
| 491 | # Determine the API base URL |
| 492 | if parsed_url.netloc == "github.com": |
| 493 | # Public GitHub |
| 494 | api_base = "https://api.github.com" |
| 495 | else: |
| 496 | # GitHub Enterprise - API is typically at https://domain/api/v3/ |
| 497 | api_base = f"{parsed_url.scheme}://{parsed_url.netloc}/api/v3" |
| 498 | |
| 499 | # Use GitHub API to get file content |
| 500 | # The API endpoint for getting file content is: /repos/{owner}/{repo}/contents/{path} |
| 501 | api_url = f"{api_base}/repos/{owner}/{repo}/contents/{file_path}" |
| 502 | |
| 503 | # Fetch file content from GitHub API |
| 504 | headers = {} |
| 505 | if access_token: |
| 506 | headers["Authorization"] = f"token {access_token}" |
| 507 | logger.info(f"Fetching file content from GitHub API: {api_url}") |
| 508 | try: |
| 509 | response = requests.get(api_url, headers=headers) |
| 510 | response.raise_for_status() |
| 511 | except RequestException as e: |
| 512 | raise ValueError(f"Error fetching file content: {e}") |
| 513 | try: |
| 514 | content_data = response.json() |
| 515 | except json.JSONDecodeError: |
| 516 | raise ValueError("Invalid response from GitHub API") |
| 517 |