Skip to content

Instantly share code, notes, and snippets.

@yash22arora
Last active May 8, 2023 06:47
Show Gist options
  • Save yash22arora/85ec8e9f90f84a29b3a176f83efa183a to your computer and use it in GitHub Desktop.
Save yash22arora/85ec8e9f90f84a29b3a176f83efa183a to your computer and use it in GitHub Desktop.
# Script to create Github Repos for all the directories in the current folder and linking local repos to remote repos
import os
import shutil
from dotenv import load_dotenv
import requests
load_dotenv()
GITHUB_ACCESS_TOKEN = os.getenv('GITHUB_ACCESS_TOKEN')
GITHUB_ORG = os.getenv('GITHUB_ORG')
def delete_git(dirs):
for d in dirs:
path = os.path.join(os.getcwd(), d)
files = os.listdir(path)
if '.git' in files:
shutil.rmtree(os.path.join(path, '.git'))
def create_remote_repo(dir):
url = 'https://api.github.com/orgs/'+GITHUB_ORG+'/repos'
json = {"name": dir, "private": True}
resp = requests.post(url, headers={'Authorization': 'Bearer ' +
GITHUB_ACCESS_TOKEN}, json=json)
remote_repo = resp.json().get('clone_url')
print(remote_repo)
push_to_remote_repo(dir, remote_repo)
def push_to_remote_repo(dir, remote_url):
parent_dir = os.getcwd()
path = os.path.join(parent_dir, dir)
os.chdir(path)
os.system('git init')
os.system('git add .')
os.system('git commit -m "Initial commit"')
os.system('git branch -M main')
os.system('git remote add origin ' + remote_url)
os.system('git push -u origin main')
os.chdir(parent_dir)
def main():
path = os.getcwd()
files = os.listdir(path)
# Get all directories in current directory
dirs = [f for f in files if os.path.isdir(os.path.join(path, f))]
# Print all directories
for d in dirs:
print(d)
delete_git(dirs)
for d in dirs:
create_remote_repo(d)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment