Created
May 10, 2024 01:22
-
-
Save adam404/e36a3e4c827e97e9460fc173c1a6e1ff to your computer and use it in GitHub Desktop.
Facilitates the bulk downloading of repositories associated with a specified GitHub organization.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import requests | |
| def download_org_repo_exports(org_name, access_token): | |
| # GitHub API endpoint for organization repositories | |
| api_url = f"https://api.github.com/orgs/{org_name}/repos" | |
| # Set the authentication headers | |
| headers = { | |
| "Authorization": f"Bearer {access_token}", | |
| "Accept": "application/vnd.github.v3+json" | |
| } | |
| # Create a directory to store the repository exports | |
| output_dir = f"{org_name}_repo_exports" | |
| os.makedirs(output_dir, exist_ok=True) | |
| # Initialize counters | |
| total_repos = 0 | |
| downloaded_repos = 0 | |
| # Retrieve repositories page by page | |
| page = 1 | |
| while True: | |
| # Send a GET request to the GitHub API with authentication and pagination | |
| params = {"page": page, "per_page": 100} | |
| response = requests.get(api_url, headers=headers, params=params) | |
| # Check if the request was successful | |
| if response.status_code == 200: | |
| # Parse the JSON response | |
| repos = response.json() | |
| # Break the loop if no more repositories are returned | |
| if not repos: | |
| break | |
| # Iterate over each repository | |
| for repo in repos: | |
| repo_name = repo["name"] | |
| repo_url = repo["url"] | |
| print(f"Processing repository: {repo_name}") | |
| # Download the repository export as a ZIP file | |
| export_url = f"{repo_url}/zipball" | |
| export_response = requests.get(export_url, headers=headers) | |
| if export_response.status_code == 200: | |
| # Save the ZIP file | |
| export_path = os.path.join(output_dir, f"{repo_name}.zip") | |
| with open(export_path, "wb") as file: | |
| file.write(export_response.content) | |
| print(f"Repository export downloaded: {repo_name}") | |
| downloaded_repos += 1 | |
| else: | |
| print(f"Failed to download repository export: {repo_name}") | |
| total_repos += 1 | |
| # Move to the next page | |
| page += 1 | |
| else: | |
| print(f"Failed to retrieve repositories for organization: {org_name}") | |
| break | |
| print(f"Total repositories: {total_repos}") | |
| print(f"Downloaded repository exports: {downloaded_repos}") | |
| # Example usage | |
| org_name = "ORG-NAME" # Replace with your GitHub organization name | |
| access_token = "YOUR_ACCESS_TOKEN" # Replace with your personal access token | |
| download_org_repo_exports(org_name, access_token) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment