Created
December 9, 2024 13:56
-
-
Save Gershon-A/4910e39dcdb50053e57a653e70623fd5 to your computer and use it in GitHub Desktop.
Python script automates the process of listing and cloning all repositories from 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 requests | |
| import git | |
| import os | |
| import time | |
| # Replace with your GitHub organization and personal access token | |
| GITHUB_ORG = '' | |
| GITHUB_TOKEN = '' | |
| # GitHub API URL to list repositories in an organization | |
| url = f'https://api.github.com/orgs/{GITHUB_ORG}/repos' | |
| # Headers for authentication | |
| headers = { | |
| 'Authorization': f'token {GITHUB_TOKEN}' | |
| } | |
| # Get the list of repositories | |
| response = requests.get(url, headers=headers) | |
| repos = response.json() | |
| # Check for errors | |
| if response.status_code != 200: | |
| print(f"Error: Unable to fetch repositories (status code: {response.status_code})") | |
| print(repos) | |
| else: | |
| # Create a directory to clone repositories into | |
| CLONE_DIR = 'cloned_repos' | |
| os.makedirs(CLONE_DIR, exist_ok=True) | |
| # List and clone all repositories | |
| for repo in repos: | |
| repo_name = repo['name'] | |
| clone_url = repo['clone_url'] | |
| repo_path = os.path.join(CLONE_DIR, repo_name) | |
| if os.path.exists(repo_path): | |
| print(f'Skipping {repo_name}, already cloned.') | |
| continue | |
| print(f'Cloning {repo_name}...') | |
| retry_count = 3 | |
| for attempt in range(retry_count): | |
| try: | |
| git.Repo.clone_from(clone_url, repo_path) | |
| print(f'Successfully cloned {repo_name}.') | |
| break | |
| except Exception as e: | |
| print(f'Error cloning {repo_name}: {e}') | |
| if attempt < retry_count - 1: | |
| print(f'Retrying... ({attempt + 1}/{retry_count})') | |
| time.sleep(5) # Wait for 5 seconds before retrying | |
| else: | |
| print(f'Failed to clone {repo_name} after {retry_count} attempts.') | |
| print('All repositories processed.') |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Description
This Python script automates the process of listing and cloning all repositories from a specified GitHub organization. It uses the GitHub API to fetch the list of repositories and then clones each repository into a local directory.
Key Features
cloned_reposto store the cloned repositories.cloned_reposdirectory usinggitpython.Usage
GITHUB_ORGwith the name of your GitHub organization.GITHUB_TOKENwith your GitHub personal access token.