Skip to content

Instantly share code, notes, and snippets.

@Gershon-A
Created December 9, 2024 13:56
Show Gist options
  • Select an option

  • Save Gershon-A/4910e39dcdb50053e57a653e70623fd5 to your computer and use it in GitHub Desktop.

Select an option

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
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.')
@Gershon-A
Copy link
Author

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

  1. Authentication: Uses a GitHub personal access token for authenticated API requests.
  2. Repository Listing: Fetches and lists all repositories in the specified GitHub organization.
  3. Directory Creation: Creates a local directory named cloned_repos to store the cloned repositories.
  4. Cloning: Clones each repository from the organization into the cloned_repos directory using gitpython.

Usage

  1. Replace GITHUB_ORG with the name of your GitHub organization.
  2. Replace GITHUB_TOKEN with your GitHub personal access token.
  3. Run the script to list and clone all repositories from the specified organization.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment